-
Notifications
You must be signed in to change notification settings - Fork 323
/
config.go
175 lines (158 loc) · 5.04 KB
/
config.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2019 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package config
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/pkg/log"
)
// Directories
var (
// ConfigDir is the directory to store config file
ConfigDir string
// DefaultConfigFile is the default config file name
DefaultConfigFile string
)
// Error strings
var (
// ErrConfigNotMatch indicates error for no config matches
ErrConfigNotMatch = fmt.Errorf("no matching config")
// ErrEmptyEndpoint indicates error for empty endpoint
ErrEmptyEndpoint = fmt.Errorf("no endpoint has been set")
// ErrConfigDefaultAccountNotSet indicates an error for the default account not being set
ErrConfigDefaultAccountNotSet = fmt.Errorf("default account not set")
)
// Language type used to enumerate supported language of ioctl
type Language int
// Multi-language support
const (
English Language = iota
Chinese
)
// ConfigCmd represents the config command
var ConfigCmd = &cobra.Command{
Use: "config",
Short: "Get, set, or reset configuration for ioctl",
}
// Context represents the current context
type Context struct {
AddressOrAlias string `json:"addressOrAlias" yaml:"addressOrAlias"`
}
// Config defines the config schema
type Config struct {
Wallet string `json:"wallet" yaml:"wallet"`
Endpoint string `json:"endpoint" yaml:"endpoint"`
SecureConnect bool `json:"secureConnect" yaml:"secureConnect"`
Aliases map[string]string `json:"aliases" yaml:"aliases"`
DefaultAccount Context `json:"defaultAccount" yaml:"defaultAccount"`
Explorer string `json:"explorer" yaml:"explorer"`
Language string `json:"language" yaml:"language"`
Nsv2height uint64 `json:"nsv2height" yaml:"nsv2height"`
AnalyserEndpoint string `json:"analyserEndpoint" yaml:"analyserEndpoint"`
// WsEndpoint w3bstream endpoint
WsEndpoint string `json:"wsEndpoint" yaml:"wsEndpoint"`
}
var (
// ReadConfig represents the current config read from local
ReadConfig Config
// Insecure represents the insecure connect option of grpc dial, default is false
Insecure = false
// UILanguage represents the language of ioctl user interface, default is 0 representing English
UILanguage Language
)
func init() {
ConfigDir = os.Getenv("HOME") + "/.config/ioctl/default"
// Create path to config directory
if err := os.MkdirAll(ConfigDir, 0700); err != nil {
log.L().Panic(err.Error())
}
// Path to config file
DefaultConfigFile = ConfigDir + "/config.default"
// Load or reset config file
var err error
ReadConfig, err = LoadConfig()
if err != nil {
if os.IsNotExist(err) {
err = reset() // Config file doesn't exist
}
if err != nil {
log.L().Panic(err.Error())
}
}
// Check completeness of config file
completeness := true
if ReadConfig.Wallet == "" {
ReadConfig.Wallet = ConfigDir
completeness = false
}
if ReadConfig.Language == "" {
ReadConfig.Language = _supportedLanguage[0]
completeness = false
}
if ReadConfig.Nsv2height == 0 {
ReadConfig.Nsv2height = genesis.Default.FairbankBlockHeight
}
if ReadConfig.AnalyserEndpoint == "" {
ReadConfig.AnalyserEndpoint = _defaultAnalyserEndpoint
completeness = false
}
if ReadConfig.WsEndpoint == "" {
ReadConfig.WsEndpoint = _defaultWsEndpoint
completeness = false
}
if !completeness {
err := writeConfig()
if err != nil {
log.L().Panic(err.Error())
}
}
// Set language for ioctl
UILanguage = isSupportedLanguage(ReadConfig.Language)
if UILanguage == -1 {
UILanguage = 0
message := output.StringMessage(fmt.Sprintf("Language %s is not supported, English instead.",
ReadConfig.Language))
fmt.Println(message.Warn())
}
// Init subcommands
ConfigCmd.AddCommand(_configGetCmd)
ConfigCmd.AddCommand(_configSetCmd)
ConfigCmd.AddCommand(_configResetCmd)
}
// LoadConfig loads config file in yaml format
func LoadConfig() (Config, error) {
ReadConfig := Config{
Aliases: make(map[string]string),
}
in, err := os.ReadFile(filepath.Clean(DefaultConfigFile))
if err == nil {
if err := yaml.Unmarshal(in, &ReadConfig); err != nil {
return ReadConfig, err
}
}
return ReadConfig, err
}
// TranslateInLang returns translation in selected language
func TranslateInLang(translations map[Language]string, lang Language) string {
if tsl, ok := translations[lang]; ok {
return tsl
}
// Assumption: English should always be provided
return translations[English]
}
// Lang returns the selected language, default is English
func (c *Config) Lang() Language {
switch c.Language {
case "中文":
return Chinese
default:
return English
}
}