Skip to content

Commit

Permalink
Merge pull request #32 from jakefhyde/3-config-preservation
Browse files Browse the repository at this point in the history
Add config vars subcommands, and updated logic to merge existing config
  • Loading branch information
jakefhyde authored Feb 14, 2022
2 parents 5b794aa + 186fe72 commit 6783048
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 163 deletions.
57 changes: 26 additions & 31 deletions cmd/config.go → cmd/config/config.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package cmd
package config

import (
"fmt"
"os"
"os/user"
"path/filepath"

"github.com/rancherlabs/corral/cmd/config/vars"
"github.com/rancherlabs/corral/pkg/config"
"github.com/rancherlabs/corral/pkg/corral"
"github.com/rancherlabs/corral/pkg/version"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -23,60 +22,56 @@ func NewCommandConfig() *cobra.Command {

cmd.Flags().String("user_id", "", "The user id is used by packages to help identify resources.")
cmd.Flags().String("public_key", "", "Path to a public key you want packages to install on nodes.")
cmd.Flags().StringArrayP("variable", "v", []string{}, "Global variable applied to all corrals.")

cmd.AddCommand(vars.NewVarsCommand())

return cmd
}

func configCorral(cmd *cobra.Command, _ []string) {
cfg, _ := config.Load()
userId, _ := cmd.Flags().GetString("user_id")
if userId != "" {
cfg.UserID = userId
}
userPublicKeyPath, _ := cmd.Flags().GetString("public_key")
if userPublicKeyPath != "" {
cfg.UserPublicKeyPath = userPublicKeyPath
}

if userId == "" {
u, _ := user.Current()
if u != nil {
userId = u.Username
if cfg.UserID == "" {
u, _ := user.Current()
if u != nil {
cfg.UserID = u.Username
}
}

if input := prompt(fmt.Sprintf("How should packages identify you(%s): ", userId)); len(input) > 0 {
userId = input
if input := prompt(fmt.Sprintf("How should packages identify you(%s): ", cfg.UserID)); len(input) > 0 {
cfg.UserID = input
}
}

if userPublicKeyPath == "" {
userRoot, _ := os.UserHomeDir()
if userRoot != "" {
userPublicKeyPath = filepath.Join(userRoot, ".ssh", "id_rsa.pub")
if cfg.UserPublicKeyPath == "" {
userRoot, _ := os.UserHomeDir()
if userRoot != "" {
cfg.UserPublicKeyPath = filepath.Join(userRoot, ".ssh", "id_rsa.pub")
}
}

if input := prompt(fmt.Sprintf("What ssh public key should packages use (%s): ", userPublicKeyPath)); len(input) > 0 {
userPublicKeyPath = input
if input := prompt(fmt.Sprintf("What ssh public key should packages use (%s): ", cfg.UserPublicKeyPath)); len(input) > 0 {
cfg.UserPublicKeyPath = input
}
}

globalVars := map[string]string{}
globalVarsSlice, _ := cmd.Flags().GetStringArray("variable")
for _, raw := range globalVarsSlice {
k, v := corral.ToVar(raw)
if k == "" {
logrus.Fatal("variables should be in the format <key>=<value>")
}
globalVars[k] = v
}

logrus.Info("installing corral, this can take a minute")

if err := config.Install(); err != nil {
logrus.Fatal(err)
}

newCfg := &config.Config{
UserID: userId,
UserPublicKeyPath: userPublicKeyPath,
Version: version.Version,
Vars: globalVars,
}
if err := newCfg.Save(); err != nil {
if err := cfg.Save(); err != nil {
logrus.Fatal("error saving configuration: ", err)
}

Expand Down
32 changes: 32 additions & 0 deletions cmd/config/vars/delete.go
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)
}
}
32 changes: 32 additions & 0 deletions cmd/config/vars/set.go
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)
}
}
42 changes: 42 additions & 0 deletions cmd/config/vars/vars.go
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)
}
}
2 changes: 1 addition & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NewCommandCreate() *cobra.Command {
}

func create(_ *cobra.Command, args []string) {
cfg := config.Load()
cfg := config.MustLoad()

var corr corral.Corral
corr.RootPath = config.CorralPath(args[0])
Expand Down
2 changes: 1 addition & 1 deletion cmd/package/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewCommandPublish() *cobra.Command {
}

func publish(_ *cobra.Command, args []string) {
cfg := config.Load()
cfg := config.MustLoad()

pkg, err := _package.LoadPackage(args[0])
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/rancherlabs/corral/cmd/config"
cmd_package "github.com/rancherlabs/corral/cmd/package"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -26,7 +27,7 @@ func Execute() {
}

rootCmd.AddCommand(
NewCommandConfig(),
config.NewCommandConfig(),
NewCommandDelete(),
NewCommandList(),
NewCommandVars(),
Expand Down
72 changes: 36 additions & 36 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,83 @@ module github.com/rancherlabs/corral
go 1.17

require (
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/go-version v1.4.0
github.com/hashicorp/hc-install v0.3.1
github.com/hashicorp/terraform-exec v0.15.0
github.com/jedib0t/go-pretty/v6 v6.2.4
github.com/hashicorp/terraform-exec v0.16.0
github.com/jedib0t/go-pretty/v6 v6.2.7
github.com/opencontainers/image-spec v1.0.2
github.com/pkg/sftp v1.10.1
github.com/pkg/sftp v1.13.4
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.8.1
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.0
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/apimachinery v0.23.0
k8s.io/apimachinery v0.23.3
oras.land/oras-go v1.0.0
)

require (
github.com/containerd/containerd v1.5.8
github.com/containerd/containerd v1.5.9
github.com/pkg/errors v0.9.1
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0
github.com/stretchr/testify v1.7.0
)

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/aws/aws-sdk-go v1.34.9 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v20.10.11+incompatible // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v20.10.11+incompatible // indirect
github.com/docker/cli v20.10.12+incompatible // indirect
github.com/docker/distribution v2.8.0+incompatible // indirect
github.com/docker/docker v20.10.12+incompatible // indirect
github.com/docker/docker-credential-helpers v0.6.4 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-logr/logr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/klauspost/compress v1.11.13 // indirect
github.com/klauspost/compress v1.14.2 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/term v0.0.0-20200312100748-672ec06f55cd // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.7.1 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.10.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/afero v1.8.1 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/zclconf/go-cty v1.9.1 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e // indirect
github.com/zclconf/go-cty v1.10.0 // indirect
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/genproto v0.0.0-20220211171837-173942840c17 // indirect
google.golang.org/grpc v1.44.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
k8s.io/klog/v2 v2.30.0 // indirect
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/klog/v2 v2.40.1 // indirect
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
)
Loading

0 comments on commit 6783048

Please sign in to comment.