-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
107 lines (88 loc) · 2.63 KB
/
root.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package cmd
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/oasisprotocol/cli/cmd/account"
"github.com/oasisprotocol/cli/cmd/network"
"github.com/oasisprotocol/cli/cmd/paratime"
"github.com/oasisprotocol/cli/cmd/wallet"
"github.com/oasisprotocol/cli/config"
"github.com/oasisprotocol/cli/version"
_ "github.com/oasisprotocol/cli/wallet/file" // Register file wallet backend.
_ "github.com/oasisprotocol/cli/wallet/ledger" // Register ledger wallet backend.
)
var (
cfgFile string
rootCmd = &cobra.Command{
Use: "oasis",
Short: "CLI for interacting with the Oasis network",
Version: version.Software,
}
)
// Execute executes the root command.
func Execute() error {
return rootCmd.Execute()
}
func initVersions() {
cobra.AddTemplateFunc("toolchain", func() interface{} { return version.Toolchain })
cobra.AddTemplateFunc("sdk", func() interface{} { return version.GetOasisSDKVersion() })
cobra.AddTemplateFunc("core", func() interface{} { return version.GetOasisCoreVersion() })
rootCmd.SetVersionTemplate(`Software version: {{.Version}}
Oasis SDK version: {{ sdk }}
Oasis Core version: {{ core }}
Go toolchain version: {{ toolchain }}
`)
}
func initConfig() {
v := viper.New()
if cfgFile != "" {
// Use config file from the flag.
v.SetConfigFile(cfgFile)
} else {
const configFilename = "cli.toml"
configDir := config.DefaultDirectory()
configPath := filepath.Join(configDir, configFilename)
v.AddConfigPath(configDir)
v.SetConfigType("toml")
v.SetConfigName(configFilename)
// Ensure the configuration file exists.
_ = os.MkdirAll(configDir, 0o700)
if _, err := os.Stat(configPath); errors.Is(err, fs.ErrNotExist) {
if _, err := os.Create(configPath); err != nil {
cobra.CheckErr(fmt.Errorf("failed to create configuration file: %w", err))
}
// Populate the initial configuration file with defaults.
config.ResetDefaults()
_ = config.Save(v)
}
}
_ = v.ReadInConfig()
// Load and validate global configuration.
err := config.Load(v)
cobra.CheckErr(err)
changes, err := config.Global().Migrate()
cobra.CheckErr(err)
if changes {
err = config.Global().Save()
cobra.CheckErr(err)
}
err = config.Global().Validate()
cobra.CheckErr(err)
}
func init() {
initVersions()
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file to use")
rootCmd.AddCommand(network.Cmd)
rootCmd.AddCommand(paratime.Cmd)
rootCmd.AddCommand(wallet.Cmd)
rootCmd.AddCommand(account.Cmd)
rootCmd.AddCommand(addressBookCmd)
rootCmd.AddCommand(contractCmd)
rootCmd.AddCommand(txCmd)
}