/
root.go
79 lines (67 loc) · 2.38 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
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// rootCmd represents the base command when called without any subcommands
var (
cfgFile string
token string
addr string
rootCmd = &cobra.Command{
Use: "sutol",
Short: "A simple CLI to test deal proposals to Filecoin miner.",
Long: `Sutol is a CLI library that can be used to send self-forged proposals to your Filecoin miner (WIP) and replay already sent proposals.
`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
)
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is golang:os.UseConfigDir()/sutol/.sutol.yaml, i.e in Linux $HOME/.config/sutol/.sutol.yaml)")
rootCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "Token to connect to lotus-daemon (default to env var SUTOL_TOKEN else value in conf file else empty string)")
rootCmd.PersistentFlags().StringVarP(&addr, "addr", "u", "", "Socket address (IP/hostname:port) of the lotus-daemon to connect to (default to env var SUTOL_ADDR else value in conf file else 'localhost:1234')")
viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token"))
viper.BindPFlag("addr", rootCmd.PersistentFlags().Lookup("addr"))
viper.SetDefault("token", "")
viper.SetDefault("addr", "localhost:1234")
}
func initConfig() {
initConfigAndPrint(true)
}
func initConfigAndPrint(doPrint bool) {
viper.SetEnvPrefix("sutol")
replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer) // replace `-` with `_` in environment variables
viper.AutomaticEnv()
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
configDir, err := os.UserConfigDir()
configDir = configDir + "/sutol"
cobra.CheckErr(err)
viper.AddConfigPath(configDir)
viper.SetConfigType("yaml")
viper.SetConfigName(".sutol")
}
if err := viper.ReadInConfig(); err == nil {
if doPrint {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
token = viper.GetString("token")
addr = viper.GetString("addr")
}