Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: skip questions when already bootstrapped #103

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 31 additions & 51 deletions cmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
"time"

"github.com/algorandfoundation/nodekit/api"
cmdutils "github.com/algorandfoundation/nodekit/cmd/utils"
"github.com/algorandfoundation/nodekit/cmd/utils/explanations"
"github.com/algorandfoundation/nodekit/internal/algod"
"github.com/algorandfoundation/nodekit/internal/algod/utils"
"github.com/algorandfoundation/nodekit/internal/system"
"github.com/algorandfoundation/nodekit/ui"
"github.com/algorandfoundation/nodekit/ui/app"
"github.com/algorandfoundation/nodekit/ui/bootstrap"
"github.com/algorandfoundation/nodekit/ui/style"
Expand Down Expand Up @@ -63,8 +60,20 @@
log.Fatal("invalid state, exiting")
}

ctx := context.Background()
httpPkg := new(api.HttpPkg)
// Just launch the TUI if it's already running
if algod.IsInstalled() && algod.IsService() && algod.IsRunning() {
dir, err := algod.GetDataDir("")
if err != nil {
log.Fatal(err)
}
err = runTUI(RootCmd, dir, false)
if err != nil {
log.Fatal(err)
}
return nil

Check warning on line 73 in cmd/bootstrap.go

View check run for this annotation

Codecov / codecov/patch

cmd/bootstrap.go#L64-L73

Added lines #L64 - L73 were not covered by tests
}

// Render the welcome text
r, _ := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
)
Expand All @@ -75,6 +84,7 @@
}
fmt.Println(out)

// Create the Bootstrap TUI

Check warning on line 87 in cmd/bootstrap.go

View check run for this annotation

Codecov / codecov/patch

cmd/bootstrap.go#L87

Added line #L87 was not covered by tests
model := bootstrap.NewModel()
if algod.IsInstalled() {
model.BootstrapMsg.Install = false
Expand All @@ -92,15 +102,16 @@
}
}
}()

if _, err := p.Run(); err != nil {
log.Fatal(err)
}

// If the pointer is empty, return (should not happen)
if msg == nil {
return nil
}

// User Answer for Install Question
if msg.Install {
log.Warn(style.Yellow.Render(explanations.SudoWarningMsg))

Expand Down Expand Up @@ -128,17 +139,22 @@
}
}

// Find the data directory automatically
dataDir, err := algod.GetDataDir("")
if err != nil {
return err
}
// Create the client
client, err := algod.GetClient(dataDir)
if err != nil {
return err
}

// User answer for catchup question

Check warning on line 145 in cmd/bootstrap.go

View check run for this annotation

Codecov / codecov/patch

cmd/bootstrap.go#L145

Added line #L145 was not covered by tests
if msg.Catchup {
ctx := context.Background()
httpPkg := new(api.HttpPkg)

if err != nil {
return err
}

Check warning on line 152 in cmd/bootstrap.go

View check run for this annotation

Codecov / codecov/patch

cmd/bootstrap.go#L147-L152

Added lines #L147 - L152 were not covered by tests
// Create the client
client, err := algod.GetClient(dataDir)
if err != nil {
return err
}

Check warning on line 157 in cmd/bootstrap.go

View check run for this annotation

Codecov / codecov/patch

cmd/bootstrap.go#L154-L157

Added lines #L154 - L157 were not covered by tests
tasosbit marked this conversation as resolved.
Show resolved Hide resolved
network, err := utils.GetNetworkFromDataDir(dataDir)
if err != nil {
return err
Expand All @@ -160,42 +176,6 @@

}

t := new(system.Clock)
// Fetch the state and handle any creation errors
state, stateResponse, err := algod.NewStateModel(ctx, client, httpPkg, false)
cmdutils.WithInvalidResponsesExplanations(err, stateResponse, cmd.UsageString())
cobra.CheckErr(err)

// Construct the TUI Model from the State
m, err := ui.NewViewportViewModel(state, client)
cobra.CheckErr(err)

// Construct the TUI Application
p = tea.NewProgram(
m,
tea.WithAltScreen(),
tea.WithFPS(120),
)

// Watch for State Updates on a separate thread
// TODO: refactor into context aware watcher without callbacks
go func() {
state.Watch(func(status *algod.StateModel, err error) {
if err == nil {
p.Send(state)
}
if err != nil {
p.Send(state)
p.Send(err)
}
}, ctx, t)
}()

// Execute the TUI Application
_, err = p.Run()
if err != nil {
log.Fatal(err)
}
return nil
return runTUI(RootCmd, dataDir, false)

Check warning on line 179 in cmd/bootstrap.go

View check run for this annotation

Codecov / codecov/patch

cmd/bootstrap.go#L179

Added line #L179 was not covered by tests
},
}
85 changes: 47 additions & 38 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,44 +56,7 @@
},
Run: func(cmd *cobra.Command, args []string) {
log.SetOutput(cmd.OutOrStdout())
// Create the dependencies
ctx := context.Background()
client, err := algod.GetClient(algodData)
cobra.CheckErr(err)
httpPkg := new(api.HttpPkg)
t := new(system.Clock)
// Fetch the state and handle any creation errors
state, stateResponse, err := algod.NewStateModel(ctx, client, httpPkg, IncentivesDisabled)
utils.WithInvalidResponsesExplanations(err, stateResponse, cmd.UsageString())
cobra.CheckErr(err)

// Construct the TUI Model from the State
m, err := ui.NewViewportViewModel(state, client)
cobra.CheckErr(err)

// Construct the TUI Application
p := tea.NewProgram(
m,
tea.WithAltScreen(),
tea.WithFPS(120),
)

// Watch for State Updates on a separate thread
// TODO: refactor into context aware watcher without callbacks
go func() {
state.Watch(func(status *algod.StateModel, err error) {
if err == nil {
p.Send(state)
}
if err != nil {
p.Send(state)
p.Send(err)
}
}, ctx, t)
}()

// Execute the TUI Application
_, err = p.Run()
err := runTUI(cmd, algodData, IncentivesDisabled)

Check warning on line 59 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L59

Added line #L59 was not covered by tests
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -152,3 +115,49 @@
NeedsUpgrade = needsUpgrade
return RootCmd.Execute()
}

func runTUI(cmd *cobra.Command, dataDir string, incentivesFlag bool) error {
if cmd == nil {
return fmt.Errorf("cmd is nil")
}

Check warning on line 122 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L119-L122

Added lines #L119 - L122 were not covered by tests
// Create the dependencies
ctx := context.Background()
httpPkg := new(api.HttpPkg)
t := new(system.Clock)
client, err := algod.GetClient(dataDir)
cobra.CheckErr(err)

// Fetch the state and handle any creation errors
state, stateResponse, err := algod.NewStateModel(ctx, client, httpPkg, incentivesFlag)
utils.WithInvalidResponsesExplanations(err, stateResponse, cmd.UsageString())
cobra.CheckErr(err)

// Construct the TUI Model from the State
m, err := ui.NewViewportViewModel(state, client)
cobra.CheckErr(err)

// Construct the TUI Application
p := tea.NewProgram(
m,
tea.WithAltScreen(),
tea.WithFPS(120),
)

// Watch for State Updates on a separate thread
// TODO: refactor into context aware watcher without callbacks
go func() {
state.Watch(func(status *algod.StateModel, err error) {
if err == nil {
p.Send(state)
}
if err != nil {
p.Send(state)
p.Send(err)
}

Check warning on line 156 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L124-L156

Added lines #L124 - L156 were not covered by tests
}, ctx, t)
}()

// Execute the TUI Application
_, err = p.Run()
return err

Check warning on line 162 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L161-L162

Added lines #L161 - L162 were not covered by tests
}
Loading