-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from jakefhyde/3-config-preservation
Add config vars subcommands, and updated logic to merge existing config
- Loading branch information
Showing
10 changed files
with
445 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package vars | ||
|
||
import ( | ||
"github.com/rancherlabs/corral/pkg/config" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCommandDelete() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "delete [NAME | [NAME...]]", | ||
Short: "Remove existing global variable.", | ||
Long: "Remove existing global variable.", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: deleteVar, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func deleteVar(_ *cobra.Command, args []string) { | ||
cfg := config.MustLoad() | ||
|
||
for _, arg := range args { | ||
delete(cfg.Vars, arg) | ||
} | ||
|
||
err := cfg.Save() | ||
if err != nil { | ||
logrus.Fatalf("%e", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package vars | ||
|
||
import ( | ||
"github.com/rancherlabs/corral/pkg/config" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCommandSet() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "set NAME VALUE", | ||
Short: "Create or update global variable.", | ||
Long: "Create or update global variable.", | ||
Args: cobra.ExactArgs(2), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
createVar(args[0], args[1]) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func createVar(key, value string) { | ||
cfg := config.MustLoad() | ||
|
||
cfg.Vars[key] = value | ||
|
||
err := cfg.Save() | ||
if err != nil { | ||
logrus.Fatalf("%e", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package vars | ||
|
||
import ( | ||
"github.com/rancherlabs/corral/pkg/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewVarsCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "vars [VAR | [VAR...]]", | ||
Short: "List and modify global configuration.", | ||
Long: "List and modify global configuration.", | ||
Run: listVars, | ||
} | ||
|
||
cmd.AddCommand( | ||
NewCommandSet(), | ||
NewCommandDelete()) | ||
return cmd | ||
} | ||
|
||
func listVars(_ *cobra.Command, args []string) { | ||
cfg := config.MustLoad() | ||
|
||
if len(args) == 1 { | ||
println(cfg.Vars[args[0]]) | ||
return | ||
} | ||
|
||
if len(args) > 1 { | ||
println("NAME\tVALUE") | ||
for _, k := range args { | ||
println(k + "\t" + cfg.Vars[k]) | ||
} | ||
return | ||
} | ||
|
||
println("NAME\tVALUE") | ||
for k, v := range cfg.Vars { | ||
println(k + "\t" + v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.