Skip to content

Commit

Permalink
Add jank dc config
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michaeljs1990 committed Feb 3, 2019
1 parent cc9cf4a commit a71ce30
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 5 deletions.
157 changes: 155 additions & 2 deletions datacenter.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,169 @@
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 {
return 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
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
1 change: 0 additions & 1 deletion power.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func powerSubcommand() cli.Command {
Category: "Power options",
},
},

Action: powerRunCommand,
}
}
Expand Down

0 comments on commit a71ce30

Please sign in to comment.