Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config store helper #328

Open
wants to merge 2 commits into
base: v2.7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,25 @@ func verifyCert(caCert []byte) (string, error) {
}

func loadConfig(ctx *cli.Context) (config.Config, error) {
switch ctx.GlobalString("config-helper") {
case "built-in":
return loadConfigNative(ctx)
default:
// allow loading of rancher config by triggering an
// external helper executable
return loadConfigWithHelper(ctx)
}
}

func loadConfigNative(ctx *cli.Context) (config.Config, error) {
// path will always be set by the global flag default
path := ctx.GlobalString("config")
path = filepath.Join(path, cfgFile)

cf := config.Config{
Path: path,
Servers: make(map[string]*config.ServerConfig),
Helper: ctx.GlobalString("config-helper"),
}

content, err := ioutil.ReadFile(path)
Expand All @@ -284,6 +296,26 @@ func loadConfig(ctx *cli.Context) (config.Config, error) {
return cf, err
}

// fetch rancher cli config by executing an external helper
func loadConfigWithHelper(ctx *cli.Context) (config.Config, error) {
cf := config.Config{
Path: "",
Servers: make(map[string]*config.ServerConfig),
Helper: ctx.GlobalString("config-helper"),
}

cmd := exec.Command(ctx.GlobalString("config-helper"), "get")
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
content, err := cmd.Output()
if err != nil {
return cf, err
}

err = json.Unmarshal(content, &cf)
return cf, err
}

func lookupConfig(ctx *cli.Context) (*config.ServerConfig, error) {
cf, err := loadConfig(ctx)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/url"
"os"
"os/exec"
"path"
"strings"

Expand All @@ -18,6 +19,8 @@ type Config struct {
Path string `json:"path,omitempty"`
// CurrentServer the user has in focus
CurrentServer string
// Helper executable to store config
Helper string
}

//ServerConfig holds the config for each server the user has setup
Expand All @@ -33,6 +36,17 @@ type ServerConfig struct {
}

func (c Config) Write() error {
switch c.Helper {
case "built-in":
return c.writeNative()
default:
// if rancher config was loaded by external helper
// use the same helper to persist the config
return c.writeWithHelper()
}
}

func (c Config) writeNative() error {
err := os.MkdirAll(path.Dir(c.Path), 0700)
if err != nil {
return err
Expand All @@ -50,6 +64,19 @@ func (c Config) Write() error {
return json.NewEncoder(output).Encode(c)
}

func (c Config) writeWithHelper() error {
logrus.Infof("Saving config with helper %s", c.Helper)
jsonConfig, err := json.Marshal(c)
if err != nil {
return err
}
cmd := exec.Command(c.Helper, "store", string(jsonConfig))
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
return err
}

func (c Config) FocusedServer() *ServerConfig {
return c.Servers[c.CurrentServer]
}
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func mainErr() error {
EnvVar: "RANCHER_CONFIG_DIR",
Value: os.ExpandEnv("${HOME}/.rancher"),
},
cli.StringFlag{
Name: "config-helper",
Usage: "Helper executable to load/store config",
EnvVar: "RANCHER_CONFIG_HELPER",
Value: os.ExpandEnv("built-in"),
},
}
app.Commands = []cli.Command{
cmd.AppCommand(),
Expand Down