-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
289 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package node | ||
|
||
import ( | ||
"fmt" | ||
"github.com/algorandfoundation/algorun-tui/internal/algod" | ||
"github.com/algorandfoundation/algorun-tui/ui/app" | ||
"github.com/algorandfoundation/algorun-tui/ui/bootstrap" | ||
"github.com/algorandfoundation/algorun-tui/ui/style" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/glamour" | ||
"github.com/charmbracelet/log" | ||
"github.com/spf13/cobra" | ||
"time" | ||
) | ||
|
||
var in = `# Welcome! | ||
This is the beginning of your adventure into running the an Algorand node! | ||
Morbi mauris quam, ornare ac commodo et, posuere id sem. Nulla id condimentum mauris. In vehicula sit amet libero vitae interdum. Nullam ac massa in erat volutpat sodales. Integer imperdiet enim cursus, ullamcorper tortor vel, imperdiet diam. Maecenas viverra ex iaculis, vehicula ligula quis, cursus lorem. Mauris nec nunc feugiat tortor sollicitudin porta ac quis turpis. Nam auctor hendrerit metus et pharetra. | ||
` | ||
|
||
// bootstrapCmd defines the "debug" command used to display diagnostic information for developers, including debug data. | ||
var bootstrapCmd = &cobra.Command{ | ||
Use: "bootstrap", | ||
Short: "Initialize a fresh node. Alias for install, catchup, and start.", | ||
Long: "Text", | ||
SilenceUsage: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
fmt.Print(style.Purple(style.BANNER)) | ||
out, err := glamour.Render(in, "dark") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(out) | ||
|
||
model := bootstrap.NewModel() | ||
p := tea.NewProgram(model) | ||
var msg *app.BootstrapMsg | ||
go func() { | ||
for { | ||
val := <-model.Outside | ||
switch val.(type) { | ||
case app.BootstrapMsg: | ||
msgVal := val.(app.BootstrapMsg) | ||
msg = &msgVal | ||
} | ||
} | ||
}() | ||
|
||
if _, err := p.Run(); err != nil { | ||
log.Fatal(err) | ||
} | ||
if msg == nil { | ||
return nil | ||
} | ||
|
||
log.Warn(style.Yellow.Render(SudoWarningMsg)) | ||
if msg.Install && !algod.IsInstalled() { | ||
err := algod.Install() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Wait for algod | ||
time.Sleep(10 * time.Second) | ||
|
||
if !algod.IsRunning() { | ||
log.Fatal("algod is not running") | ||
} | ||
|
||
//if msg.Catchup { | ||
// ctx := context.Background() | ||
// httpPkg := new(api.HttpPkg) | ||
// client, err := algod.GetClient(endpoint, token) | ||
// | ||
// // Get the latest catchpoint | ||
// catchpoint, _, err := algod.GetLatestCatchpoint(httpPkg, status.Network) | ||
// if err != nil && err.Error() == api.InvalidNetworkParamMsg { | ||
// log.Fatal("This network does not support fast-catchup.") | ||
// } else { | ||
// log.Info(style.Green.Render("Latest Catchpoint: " + catchpoint)) | ||
// } | ||
// | ||
// // Start catchup | ||
// res, _, err := algod.StartCatchup(ctx, client, catchpoint, nil) | ||
// if err != nil { | ||
// log.Fatal(err) | ||
// } | ||
// | ||
//} | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package app | ||
|
||
import ( | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
type BootstrapMsg struct { | ||
Install bool | ||
Catchup bool | ||
} | ||
|
||
type BoostrapSelected BootstrapMsg | ||
|
||
// EmitBootstrapSelection waits for and retrieves a new set of table rows from a given channel. | ||
func EmitBootstrapSelection(selection BoostrapSelected) tea.Cmd { | ||
return func() tea.Msg { | ||
return selection | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package app | ||
|
||
import tea "github.com/charmbracelet/bubbletea" | ||
|
||
type Outside chan tea.Msg | ||
|
||
func NewOutside() Outside { | ||
return make(chan tea.Msg) | ||
} | ||
|
||
func (o Outside) Emit(msg tea.Msg) tea.Cmd { | ||
return func() tea.Msg { | ||
o <- msg | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package bootstrap | ||
|
||
import ( | ||
"github.com/algorandfoundation/algorun-tui/ui/app" | ||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/glamour" | ||
) | ||
|
||
type Question string | ||
|
||
const ( | ||
InstallQuestion Question = "install" | ||
CatchupQuestion Question = "catchup" | ||
WaitingQuestion Question = "waiting" | ||
) | ||
|
||
const InstallQuestionMsg = `# Installing A Node | ||
It looks like you're running this for the first time. Would you like to install a node? (Y/n) | ||
` | ||
|
||
const CatchupQuestionMsg = `# Catching Up | ||
Would you like to preform a fast-catchup? (Y/n) | ||
` | ||
|
||
type Model struct { | ||
Outside app.Outside | ||
BootstrapMsg app.BootstrapMsg | ||
Question Question | ||
} | ||
|
||
func NewModel() Model { | ||
return Model{ | ||
Outside: make(app.Outside), | ||
Question: InstallQuestion, | ||
BootstrapMsg: app.BootstrapMsg{ | ||
Install: false, | ||
Catchup: false, | ||
}, | ||
} | ||
} | ||
func (m Model) Init() tea.Cmd { | ||
return textinput.Blink | ||
} | ||
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
m.Outside.Emit(msg) | ||
if m.Question == WaitingQuestion { | ||
return m, tea.Sequence(m.Outside.Emit(m.BootstrapMsg), tea.Quit) | ||
} | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "y": | ||
{ | ||
switch m.Question { | ||
case InstallQuestion: | ||
m.Question = CatchupQuestion | ||
m.BootstrapMsg.Install = true | ||
case CatchupQuestion: | ||
m.BootstrapMsg.Catchup = true | ||
m.Question = WaitingQuestion | ||
return m, app.EmitBootstrapSelection(app.BoostrapSelected(m.BootstrapMsg)) | ||
} | ||
|
||
} | ||
case "n": | ||
{ | ||
switch m.Question { | ||
case InstallQuestion: | ||
m.Question = CatchupQuestion | ||
m.BootstrapMsg.Install = true | ||
case CatchupQuestion: | ||
m.Question = WaitingQuestion | ||
m.BootstrapMsg.Catchup = true | ||
case WaitingQuestion: | ||
return m, tea.Sequence(m.Outside.Emit(m.BootstrapMsg), tea.Quit) | ||
} | ||
|
||
} | ||
|
||
case "ctrl+c", "esc", "q": | ||
return m, tea.Quit | ||
} | ||
} | ||
return m, cmd | ||
} | ||
|
||
func (m Model) View() string { | ||
var str string | ||
switch m.Question { | ||
case InstallQuestion: | ||
str = InstallQuestionMsg | ||
case CatchupQuestion: | ||
str = CatchupQuestionMsg | ||
} | ||
msg, _ := glamour.Render(str, "dark") | ||
return msg | ||
} |
Oops, something went wrong.