1
0
mirror of https://github.com/rancher/os.git synced 2025-05-17 20:39:44 +00:00
os/cmd/control/config.go

227 lines
3.6 KiB
Go
Raw Normal View History

2015-02-17 21:31:37 +00:00
package control
import (
"fmt"
2015-02-18 01:42:26 +00:00
"io"
2015-02-17 21:31:37 +00:00
"io/ioutil"
2015-02-18 01:42:26 +00:00
"os"
2015-02-17 22:32:15 +00:00
"strings"
2015-02-17 21:31:37 +00:00
log "github.com/Sirupsen/logrus"
2015-02-17 22:32:15 +00:00
"gopkg.in/yaml.v2"
2015-02-17 21:31:37 +00:00
"github.com/codegangsta/cli"
"github.com/rancherio/os/config"
)
func configSubcommands() []cli.Command {
return []cli.Command{
{
2015-02-17 22:32:15 +00:00
Name: "get",
Usage: "get value",
Action: configGet,
2015-02-17 21:31:37 +00:00
},
2015-02-18 00:00:30 +00:00
{
Name: "set",
Usage: "set a value",
Action: configSet,
},
2015-02-17 21:31:37 +00:00
{
2015-02-18 01:42:26 +00:00
Name: "import",
Usage: "import configuration from standard in or a file",
2015-03-15 04:27:04 +00:00
Action: runImport,
2015-02-18 01:42:26 +00:00
Flags: []cli.Flag{
cli.StringFlag{
Name: "input, i",
Usage: "File from which to read",
},
},
2015-02-17 21:31:37 +00:00
},
{
Name: "export",
2015-03-15 04:27:04 +00:00
Usage: "export configuration",
2015-02-17 21:31:37 +00:00
Flags: []cli.Flag{
cli.StringFlag{
Name: "output, o",
Usage: "File to which to save",
},
cli.BoolFlag{
2015-03-15 04:27:04 +00:00
Name: "private, p",
Usage: "Include private information such as keys",
},
cli.BoolFlag{
Name: "full, f",
Usage: "Include full configuration, including internal and default settings",
2015-02-17 21:31:37 +00:00
},
},
2015-03-15 04:27:04 +00:00
Action: export,
},
{
Name: "merge",
Usage: "merge configuration from stdin",
Action: merge,
2015-02-17 21:31:37 +00:00
},
}
}
2015-03-15 04:27:04 +00:00
func runImport(c *cli.Context) {
var input io.ReadCloser
var err error
input = os.Stdin
2015-02-17 22:32:15 +00:00
cfg, err := config.LoadConfig()
if err != nil {
log.Fatal(err)
}
2015-02-18 01:42:26 +00:00
inputFile := c.String("input")
if inputFile != "" {
input, err = os.Open(inputFile)
if err != nil {
log.Fatal(err)
}
2015-03-15 04:27:04 +00:00
defer input.Close()
2015-02-18 00:00:30 +00:00
}
2015-02-18 01:42:26 +00:00
bytes, err := ioutil.ReadAll(input)
if err != nil {
log.Fatal(err)
}
2015-02-18 00:00:30 +00:00
2015-03-15 04:27:04 +00:00
err = cfg.Import(bytes)
2015-02-18 00:00:30 +00:00
if err != nil {
log.Fatal(err)
}
2015-02-18 01:42:26 +00:00
}
2015-02-18 00:00:30 +00:00
2015-02-18 01:42:26 +00:00
func configSet(c *cli.Context) {
key := c.Args().Get(0)
value := c.Args().Get(1)
if key == "" {
return
}
2015-03-15 04:27:04 +00:00
cfg, err := config.LoadConfig()
2015-02-18 01:42:26 +00:00
if err != nil {
log.Fatal(err)
}
2015-03-15 04:27:04 +00:00
err = cfg.Set(key, value)
2015-02-18 01:42:26 +00:00
if err != nil {
log.Fatal(err)
}
2015-02-18 00:00:30 +00:00
}
func configGet(c *cli.Context) {
2015-02-17 22:32:15 +00:00
arg := c.Args().Get(0)
if arg == "" {
return
}
2015-03-15 04:27:04 +00:00
cfg, err := config.LoadConfig()
2015-02-18 00:00:30 +00:00
if err != nil {
log.Fatal(err)
}
2015-02-17 23:03:56 +00:00
2015-03-15 04:27:04 +00:00
val, err := cfg.Get(arg)
if err != nil {
log.Fatal(err)
}
2015-02-17 23:03:56 +00:00
printYaml := false
switch val.(type) {
case []interface{}:
printYaml = true
case map[interface{}]interface{}:
printYaml = true
}
if printYaml {
bytes, err := yaml.Marshal(val)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(bytes))
} else {
fmt.Println(val)
}
}
2015-02-18 00:00:30 +00:00
func getOrSetVal(args string, data map[interface{}]interface{}, value interface{}) interface{} {
parts := strings.Split(args, ".")
2015-02-17 22:32:15 +00:00
for i, part := range parts {
2015-02-17 23:03:56 +00:00
val, ok := data[part]
2015-02-18 00:00:30 +00:00
last := i+1 == len(parts)
2015-02-23 19:00:33 +00:00
2015-03-15 04:27:04 +00:00
// Reached end, set the value
2015-02-18 00:00:30 +00:00
if last && value != nil {
if s, ok := value.(string); ok {
value = config.DummyMarshall(s)
}
data[part] = value
return value
}
2015-03-15 04:27:04 +00:00
// Missing intermediate key, create key
if !last && value != nil && !ok {
newData := map[interface{}]interface{}{}
data[part] = newData
data = newData
continue
}
2015-02-17 23:03:56 +00:00
if !ok {
2015-02-17 22:32:15 +00:00
break
}
2015-02-17 23:03:56 +00:00
2015-02-18 00:00:30 +00:00
if last {
2015-02-17 23:03:56 +00:00
return val
}
newData, ok := val.(map[interface{}]interface{})
if !ok {
break
}
data = newData
2015-02-17 22:32:15 +00:00
}
2015-02-17 23:03:56 +00:00
return ""
2015-02-17 22:32:15 +00:00
}
2015-03-15 04:27:04 +00:00
func merge(c *cli.Context) {
bytes, err := ioutil.ReadAll(os.Stdin)
2015-02-17 21:31:37 +00:00
if err != nil {
log.Fatal(err)
}
2015-03-15 04:27:04 +00:00
cfg, err := config.LoadConfig()
if err != nil {
log.Fatal(err)
2015-02-17 21:31:37 +00:00
}
2015-03-15 04:27:04 +00:00
err = cfg.Merge(bytes)
if err != nil {
log.Fatal(err)
2015-02-17 21:31:37 +00:00
}
2015-03-15 04:27:04 +00:00
}
2015-02-17 21:31:37 +00:00
2015-03-15 04:27:04 +00:00
func export(c *cli.Context) {
content, err := config.Dump(c.Bool("private"), c.Bool("full"))
if err != nil {
log.Fatal(err)
}
2015-02-17 21:31:37 +00:00
output := c.String("output")
if output == "" {
fmt.Println(content)
} else {
err := ioutil.WriteFile(output, []byte(content), 0400)
if err != nil {
log.Fatal(err)
}
}
}