Skip to content

Commit

Permalink
fix: configuration override order
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Nov 4, 2024
1 parent 3036470 commit fe7325a
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 14 deletions.
59 changes: 46 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
},
RunE: func(cmd *cobra.Command, args []string) error {
log.SetOutput(cmd.OutOrStdout())
initConfig()
client, err := getClient()
cobra.CheckErr(err)

Expand Down Expand Up @@ -106,7 +107,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 +143,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 +169,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 +198,41 @@ 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 {
// 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)
}

}

// Handle Token Path
tokenPath := algorandData + "/algod.admin.token"
if loadedToken == "" {
// 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("data", dataConfigPath)
}

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

func Test_InitConfig(t *testing.T) {
cwd, _ := os.Getwd()
t.Setenv("ALGORAND_DATA", cwd+"/testdata")
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()
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")
}
}
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.
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 @@
{
}

0 comments on commit fe7325a

Please sign in to comment.