Skip to content

Commit 143f477

Browse files
# This is a combination of 39 commits.
# 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
1 parent 49c0ee6 commit 143f477

File tree

19 files changed

+1121
-20
lines changed

19 files changed

+1121
-20
lines changed

cmd/node.go

Lines changed: 609 additions & 0 deletions
Large diffs are not rendered by default.

cmd/root.go

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"github.com/algorandfoundation/hack-tui/api"
78
"github.com/algorandfoundation/hack-tui/internal"
89
"github.com/algorandfoundation/hack-tui/ui"
@@ -39,11 +40,24 @@ var (
3940
},
4041
RunE: func(cmd *cobra.Command, args []string) error {
4142
log.SetOutput(cmd.OutOrStdout())
43+
initConfig()
44+
45+
if viper.GetString("server") == "" {
46+
return fmt.Errorf(style.Red.Render("server is required"))
47+
}
48+
if viper.GetString("token") == "" {
49+
return fmt.Errorf(style.Red.Render("token is required"))
50+
}
51+
4252
client, err := getClient()
4353
cobra.CheckErr(err)
4454

4555
partkeys, err := internal.GetPartKeys(context.Background(), client)
46-
cobra.CheckErr(err)
56+
if err != nil {
57+
return fmt.Errorf(
58+
style.Red.Render("failed to get participation keys: %s"),
59+
err)
60+
}
4761

4862
state := internal.StateModel{
4963
Status: internal.StatusModel{
@@ -106,7 +120,7 @@ func check(err interface{}) {
106120
// Handle global flags and set usage templates
107121
func init() {
108122
log.SetReportTimestamp(false)
109-
initConfig()
123+
110124
// Configure Version
111125
if Version == "" {
112126
Version = "unknown (built from source)"
@@ -142,6 +156,15 @@ type AlgodConfig struct {
142156
EndpointAddress string `json:"EndpointAddress"`
143157
}
144158

159+
func replaceEndpointUrl(s string) string {
160+
s = strings.Replace(s, "\n", "", 1)
161+
s = strings.Replace(s, "0.0.0.0", "127.0.0.1", 1)
162+
s = strings.Replace(s, "[::]", "127.0.0.1", 1)
163+
return s
164+
}
165+
func hasWildcardEndpointUrl(s string) bool {
166+
return strings.Contains(s, "0.0.0.0") || strings.Contains(s, "::")
167+
}
145168
func initConfig() {
146169
// Find home directory.
147170
home, err := os.UserHomeDir()
@@ -159,12 +182,17 @@ func initConfig() {
159182

160183
// Load Configurations
161184
viper.AutomaticEnv()
162-
err = viper.ReadInConfig()
185+
_ = viper.ReadInConfig()
186+
187+
// Check for server
188+
loadedServer := viper.GetString("server")
189+
loadedToken := viper.GetString("token")
190+
163191
// Load ALGORAND_DATA/config.json
164192
algorandData, exists := os.LookupEnv("ALGORAND_DATA")
165193

166194
// Load the Algorand Data Configuration
167-
if exists && algorandData != "" {
195+
if exists && algorandData != "" && loadedServer == "" {
168196
// Placeholder for Struct
169197
var algodConfig AlgodConfig
170198

@@ -183,23 +211,43 @@ func initConfig() {
183211
err = configFile.Close()
184212
check(err)
185213

186-
// Replace catchall address with localhost
187-
if strings.Contains(algodConfig.EndpointAddress, "0.0.0.0") {
188-
algodConfig.EndpointAddress = strings.Replace(algodConfig.EndpointAddress, "0.0.0.0", "127.0.0.1", 1)
214+
// Check for endpoint address
215+
if hasWildcardEndpointUrl(algodConfig.EndpointAddress) {
216+
algodConfig.EndpointAddress = replaceEndpointUrl(algodConfig.EndpointAddress)
217+
} else if algodConfig.EndpointAddress == "" {
218+
// Assume it is not set, try to discover the port from the network file
219+
networkPath := algorandData + "/algod.net"
220+
networkFile, err := os.Open(networkPath)
221+
check(err)
222+
223+
byteValue, err = io.ReadAll(networkFile)
224+
check(err)
225+
226+
if hasWildcardEndpointUrl(string(byteValue)) {
227+
algodConfig.EndpointAddress = replaceEndpointUrl(string(byteValue))
228+
} else {
229+
algodConfig.EndpointAddress = string(byteValue)
230+
}
231+
189232
}
233+
if strings.Contains(algodConfig.EndpointAddress, ":0") {
234+
algodConfig.EndpointAddress = strings.Replace(algodConfig.EndpointAddress, ":0", ":8080", 1)
235+
}
236+
if loadedToken == "" {
237+
// Handle Token Path
238+
tokenPath := algorandData + "/algod.admin.token"
190239

191-
// Handle Token Path
192-
tokenPath := algorandData + "/algod.admin.token"
240+
tokenFile, err := os.Open(tokenPath)
241+
check(err)
193242

194-
tokenFile, err := os.Open(tokenPath)
195-
check(err)
243+
byteValue, err = io.ReadAll(tokenFile)
244+
check(err)
196245

197-
byteValue, err = io.ReadAll(tokenFile)
198-
check(err)
246+
viper.Set("token", strings.Replace(string(byteValue), "\n", "", 1))
247+
}
199248

200249
// Set the server configuration
201-
viper.Set("server", "http://"+algodConfig.EndpointAddress)
202-
viper.Set("token", string(byteValue))
250+
viper.Set("server", "http://"+strings.Replace(algodConfig.EndpointAddress, "\n", "", 1))
203251
viper.Set("data", dataConfigPath)
204252
}
205253

cmd/root_test.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,64 @@ func Test_ExecuteRootCommand(t *testing.T) {
2121

2222
func Test_InitConfig(t *testing.T) {
2323
cwd, _ := os.Getwd()
24-
t.Setenv("ALGORAND_DATA", cwd+"/testdata")
24+
viper.Set("token", "")
25+
viper.Set("server", "")
26+
t.Setenv("ALGORAND_DATA", cwd+"/testdata/Test_InitConfig")
2527

2628
initConfig()
2729
server := viper.Get("server")
2830
if server == "" {
2931
t.Fatal("Invalid Server")
3032
}
33+
if server != "http://127.0.0.1:8080" {
34+
t.Fatal("Invalid Server")
35+
}
36+
}
37+
38+
func Test_InitConfigWithoutEndpoint(t *testing.T) {
39+
cwd, _ := os.Getwd()
40+
viper.Set("token", "")
41+
viper.Set("server", "")
42+
t.Setenv("ALGORAND_DATA", cwd+"/testdata/Test_InitConfigWithoutEndpoint")
43+
44+
initConfig()
45+
server := viper.Get("server")
46+
if server == "" {
47+
t.Fatal("Invalid Server")
48+
}
49+
if server != "http://127.0.0.1:8080" {
50+
t.Fatal("Invalid Server")
51+
}
52+
}
53+
54+
func Test_InitConfigWithAddress(t *testing.T) {
55+
cwd, _ := os.Getwd()
56+
viper.Set("token", "")
57+
viper.Set("server", "")
58+
t.Setenv("ALGORAND_DATA", cwd+"/testdata/Test_InitConfigWithAddress")
59+
60+
initConfig()
61+
server := viper.Get("server")
62+
if server == "" {
63+
t.Fatal("Invalid Server")
64+
}
65+
if server != "http://255.255.255.255:8080" {
66+
t.Fatal("Invalid Server")
67+
}
68+
}
3169

70+
func Test_InitConfigWithAddressAndDefaultPort(t *testing.T) {
71+
cwd, _ := os.Getwd()
72+
viper.Set("token", "")
73+
viper.Set("server", "")
74+
t.Setenv("ALGORAND_DATA", cwd+"/testdata/Test_InitConfigWithAddressAndDefaultPort")
75+
76+
initConfig()
77+
server := viper.Get("server")
78+
if server == "" {
79+
t.Fatal("Invalid Server")
80+
}
81+
if server != "http://255.255.255.255:8080" {
82+
t.Fatal("Invalid Server")
83+
}
3284
}

cmd/status.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var statusCmd = &cobra.Command{
1919
Short: "Get the node status",
2020
Long: style.Purple(BANNER) + "\n" + style.LightBlue("View the node status"),
2121
RunE: func(cmd *cobra.Command, args []string) error {
22+
initConfig()
2223
if viper.GetString("server") == "" {
2324
return errors.New(style.Magenta("server is required"))
2425
}
File renamed without changes.
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"EndpointAddress": "255.255.255.255:8080",
3+
"OtherKey": ""
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"EndpointAddress": "255.255.255.255:0",
3+
"OtherKey": ""
4+
}

0 commit comments

Comments
 (0)