1
// Copyright 2018 The CubeFS Authors.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
25
"github.com/spf13/cobra"
29
cmdConfigShort = "Manage global config file"
33
defaultHomeDir, _ = os.UserHomeDir()
34
defaultConfigName = ".cfs-cli.json"
35
defaultConfigPath = path.Join(defaultHomeDir, defaultConfigName)
36
defaultConfigData = []byte(`
44
defaultConfigTimeout uint16 = 60
48
MasterAddr []string `json:"masterAddr"`
49
Timeout uint16 `json:"timeout"`
50
ClientIDKey string `json:"clientIDKey"`
53
func newConfigCmd() *cobra.Command {
54
cmd := &cobra.Command{
55
Use: CliResourceConfig,
56
Short: cmdConfigShort,
58
cmd.AddCommand(newConfigSetCmd())
59
cmd.AddCommand(newConfigInfoCmd())
64
cmdConfigSetShort = "set value of config file"
65
cmdConfigInfoShort = "show info of config file"
68
func newConfigSetCmd() *cobra.Command {
69
var optMasterHosts string
71
cmd := &cobra.Command{
73
Short: cmdConfigSetShort,
74
Long: `Set the config file`,
75
Run: func(cmd *cobra.Command, args []string) {
80
tmp, _ := strconv.Atoi(optTimeout)
81
if tmp > math.MaxUint16 {
82
stdoutln("Please reset timeout. Input less than math.MaxUint16")
85
timeOut := uint16(tmp)
86
if optMasterHosts == "" {
87
stdout("Please set addr. Input 'cfs-cli config set -h' for help.\n")
92
stdout("timeOut %v is invalid.\n", timeOut)
96
if err = setConfig(optMasterHosts, timeOut); err != nil {
99
stdout("Config has been set successfully!\n")
102
cmd.Flags().StringVar(&optMasterHosts, "addr", "",
103
"Specify master address {HOST}:{PORT}[,{HOST}:{PORT}]")
104
cmd.Flags().StringVar(&optTimeout, "timeout", "60", "Specify timeout for requests [Unit: s]")
108
func newConfigInfoCmd() *cobra.Command {
109
var optFilterStatus string
110
var optFilterWritable string
111
cmd := &cobra.Command{
113
Short: cmdConfigInfoShort,
114
Run: func(cmd *cobra.Command, args []string) {
115
config, err := LoadConfig()
117
printConfigInfo(config)
120
cmd.Flags().StringVar(&optFilterWritable, "filter-writable", "", "Filter node writable status")
121
cmd.Flags().StringVar(&optFilterStatus, "filter-status", "", "Filter node status [Active, Inactive]")
125
func printConfigInfo(config *Config) {
126
stdout("Config info:\n")
127
stdout(" Master Address : %v\n", config.MasterAddr)
128
stdout(" Request Timeout [s]: %v\n", config.Timeout)
131
func setConfig(masterHosts string, timeout uint16) (err error) {
133
if config, err = LoadConfig(); err != nil {
136
hosts := strings.Split(masterHosts, ",")
137
if masterHosts != "" && len(hosts) > 0 {
138
config.MasterAddr = hosts
141
config.Timeout = timeout
143
var configData []byte
144
if configData, err = json.Marshal(config); err != nil {
147
if err = os.WriteFile(defaultConfigPath, configData, 0o600); err != nil {
153
func LoadConfig() (*Config, error) {
155
var configData []byte
156
if configData, err = os.ReadFile(defaultConfigPath); err != nil && !os.IsNotExist(err) {
159
if os.IsNotExist(err) {
160
if err = os.WriteFile(defaultConfigPath, defaultConfigData, 0o600); err != nil {
163
configData = defaultConfigData
166
if err = json.Unmarshal(configData, config); err != nil {
169
if config.Timeout == 0 {
170
config.Timeout = defaultConfigTimeout