Skip to content

Commit

Permalink
# This is a combination of 39 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

feat: add prompt-ui

# This is the commit message #2:

feat: configure and set algod data directory

# This is the commit message #3:

fix: RX/TX display

# This is the commit message #4:

fix: bit rate display for GB

# This is the commit message #5:

fix: configuration override order

# This is the commit message #6:

feat: handle invalid configuration and token gracefully

# This is the commit message #7:

test: fix test state

# This is the commit message #8:

fix: loading of custom endpoint address

# This is the commit message #9:

fix: loading default port

# This is the commit message #10:

test: clear viper settings

# This is the commit message #11:

fix: finds path to directory and gives cmd instruction

# This is the commit message #12:

feat: adds node start and node stop commands

# This is the commit message #13:

fix: add -y

# This is the commit message #14:

fix: turn script into indivudal commands

# This is the commit message #15:

feat: check if sudo, clarify shell

# This is the commit message #16:

chore: make more go idiomatic

# This is the commit message #17:

fix: fix proper path check

# This is the commit message #18:

fix: interact with systemctl, cleanup prompts

# This is the commit message #19:

fix: remove sudo

# This is the commit message #20:

fix: separate commands

# This is the commit message #21:

fix: proper algorand service name

# This is the commit message #22:

fix: calling with sudo

# This is the commit message #23:

chore: testing systemctl

# This is the commit message #24:

fix: checks algorand system service has been enabled directly

# This is the commit message #25:

feat: implements editAlgorandServiceFile

# This is the commit message #26:

fix: else statement

# This is the commit message #27:

fix: quick check branch

# This is the commit message #28:

fix: string template

# This is the commit message #29:

feat: adds upgrade

# This is the commit message #30:

chore: removeu nnecessary code

# This is the commit message #31:

fix: check that installed and candidate are the same

# This is the commit message #32:

chore: improve print

# This is the commit message #33:

chore: add more output

# This is the commit message #34:

fix: single quote

# This is the commit message #35:

fix: -y

# This is the commit message #36:

fix: systemctl

# This is the commit message #37:

fix: upgrade and sudo text

# This is the commit message #38:

chore: go mod tidy

# This is the commit message #39:

fix: upgrade
  • Loading branch information
HashMapsData2Value committed Nov 25, 2024
1 parent 3036470 commit ea7c2b3
Show file tree
Hide file tree
Showing 19 changed files with 1,121 additions and 20 deletions.
609 changes: 609 additions & 0 deletions cmd/node.go

Large diffs are not rendered by default.

78 changes: 63 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"encoding/json"
"fmt"
"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/internal"
"github.com/algorandfoundation/hack-tui/ui"
Expand Down Expand Up @@ -39,11 +40,24 @@ var (
},
RunE: func(cmd *cobra.Command, args []string) error {
log.SetOutput(cmd.OutOrStdout())
initConfig()

if viper.GetString("server") == "" {
return fmt.Errorf(style.Red.Render("server is required"))
}
if viper.GetString("token") == "" {
return fmt.Errorf(style.Red.Render("token is required"))
}

client, err := getClient()
cobra.CheckErr(err)

partkeys, err := internal.GetPartKeys(context.Background(), client)
cobra.CheckErr(err)
if err != nil {
return fmt.Errorf(
style.Red.Render("failed to get participation keys: %s"),
err)
}

state := internal.StateModel{
Status: internal.StatusModel{
Expand Down Expand Up @@ -106,7 +120,7 @@ func check(err interface{}) {
// Handle global flags and set usage templates
func init() {
log.SetReportTimestamp(false)
initConfig()

// Configure Version
if Version == "" {
Version = "unknown (built from source)"
Expand Down Expand Up @@ -142,6 +156,15 @@ type AlgodConfig struct {
EndpointAddress string `json:"EndpointAddress"`
}

func replaceEndpointUrl(s string) string {
s = strings.Replace(s, "\n", "", 1)
s = strings.Replace(s, "0.0.0.0", "127.0.0.1", 1)
s = strings.Replace(s, "[::]", "127.0.0.1", 1)
return s
}
func hasWildcardEndpointUrl(s string) bool {
return strings.Contains(s, "0.0.0.0") || strings.Contains(s, "::")
}
func initConfig() {
// Find home directory.
home, err := os.UserHomeDir()
Expand All @@ -159,12 +182,17 @@ func initConfig() {

// Load Configurations
viper.AutomaticEnv()
err = viper.ReadInConfig()
_ = viper.ReadInConfig()

// Check for server
loadedServer := viper.GetString("server")
loadedToken := viper.GetString("token")

// Load ALGORAND_DATA/config.json
algorandData, exists := os.LookupEnv("ALGORAND_DATA")

// Load the Algorand Data Configuration
if exists && algorandData != "" {
if exists && algorandData != "" && loadedServer == "" {
// Placeholder for Struct
var algodConfig AlgodConfig

Expand All @@ -183,23 +211,43 @@ func initConfig() {
err = configFile.Close()
check(err)

// Replace catchall address with localhost
if strings.Contains(algodConfig.EndpointAddress, "0.0.0.0") {
algodConfig.EndpointAddress = strings.Replace(algodConfig.EndpointAddress, "0.0.0.0", "127.0.0.1", 1)
// Check for endpoint address
if hasWildcardEndpointUrl(algodConfig.EndpointAddress) {
algodConfig.EndpointAddress = replaceEndpointUrl(algodConfig.EndpointAddress)
} else if algodConfig.EndpointAddress == "" {
// Assume it is not set, try to discover the port from the network file
networkPath := algorandData + "/algod.net"
networkFile, err := os.Open(networkPath)
check(err)

byteValue, err = io.ReadAll(networkFile)
check(err)

if hasWildcardEndpointUrl(string(byteValue)) {
algodConfig.EndpointAddress = replaceEndpointUrl(string(byteValue))
} else {
algodConfig.EndpointAddress = string(byteValue)
}

}
if strings.Contains(algodConfig.EndpointAddress, ":0") {
algodConfig.EndpointAddress = strings.Replace(algodConfig.EndpointAddress, ":0", ":8080", 1)
}
if loadedToken == "" {
// Handle Token Path
tokenPath := algorandData + "/algod.admin.token"

// Handle Token Path
tokenPath := algorandData + "/algod.admin.token"
tokenFile, err := os.Open(tokenPath)
check(err)

tokenFile, err := os.Open(tokenPath)
check(err)
byteValue, err = io.ReadAll(tokenFile)
check(err)

byteValue, err = io.ReadAll(tokenFile)
check(err)
viper.Set("token", strings.Replace(string(byteValue), "\n", "", 1))
}

// Set the server configuration
viper.Set("server", "http://"+algodConfig.EndpointAddress)
viper.Set("token", string(byteValue))
viper.Set("server", "http://"+strings.Replace(algodConfig.EndpointAddress, "\n", "", 1))
viper.Set("data", dataConfigPath)
}

Expand Down
54 changes: 53 additions & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,64 @@ func Test_ExecuteRootCommand(t *testing.T) {

func Test_InitConfig(t *testing.T) {
cwd, _ := os.Getwd()
t.Setenv("ALGORAND_DATA", cwd+"/testdata")
viper.Set("token", "")
viper.Set("server", "")
t.Setenv("ALGORAND_DATA", cwd+"/testdata/Test_InitConfig")

initConfig()
server := viper.Get("server")
if server == "" {
t.Fatal("Invalid Server")
}
if server != "http://127.0.0.1:8080" {
t.Fatal("Invalid Server")
}
}

func Test_InitConfigWithoutEndpoint(t *testing.T) {
cwd, _ := os.Getwd()
viper.Set("token", "")
viper.Set("server", "")
t.Setenv("ALGORAND_DATA", cwd+"/testdata/Test_InitConfigWithoutEndpoint")

initConfig()
server := viper.Get("server")
if server == "" {
t.Fatal("Invalid Server")
}
if server != "http://127.0.0.1:8080" {
t.Fatal("Invalid Server")
}
}

func Test_InitConfigWithAddress(t *testing.T) {
cwd, _ := os.Getwd()
viper.Set("token", "")
viper.Set("server", "")
t.Setenv("ALGORAND_DATA", cwd+"/testdata/Test_InitConfigWithAddress")

initConfig()
server := viper.Get("server")
if server == "" {
t.Fatal("Invalid Server")
}
if server != "http://255.255.255.255:8080" {
t.Fatal("Invalid Server")
}
}

func Test_InitConfigWithAddressAndDefaultPort(t *testing.T) {
cwd, _ := os.Getwd()
viper.Set("token", "")
viper.Set("server", "")
t.Setenv("ALGORAND_DATA", cwd+"/testdata/Test_InitConfigWithAddressAndDefaultPort")

initConfig()
server := viper.Get("server")
if server == "" {
t.Fatal("Invalid Server")
}
if server != "http://255.255.255.255:8080" {
t.Fatal("Invalid Server")
}
}
1 change: 1 addition & 0 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var statusCmd = &cobra.Command{
Short: "Get the node status",
Long: style.Purple(BANNER) + "\n" + style.LightBlue("View the node status"),
RunE: func(cmd *cobra.Command, args []string) error {
initConfig()
if viper.GetString("server") == "" {
return errors.New(style.Magenta("server is required"))
}
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions cmd/testdata/Test_InitConfigWithAddress/algod.admin.token
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
4 changes: 4 additions & 0 deletions cmd/testdata/Test_InitConfigWithAddress/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"EndpointAddress": "255.255.255.255:8080",
"OtherKey": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"EndpointAddress": "255.255.255.255:0",
"OtherKey": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1 change: 1 addition & 0 deletions cmd/testdata/Test_InitConfigWithoutEndpoint/algod.net
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[::]:8080
2 changes: 2 additions & 0 deletions cmd/testdata/Test_InitConfigWithoutEndpoint/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Loading

0 comments on commit ea7c2b3

Please sign in to comment.