Skip to content

Commit

Permalink
feat(node): charmbracelet logger
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Dec 12, 2024
1 parent bae2d6f commit 8414f71
Show file tree
Hide file tree
Showing 16 changed files with 331 additions and 251 deletions.
10 changes: 5 additions & 5 deletions cmd/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
)

var Cmd = &cobra.Command{
Use: "configure",
Short: "Configure Algod",
Long: "Configure Algod settings",
SilenceUsage: true,
PersistentPreRunE: node.NeedsToBeStopped,
Use: "configure",
Short: "Configure Algod",
Long: "Configure Algod settings",
SilenceUsage: true,
PersistentPreRun: node.NeedsToBeStopped,
//RunE: func(cmd *cobra.Command, args []string) error {
// return configureNode()
//},
Expand Down
39 changes: 34 additions & 5 deletions cmd/node/debug.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
package node

import (
"encoding/json"
"fmt"
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/internal/algod/utils"
"github.com/algorandfoundation/algorun-tui/internal/system"
"github.com/algorandfoundation/algorun-tui/ui/style"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
"os/exec"
)

type DebugInfo struct {
InPath bool `json:"inPath"`
IsRunning bool `json:"isRunning"`
IsService bool `json:"isService"`
IsInstalled bool `json:"isInstalled"`
Algod string `json:"algod"`
Data []string `json:"data"`
}

var debugCmd = &cobra.Command{
Use: "debug",
Short: "Display debug information for developers",
Long: "Prints debug data to be copy and pasted to a bug report.",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
log.Info("Collecting debug information...")

// Warn user for prompt
log.Warn(style.Yellow.Render(SudoWarningMsg))

paths := utils.GetKnownDataPaths()
fmt.Printf("Algod in PATH: %v\n", system.CmdExists("algod"))
fmt.Printf("Algod is installed: %v\n", algod.IsInstalled())
fmt.Printf("Algod is running: %v\n", algod.IsRunning())
fmt.Printf("Algod is service: %v\n", algod.IsService())
fmt.Printf("Algod paths: %+v\n", paths)
path, _ := exec.LookPath("algod")
info := DebugInfo{
InPath: system.CmdExists("algod"),
IsRunning: algod.IsRunning(),
IsService: algod.IsService(),
IsInstalled: algod.IsInstalled(),
Algod: path,
Data: paths,
}
data, err := json.MarshalIndent(info, "", " ")
if err != nil {
return err
}

log.Info(style.Blue.Render("Copy and paste the following to a bug report:"))
fmt.Println(style.Bold(string(data)))
return nil
},
}
38 changes: 30 additions & 8 deletions cmd/node/install.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
package node

import (
"fmt"
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/ui/style"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
"os"
)

const InstallMsg = "Installing Algorand"
const InstallExistsMsg = "algod is already installed"

var installCmd = &cobra.Command{
Use: "install",
Short: "Install Algorand node (Algod)",
Long: "Install Algorand node (Algod) and other binaries on your system",
Short: "Install the algorand daemon",
Long: style.Purple(style.BANNER) + "\n" + style.LightBlue("Install the algorand daemon on your local machine"),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Checking if Algod is installed...")
if algod.IsInstalled() {
return fmt.Errorf(InstallExistsMsg)
Run: func(cmd *cobra.Command, args []string) {
// TODO: yes flag

// TODO: get expected version
log.Info(style.Green.Render(InstallMsg + " vX.X.X"))
// Warn user for prompt
log.Warn(style.Yellow.Render(SudoWarningMsg))

// TODO: compare expected version to existing version
if algod.IsInstalled() && !force {
log.Error(InstallExistsMsg)
os.Exit(1)
}

// Run the installation
err := algod.Install()
if err != nil {
log.Error(err)
os.Exit(1)
}
return algod.Install()
log.Info(style.Green.Render("Algorand installed successfully 🎉"))
},
}

func init() {
installCmd.Flags().BoolVarP(&force, "force", "f", false, style.Yellow.Render("forcefully install the node"))
}
27 changes: 18 additions & 9 deletions cmd/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@ package node

import (
"errors"
"fmt"
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/ui/style"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
"os"
"runtime"
)

const SudoWarningMsg = "(You may be prompted for your password)"
const PermissionErrorMsg = "this command must be run with super-user privileges (sudo)"
const NotInstalledErrorMsg = "algod is not installed. please run the *node install* command"
const RunningErrorMsg = "algod is running, please run the *node stop* command"
const NotRunningErrorMsg = "algod is not running"

var (
force bool = false
)
var Cmd = &cobra.Command{
Use: "node",
Short: "Node Management",
Long: style.Purple(style.BANNER) + "\n" + style.LightBlue("Manage your Algorand node"),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {

// Check that we are calling with sudo on linux
if os.Geteuid() != 0 && runtime.GOOS == "linux" {
return errors.New(PermissionErrorMsg)
Expand All @@ -28,24 +33,28 @@ var Cmd = &cobra.Command{
},
}

func NeedsToBeRunning(cmd *cobra.Command, args []string) error {
func NeedsToBeRunning(cmd *cobra.Command, args []string) {
if force {
return
}
if !algod.IsInstalled() {
return fmt.Errorf(NotInstalledErrorMsg)
log.Fatal(NotInstalledErrorMsg)
}
if !algod.IsRunning() {
return fmt.Errorf(NotRunningErrorMsg)
log.Fatal(NotRunningErrorMsg)
}
return nil
}

func NeedsToBeStopped(cmd *cobra.Command, args []string) error {
func NeedsToBeStopped(cmd *cobra.Command, args []string) {
if force {
return
}
if !algod.IsInstalled() {
return fmt.Errorf(NotInstalledErrorMsg)
log.Fatal(NotInstalledErrorMsg)
}
if algod.IsRunning() {
return fmt.Errorf(RunningErrorMsg)
log.Fatal(RunningErrorMsg)
}
return nil
}

func init() {
Expand Down
27 changes: 20 additions & 7 deletions cmd/node/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@ package node

import (
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/ui/style"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
)

var startCmd = &cobra.Command{
Use: "start",
Short: "Start Algod",
Long: "Start Algod on your system (the one on your PATH).",
SilenceUsage: true,
PersistentPreRunE: NeedsToBeStopped,
RunE: func(cmd *cobra.Command, args []string) error {
return algod.Start()
Use: "start",
Short: "Start Algod",
Long: "Start Algod on your system (the one on your PATH).",
SilenceUsage: true,
PersistentPreRun: NeedsToBeStopped,
Run: func(cmd *cobra.Command, args []string) {
log.Info(style.Green.Render("Starting Algod 🚀"))
// Warn user for prompt
log.Warn(style.Yellow.Render(SudoWarningMsg))
err := algod.Start()
if err != nil {
log.Fatal(err)
}
log.Info(style.Green.Render("Algorand started successfully 🎉"))
},
}

func init() {
startCmd.Flags().BoolVarP(&force, "force", "f", false, style.Yellow.Render("forcefully start the node"))
}
23 changes: 16 additions & 7 deletions cmd/node/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package node
import (
"fmt"
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/ui/style"
"github.com/charmbracelet/log"
"time"

"github.com/spf13/cobra"
Expand All @@ -13,13 +15,16 @@ const StopSuccessMsg = "Algod stopped successfully"
const StopFailureMsg = "failed to stop Algod"

var stopCmd = &cobra.Command{
Use: "stop",
Short: "Stop Algod",
Long: "Stop the Algod process on your system.",
SilenceUsage: true,
PersistentPreRunE: NeedsToBeRunning,
Use: "stop",
Short: "Stop Algod",
Long: "Stop the Algod process on your system.",
SilenceUsage: true,
PersistentPreRun: NeedsToBeRunning,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Stopping Algod...")
log.Info(style.Green.Render("Stopping Algod 😢"))
// Warn user for prompt
log.Warn(style.Yellow.Render(SudoWarningMsg))

err := algod.Stop()
if err != nil {
return fmt.Errorf(StopFailureMsg)
Expand All @@ -30,7 +35,11 @@ var stopCmd = &cobra.Command{
return fmt.Errorf(StopFailureMsg)
}

fmt.Println(StopSuccessMsg)
log.Info(style.Green.Render("Algorand stopped successfully 🎉"))
return nil
},
}

func init() {
stopCmd.Flags().BoolVarP(&force, "force", "f", false, style.Yellow.Render("forcefully stop the node"))
}
31 changes: 24 additions & 7 deletions cmd/node/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@ package node

import (
"github.com/algorandfoundation/algorun-tui/internal/algod"
"github.com/algorandfoundation/algorun-tui/ui/style"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
)

const UninstallWarningMsg = "(You may be prompted for your password to uninstall)"

var uninstallCmd = &cobra.Command{
Use: "uninstall",
Short: "Uninstall Algorand node (Algod)",
Long: "Uninstall Algorand node (Algod) and other binaries on your system installed by this tool.",
SilenceUsage: true,
PersistentPreRunE: NeedsToBeStopped,
RunE: func(cmd *cobra.Command, args []string) error {
return algod.Uninstall()
Use: "uninstall",
Short: "Uninstall Algorand node (Algod)",
Long: "Uninstall Algorand node (Algod) and other binaries on your system installed by this tool.",
SilenceUsage: true,
PersistentPreRun: NeedsToBeStopped,
Run: func(cmd *cobra.Command, args []string) {
if force {
log.Warn(style.Red.Render("Uninstalling Algorand (forcefully)"))
}
// Warn user for prompt
log.Warn(style.Yellow.Render(UninstallWarningMsg))

err := algod.Uninstall(force)
if err != nil {
log.Fatal(err)
}
},
}

func init() {
uninstallCmd.Flags().BoolVarP(&force, "force", "f", false, style.Yellow.Render("forcefully uninstall the node"))
}
10 changes: 5 additions & 5 deletions cmd/node/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
)

var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Upgrade Algod",
Long: "Upgrade Algod (if installed with package manager).",
SilenceUsage: true,
PersistentPreRunE: NeedsToBeStopped,
Use: "upgrade",
Short: "Upgrade Algod",
Long: "Upgrade Algod (if installed with package manager).",
SilenceUsage: true,
PersistentPreRun: NeedsToBeStopped,
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: Check Version from S3 against the local binary
return algod.Update()
Expand Down
Loading

0 comments on commit 8414f71

Please sign in to comment.