Rainbond/grctl/cmd/conf.go
2019-02-21 21:55:56 +08:00

92 lines
2.6 KiB
Go

// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// 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.
// 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.
// 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"
"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: "conf",
Usage: "Cluster and service configuration manage cmd",
Subcommands: []cli.Command{
cli.Command{
Name: "get",
Usage: "get all datacenter configs",
Action: func(c *cli.Context) error {
configs, err := clients.RegionClient.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
},
},
cli.Command{
Name: "put",
Usage: "put database configs",
Action: func(c *cli.Context) error {
key := c.Args().Get(0)
value := c.Args().Get(1)
configs, err := clients.RegionClient.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.RegionClient.Configs().Put(configs)
if err != nil {
return err
}
fmt.Printf("configs %s put success \n", key)
return nil
},
},
},
}
return c
}