Skip to content

Commit

Permalink
Allow configuration thru env variables (#1286)
Browse files Browse the repository at this point in the history
Closes #905
  • Loading branch information
0xforever9 authored Dec 19, 2023
1 parent 0902f76 commit 96c8b44
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/juno/juno.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -186,6 +187,9 @@ func NewCmd(config *node.Config, run func(*cobra.Command, []string) error) *cobr
}
}

v.AutomaticEnv()
v.SetEnvPrefix("JUNO")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
if err := v.BindPFlags(cmd.Flags()); err != nil {
return nil
}
Expand Down
103 changes: 103 additions & 0 deletions cmd/juno/juno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestConfigPrecedence(t *testing.T) {
cfgFileContents string
expectErr bool
inputArgs []string
env []string
expectedConfig *node.Config
}{
"default config with no flags": {
Expand Down Expand Up @@ -400,6 +401,95 @@ network: goerli
DBCacheSize: defaultMaxCacheSize,
},
},
"only set env variables": {
env: []string{"JUNO_HTTP_PORT", "8080", "JUNO_WS", "true", "JUNO_HTTP_HOST", "0.0.0.0"},
expectedConfig: &node.Config{
LogLevel: defaultLogLevel,
HTTP: defaultHTTP,
HTTPHost: "0.0.0.0",
HTTPPort: 8080,
Websocket: true,
WebsocketHost: defaultHost,
WebsocketPort: defaultWSPort,
GRPC: defaultGRPC,
GRPCHost: defaultHost,
GRPCPort: defaultGRPCPort,
Metrics: defaultMetrics,
MetricsHost: defaultHost,
MetricsPort: defaultMetricsPort,
DatabasePath: defaultDBPath,
Network: defaultNetwork,
Pprof: defaultPprof,
PprofHost: defaultHost,
PprofPort: defaultPprofPort,
Colour: defaultColour,
PendingPollInterval: defaultPendingPollInterval,
MaxVMs: defaultMaxVMs,
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
},
},
"some setting set in both env variables and flags": {
env: []string{"JUNO_DB_PATH", "/home/env/.juno"},
inputArgs: []string{"--db-path", "/home/flag/.juno"},
expectedConfig: &node.Config{
LogLevel: defaultLogLevel,
HTTP: defaultHTTP,
HTTPHost: defaultHost,
HTTPPort: defaultHTTPPort,
Websocket: defaultWS,
WebsocketHost: defaultHost,
WebsocketPort: defaultWSPort,
GRPC: defaultGRPC,
GRPCHost: defaultHost,
GRPCPort: defaultGRPCPort,
Metrics: defaultMetrics,
MetricsHost: defaultHost,
MetricsPort: defaultMetricsPort,
DatabasePath: "/home/flag/.juno",
Network: defaultNetwork,
Pprof: defaultPprof,
PprofHost: defaultHost,
PprofPort: defaultPprofPort,
Colour: defaultColour,
PendingPollInterval: defaultPendingPollInterval,
MaxVMs: defaultMaxVMs,
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
},
},
"some setting set in both env variables and config file": {
cfgFileContents: `db-path: /home/file/.juno`,
env: []string{"JUNO_DB_PATH", "/home/env/.juno"},
expectedConfig: &node.Config{
LogLevel: defaultLogLevel,
HTTP: defaultHTTP,
HTTPHost: defaultHost,
HTTPPort: defaultHTTPPort,
Websocket: defaultWS,
WebsocketHost: defaultHost,
WebsocketPort: defaultWSPort,
GRPC: defaultGRPC,
GRPCHost: defaultHost,
GRPCPort: defaultGRPCPort,
Metrics: defaultMetrics,
MetricsHost: defaultHost,
MetricsPort: defaultMetricsPort,
DatabasePath: "/home/env/.juno",
Network: defaultNetwork,
Pprof: defaultPprof,
PprofHost: defaultHost,
PprofPort: defaultPprofPort,
Colour: defaultColour,
PendingPollInterval: defaultPendingPollInterval,
MaxVMs: defaultMaxVMs,
MaxVMQueue: 2 * defaultMaxVMs,
RPCMaxBlockScan: defaultRPCMaxBlockScan,
DBCacheSize: defaultMaxCacheSize,
},
},
}

for name, tc := range tests {
Expand All @@ -409,6 +499,14 @@ network: goerli
tc.inputArgs = append(tc.inputArgs, "--config", fileN)
}

require.True(t, len(tc.env)%2 == 0, "The number of env variables should be an even number")

if len(tc.env) > 0 {
for i := 0; i < len(tc.env)/2; i++ {
os.Setenv(tc.env[2*i], tc.env[2*i+1])
}
}

config := new(node.Config)
cmd := juno.NewCmd(config, func(_ *cobra.Command, _ []string) error { return nil })
cmd.SetArgs(tc.inputArgs)
Expand All @@ -421,6 +519,11 @@ network: goerli
require.NoError(t, err)

assert.Equal(t, tc.expectedConfig, config)
if len(tc.env) > 0 {
for i := 0; i < len(tc.env)/2; i++ {
os.Unsetenv(tc.env[2*i])
}
}
})
}
}
Expand Down

0 comments on commit 96c8b44

Please sign in to comment.