Rainbond/grctl/cmd/configs.go

92 lines
2.6 KiB
Go
Raw Normal View History

2018-03-14 14:12:26 +08:00
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
2018-03-14 14:33:31 +08:00
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
2018-03-14 14:33:31 +08:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
2018-03-14 14:33:31 +08:00
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
2018-02-24 12:09:09 +08:00
"strings"
"github.com/apcera/termtables"
"github.com/goodrain/rainbond/grctl/clients"
"github.com/goodrain/rainbond/node/api/model"
"github.com/urfave/cli"
)
//NewCmdConfigs 全局配置相关命令
func NewCmdConfigs() cli.Command {
c := cli.Command{
Name: "configs",
Usage: "系统任务相关命令grctl tasks -h",
Subcommands: []cli.Command{
cli.Command{
Name: "get",
Usage: "get all datacenter configs",
Action: func(c *cli.Context) error {
configs, err := clients.NodeClient.Configs().Get()
if err != nil {
return err
}
taskTable := termtables.CreateTable()
taskTable.AddHeaders("Name", "CNName", "ValueType", "Value")
for _, config := range configs.Configs {
taskTable.AddRow(config.Name, config.CNName, config.ValueType, config.Value)
}
fmt.Println(taskTable.Render())
return nil
},
},
2018-02-24 12:09:09 +08:00
cli.Command{
Name: "put",
2018-02-24 12:16:25 +08:00
Usage: "put database configs",
2018-02-24 12:09:09 +08:00
Action: func(c *cli.Context) error {
2018-02-24 12:16:25 +08:00
key := c.Args().Get(0)
value := c.Args().Get(1)
2018-02-24 12:09:09 +08:00
configs, err := clients.NodeClient.Configs().Get()
if err != nil {
return err
}
gc := configs.Get(key)
if gc == nil {
gcnew := model.ConfigUnit{
Name: key,
}
configs.Add(gcnew)
gc = configs.Get(key)
}
if strings.Contains(value, ",") {
vas := strings.Split(value, ",")
gc.ValueType = "array"
gc.Value = vas
} else {
gc.ValueType = "string"
gc.Value = value
}
err = clients.NodeClient.Configs().Put(configs)
if err != nil {
return err
}
2018-02-24 12:10:28 +08:00
fmt.Printf("configs %s put success \n", key)
2018-02-24 12:09:09 +08:00
return nil
},
},
},
}
return c
}