8000 feat(engine): engine config edit command (#4787) · ovh/cds@3c250ce · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 3c250ce

Browse files
yesnaultfsamin
authored andcommitted
feat(engine): engine config edit command (#4787)
example: ./engine config edit ~/.cds/dev.toml log.level=error hatchery.swarm.commonConfiguration.name=hatchery-swarm-name > new.file.toml Signed-off-by: Yvonnick Esnault <yvonnick.esnault@corp.ovh.com>
1 parent 8af0783 commit 3c250ce

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

engine/main.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"io/ioutil"
67
"os"
78
"os/signal"
89
"sort"
10+
"strconv"
911
"strings"
1012
"syscall"
1113
"time"
1214

1315
"github.com/google/gops/agent"
1416
"github.com/spf13/cobra"
17+
"github.com/spf13/viper"
1518
_ "github.com/spf13/viper/remote"
1619
"github.com/yesnault/go-toml"
1720
"go.opencensus.io/tag"
@@ -73,6 +76,7 @@ func init() {
7376

7477
configCmd.AddCommand(configNewCmd)
7578
configCmd.AddCommand(configCheckCmd)
79+
configCmd.AddCommand(configEditCmd)
7680

7781
//Download command
7882
mainCmd.AddCommand(downloadCmd)
@@ -354,6 +358,74 @@ var configCheckCmd = &cobra.Command{
354358
},
355359
}
356360

361+
var configEditCmd = &cobra.Command{
362+
Use: "edit",
363+
Short: "Edit a CDS configuration file",
364+
Long: `$ engine config edit <path-toml-file> key=value key=value`,
365+
Example: `$ engine config edit conf.toml log.level=debug hatchery.swarm.commonConfiguration.name=hatchery-swarm-name`,
366+
Run: func(cmd *cobra.Command, args []string) {
367+
if len(args) < 2 {
368+
cmd.Help()
369+
sdk.Exit("Wrong usage")
370+
}
371+
372+
cfgFile = args[0]
373+
374+
if _, err := os.Stat(cfgFile); os.IsNotExist(err) {
375+
sdk.Exit("File %s doesn't exist", cfgFile)
376+
}
377+
378+
btes, err := ioutil.ReadFile(cfgFile)
379+
if err != nil {
380+
sdk.Exit("Error while read content of file %s - err:%v", cfgFile, err)
381+
}
382+
383+
tomlConf, err := toml.Load(string(btes))
384+
if err != nil {
385+
sdk.Exit("Error while load toml content of file %s - err:%v", cfgFile, err)
386+
}
387+
388+
for _, vk := range args[1:] {
389+
t := strings.Split(vk, "=")
390+
if len(t) != 2 {
391+
sdk.Exit("Invalid key=value: %v", vk)
392+
}
393+
// check if value is bool, float, int or else string
394+
if v, err := strconv.ParseBool(t[1]); err == nil {
395+
tomlConf.Set(t[0], "", false, v)
396+
} else if v, err := strconv.ParseFloat(t[1], 10); err == nil {
397+
tomlConf.Set(t[0], "", false, v)
398+
} else if v, err := strconv.ParseInt(t[1], 10, 64); err == nil {
399+
tomlConf.Set(t[0], "", false, v)
400+
} else {
401+
tomlConf.Set(t[0], "", false, t[1])
402+
}
403+
}
404+
405+
tmpFile := "cds.tmp.toml"
406+
if err := ioutil.WriteFile(tmpFile, []byte(tomlConf.String()), os.FileMode(0640)); err != nil {
407+
sdk.Exit("Error while create tempfile: %v", err)
408+
}
409+
410+
defer os.Remove(tmpFile)
411+
412+
viper.SetConfigFile(tmpFile)
413+
if err := viper.ReadInConfig(); err != nil {
414+
sdk.Exit(err.Error())
415+
}
416+
417+
if err := viper.Unmarshal(conf); err != nil {
418+
sdk.Exit("Unable to parse config: %v", err.Error())
419+
}
420+
421+
btesOutput, err := toml.Marshal(*conf)
422+
if err != nil {
423+
sdk.Exit("%v", err)
424+
}
425+
fmt.Println(string(btesOutput))
426+
},
427+
}
428+
357429
var startCmd = &cobra.Command{
358430
Use: "start",
359431
Short: "Start CDS",

0 commit comments

Comments
 (0)
0