@@ -3,15 +3,18 @@ package main
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "io/ioutil"
6
7
"os"
7
8
"os/signal"
8
9
"sort"
10
+ "strconv"
9
11
"strings"
10
12
"syscall"
11
13
"time"
12
14
13
15
"github.com/google/gops/agent"
14
16
"github.com/spf13/cobra"
17
+ "github.com/spf13/viper"
15
18
_ "github.com/spf13/viper/remote"
16
19
"github.com/yesnault/go-toml"
17
20
"go.opencensus.io/tag"
@@ -73,6 +76,7 @@ func init() {
73
76
74
77
configCmd .AddCommand (configNewCmd )
75
78
configCmd .AddCommand (configCheckCmd )
79
+ configCmd .AddCommand (configEditCmd )
76
80
77
81
//Download command
78
82
mainCmd .AddCommand (downloadCmd )
@@ -354,6 +358,74 @@ var configCheckCmd = &cobra.Command{
354
358
},
355
359
}
356
360
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
+
357
429
var startCmd = & cobra.Command {
358
430
Use : "start" ,
359
431
Short : "Start CDS" ,
0 commit comments