From a71ce30408be91f4c32a5310307121514ecd15c8 Mon Sep 17 00:00:00 2001 From: Michael Schuett Date: Sun, 3 Feb 2019 17:29:59 -0500 Subject: [PATCH] Add jank dc config code in here is pretty crap. Should all be refactored but just wanted to get all the subcommands that I use day to day finished for now. --- datacenter.go | 157 +++++++++++++++++++++++++++++++++++++++++++++++++- go.mod | 5 +- power.go | 1 - 3 files changed, 158 insertions(+), 5 deletions(-) diff --git a/datacenter.go b/datacenter.go index 825bece..0fd69d3 100644 --- a/datacenter.go +++ b/datacenter.go @@ -1,7 +1,16 @@ package main import ( + "bufio" + "fmt" + "io/ioutil" + "os" + "path" + "path/filepath" + "strings" + cli "github.com/urfave/cli" + yaml "gopkg.in/yaml.v2" ) func datacenterSubcommand() cli.Command { @@ -9,8 +18,152 @@ func datacenterSubcommand() cli.Command { Name: "datacenter", Aliases: []string{"dc"}, Usage: "Manage multiple Collins configurations", - Action: func(c *cli.Context) error { - return nil + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "n, new", + Usage: "Create a new configuration file for Value at ~/.collins.yml.Vaulue", + Category: "Datacenter options", + }, + cli.StringFlag{ + Name: "H, host", + Usage: "Use value for host when setting up new datacenter", + Category: "Datacenter options", + }, + cli.StringFlag{ + Name: "u, username", + Usage: "Use value for username when setting up new datacenter", + Category: "Datacenter options", + }, + cli.StringFlag{ + Name: "p, password", + Usage: "Use value for password when setting up new datacenter", + Category: "Datacenter options", + }, + cli.BoolFlag{ + Name: "l, list", + Usage: "List configured collins instances", + Category: "Datacenter options", + }, }, + Action: datacenterRunCommand, + } +} + +func makeNewDatacenterConfig(c *cli.Context, conf string) { + confDefaults := struct { + Timeout int `yaml:"timeout"` + Host string `yaml:"host"` + Username string `yaml:"username"` + Password string `yaml:"password,omitempty"` + }{ + Timeout: 120, + Host: c.String("host"), + Username: c.String("username"), + Password: c.String("password"), + } + + reader := bufio.NewReader(os.Stdin) + if !c.IsSet("host") { + fmt.Print("Enter Collins URI for " + c.String("new") + " (i.e. https://collins." + c.String("new") + ".company.net): ") + host, _ := reader.ReadString('\n') + confDefaults.Host = strings.TrimSpace(host) + } + + if !c.IsSet("username") { + userDefault := os.Getenv("USER") + fmt.Print("Enter username (default: " + userDefault + "): ") + username, _ := reader.ReadString('\n') + if username == "" { + confDefaults.Username = userDefault + } else { + confDefaults.Username = strings.TrimSpace(username) + } } + + if !c.IsSet("password") { + fmt.Print("Enter password: ") + pass, _ := reader.ReadString('\n') + confDefaults.Password = strings.TrimSpace(pass) + } + + ybytes, _ := yaml.Marshal(&confDefaults) + outYamlFile := path.Join(os.Getenv("HOME"), ".collins.yml."+c.String("new")) + err := ioutil.WriteFile(outYamlFile, ybytes, 0600) + if err != nil { + logAndDie(err.Error()) + } +} + +func datacenterRunCommand(c *cli.Context) error { + // Check if the main config file is a symlink which means that we possibly control it + confFile := path.Join(os.Getenv("HOME"), ".collins.yml") + fd, err := os.Lstat(confFile) + if err != nil { + logAndDie(err.Error()) + } + + if fd.Mode()&os.ModeSymlink != os.ModeSymlink { + logAndDie("Unable to determine default Collins datacenter: " + confFile + " is not a symlink, which means \"collins dc\" is not managing this configuration") + } + + if c.IsSet("new") && !c.IsSet("list") { + makeNewDatacenterConfig(c, confFile) + } + + if c.IsSet("list") { + files, err := filepath.Glob(os.Getenv("HOME") + "/.collins.yml.*") + if err != nil { + logAndDie(err.Error()) + } + + currentConf, err := os.Readlink(confFile) + if err != nil { + logAndDie(err.Error()) + } + + for _, file := range files { + filename := path.Base(file) + prettyOutput := strings.SplitAfterN(filename, ".", 4)[3] + if file == currentConf { + fmt.Println(prettyOutput + " *") + } else { + fmt.Println(prettyOutput) + } + } + } + + if !c.IsSet("list") && !c.IsSet("new") { + switchToConf := "" + if c.NArg() > 0 { + switchToConf = c.Args().Get(0) + + files, err := filepath.Glob(os.Getenv("HOME") + "/.collins.yml.*") + if err != nil { + logAndDie(err.Error()) + } + + validDc := false + for _, file := range files { + filename := path.Base(file) + prettyOutput := strings.SplitAfterN(filename, ".", 4)[3] + if prettyOutput == switchToConf { + validDc = true + } + } + + if !validDc { + logAndDie("No Collins configuration for datacenter \"" + switchToConf + "\" found. Perhaps you want to create it with 'collins dc --new " + switchToConf + "'?") + } + + if err = os.Remove(confFile); err != nil { + logAndDie(err.Error()) + } + if err = os.Symlink(confFile+"."+switchToConf, confFile); err != nil { + logAndDie(err.Error()) + } + + } + } + + return nil } diff --git a/go.mod b/go.mod index 9f51cde..202c9a7 100644 --- a/go.mod +++ b/go.mod @@ -2,15 +2,16 @@ module cgit.xrt0x.com/xrt0x/collins-go-cli replace github.com/urfave/cli => github.com/michaeljs1990/cli v1.20.1-0.20190128030917-b0494d5188b4 +replace gopkg.in/tumblr/go-collins.v0 => /home/eatingthenight/code/go-collins + require ( bou.ke/monkey v1.0.1 // indirect github.com/bouk/monkey v1.0.1 github.com/davecgh/go-spew v1.1.1 // indirect - github.com/google/go-querystring v1.0.0 // indirect github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/schallert/iso8601 v0.0.0-20151102174922-d51701471974 // indirect github.com/stretchr/testify v1.2.2 // indirect github.com/urfave/cli v1.20.0 gopkg.in/tumblr/go-collins.v0 v0.0.0-20180412191224-96f4cd6792f7 + gopkg.in/yaml.v2 v2.2.2 ) diff --git a/power.go b/power.go index 2f14184..3098d83 100644 --- a/power.go +++ b/power.go @@ -33,7 +33,6 @@ func powerSubcommand() cli.Command { Category: "Power options", }, }, - Action: powerRunCommand, } }