-
Notifications
You must be signed in to change notification settings - Fork 109
/
main.go
79 lines (69 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package main
import (
"os"
"strings"
"github.com/ChainSafe/gossamer/cmd/gossamer/commands"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func main() {
rootCmd, err := commands.NewRootCommand()
if err != nil {
panic(err)
}
rootCmd.AddCommand(
commands.InitCmd,
commands.AccountCmd,
commands.ImportRuntimeCmd,
commands.BuildSpecCmd,
commands.PruneStateCmd,
commands.ImportStateCmd,
commands.VersionCmd,
)
configureCobraCmd("GSSMR")
if err := rootCmd.Execute(); err != nil {
log.Errorf("failed to execute root command: %s", err)
panic(err)
}
}
// configureCobraCmd configures the cobra command with the given environment prefix and default base path.
func configureCobraCmd(envPrefix string) {
cobra.OnInitialize(func() {
if err := initEnv(envPrefix); err != nil {
return
}
})
}
// initEnv sets to use ENV variables if set.
func initEnv(prefix string) error {
if err := copyEnvVars(prefix); err != nil {
return err
}
// env variables with GSSMR prefix (eg. GSSMR_ROOT)
viper.SetEnvPrefix(prefix)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.AutomaticEnv()
return nil
}
// copyEnvVars copies all envs like GSSMRROOT to GSSMR_ROOT,
// so we can support both formats.
func copyEnvVars(prefix string) error {
prefix = strings.ToUpper(prefix)
ps := prefix + "_"
for _, e := range os.Environ() {
kv := strings.SplitN(e, "=", 2)
if len(kv) == 2 {
k, v := kv[0], kv[1]
if strings.HasPrefix(k, prefix) && !strings.HasPrefix(k, ps) {
k2 := strings.Replace(k, prefix, ps, 1)
if err := os.Setenv(k2, v); err != nil {
return err
}
}
}
}
return nil
}