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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ import (
"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 @@ var bootstrapCmd = &cobra.Command{
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)
if err != nil {
log.Fatal(err)
}
return nil
}

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

// Create the Bootstrap TUI
model := bootstrap.NewModel()
if algod.IsInstalled() {
model.BootstrapMsg.Install = false
Expand All @@ -92,15 +102,16 @@ var bootstrapCmd = &cobra.Command{
}
}
}()

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 @@ var bootstrapCmd = &cobra.Command{
}
}

// 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
if msg.Catchup {
ctx := context.Background()
httpPkg := new(api.HttpPkg)

if err != nil {
return err
}
// Create the client
client, err := algod.GetClient(dataDir)
if err != nil {
return err
}
network, err := utils.GetNetworkFromDataDir(dataDir)
if err != nil {
return err
Expand All @@ -160,42 +176,6 @@ var bootstrapCmd = &cobra.Command{

}

t := new(system.Clock)
// Fetch the state and handle any creation errors
state, stateResponse, err := algod.NewStateModel(ctx, client, httpPkg)
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)
},
}
85 changes: 47 additions & 38 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,44 +53,7 @@ var (
},
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)
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)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -148,3 +111,49 @@ func Execute(version string, needsUpgrade bool) error {
NeedsUpgrade = needsUpgrade
return RootCmd.Execute()
}

func runTUI(cmd *cobra.Command, dataDir string) error {
if cmd == nil {
return fmt.Errorf("cmd is nil")
}
// 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)
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()
return err
}
Loading