Skip to content

Commit

Permalink
feat: tui application with live status
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Oct 15, 2024
1 parent 905dc13 commit 95f0924
Show file tree
Hide file tree
Showing 8 changed files with 476 additions and 41 deletions.
120 changes: 120 additions & 0 deletions api/lf.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 14 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cmd

import (
"context"
"encoding/json"
"errors"
"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/ui"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
"github.com/oapi-codegen/oapi-codegen/v2/pkg/securityprovider"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -34,16 +35,20 @@ var (
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
// TODO: Add default application
RunE: func(cmd *cobra.Command, args []string) error {
log.SetOutput(cmd.OutOrStdout())

if viper.GetString("server") == "" {
return errors.New(ui.Magenta("server is required"))
}

log.Info(ui.Purple("Arguments: " + strings.Join(args, " ") + "Server: " + viper.GetString("server")))
return nil
client, err := getClient()
cobra.CheckErr(err)

m, err := ui.MakeViewportViewModel(context.Background(), client)
cobra.CheckErr(err)

p := tea.NewProgram(
m,
tea.WithAltScreen(),
)
_, err = p.Run()
return err
},
}
)
Expand Down
33 changes: 4 additions & 29 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,19 @@
package cmd

import (
"bytes"
"github.com/spf13/viper"
"io"
"os"
"testing"
)

// Test the stub root command
func Test_ExecuteRootCommand(t *testing.T) {
// Set output
b := bytes.NewBufferString("")
rootCmd.SetOut(b)
rootCmd.SetArgs([]string{"--server", "https://mainnet-api.4160.nodely.dev:443"})
viper.Set("server", "https://mainnet-api.4160.nodely.dev:443")

// Execute
err := rootCmd.Execute()
if err != nil {
t.Fatal(err)
}

// Read the buffer
out, err := io.ReadAll(b)
if err != nil {
t.Fatal(err)
}

// Assert the command
if string(out) != "INFO Arguments: Server: https://mainnet-api.4160.nodely.dev:443\n" {
t.Fatalf("expected \"%s\" got \"%s\"", "hi", string(out))
}
}

func Test_ExecuteRootCommand_NoArgs(t *testing.T) {
b := bytes.NewBufferString("")
rootCmd.SetOut(b)
rootCmd.SetArgs([]string{})
err := rootCmd.Execute()
if err != nil {
// Should always fail due to no TTY
if err == nil {
t.Fatal(err)
}
}
Expand All @@ -50,7 +25,7 @@ func Test_InitConfig(t *testing.T) {
initConfig()
server := viper.Get("server")
if server == "" {
t.Fatal("Wow")
t.Fatal("Invalid Server")
}

}
3 changes: 2 additions & 1 deletion generate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ output-options:
include-operation-ids:
- Metrics
- GetStatus
- WaitForBlock
- WaitForBlock
- GetVersion
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.0
toolchain go1.23.1

require (
github.com/charmbracelet/bubbles v0.20.0
github.com/charmbracelet/bubbletea v1.1.1
github.com/charmbracelet/lipgloss v0.13.0
github.com/charmbracelet/log v0.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ
github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8=
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE=
github.com/charmbracelet/bubbles v0.20.0/go.mod h1:39slydyswPy+uVOHZ5x/GjwVAFkCsV8IIVy+4MhzwwU=
github.com/charmbracelet/bubbletea v1.1.1 h1:KJ2/DnmpfqFtDNVTvYZ6zpPFL9iRCRr0qqKOCvppbPY=
github.com/charmbracelet/bubbletea v1.1.1/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4=
github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=
Expand Down
29 changes: 27 additions & 2 deletions internal/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import (

// StatusModel represents a status response from algod.Status
type StatusModel struct {
HeartBeat chan uint64 // Subscription Channel
LastRound uint64 // Last recorded round
HeartBeat chan uint64 // Subscription Channel
Version string
Network string
Voting bool
NeedsUpdate bool
LastRound uint64 // Last recorded round
}

// String prints the last round value
Expand All @@ -19,11 +23,32 @@ func (m *StatusModel) String() string {

// Fetch handles algod.Status
func (m *StatusModel) Fetch(ctx context.Context, client *api.ClientWithResponses) error {
if m.Version == "" {
v, err := client.GetVersionWithResponse(ctx)
if err != nil {
return err
}
if v.StatusCode() != 200 {
return fmt.Errorf("Satus code %d: %s", v.StatusCode(), v.Status())
}
m.Network = v.JSON200.GenesisId
m.Version = fmt.Sprintf("v%d.%d.%d-%s", v.JSON200.Build.Major, v.JSON200.Build.Minor, v.JSON200.Build.BuildNumber, v.JSON200.Build.Channel)

}
m.HeartBeat = make(chan uint64)
s, err := client.GetStatusWithResponse(ctx)
if err != nil {
return err
}

if s.StatusCode() != 200 {
return fmt.Errorf("Satus code %d: %s", s.StatusCode(), s.Status())
}
m.LastRound = uint64(s.JSON200.LastRound)

if s.JSON200.UpgradeNodeVote != nil {
m.Voting = *s.JSON200.UpgradeNodeVote
}
return nil
}

Expand Down
Loading

0 comments on commit 95f0924

Please sign in to comment.