Skip to content

Commit

Permalink
allow host, user, and token to be set with environment, implements #155
Browse files Browse the repository at this point in the history
  • Loading branch information
zaquestion committed Jun 20, 2018
1 parent d3b1b72 commit 6f23a30
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 26 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/user"
"path"
"runtime"
"strings"

"github.com/spf13/viper"
"github.com/zaquestion/lab/cmd"
Expand All @@ -16,10 +17,7 @@ import (
// version gets set on releases during build by goreleaser.
var version = "master"

func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
cmd.Version = version

func loadConfig() (string, string, string) {
var home string
switch runtime.GOOS {
case "windows":
Expand All @@ -33,7 +31,6 @@ func main() {
}
home = u.HomeDir
}

confpath := path.Join(home, ".config")
if _, err := os.Stat(confpath); os.IsNotExist(err) {
os.Mkdir(confpath, 0700)
Expand All @@ -43,7 +40,11 @@ func main() {
viper.SetConfigType("hcl")
viper.AddConfigPath(".")
viper.AddConfigPath(confpath)

viper.SetEnvPrefix("LAB")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()

if _, ok := viper.ReadInConfig().(viper.ConfigFileNotFoundError); ok {
if err := config.New(path.Join(confpath, "lab.hcl"), os.Stdin); err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -73,10 +74,26 @@ func main() {
}
}

lab.Init(
cfg["host"].(string),
cfg["user"].(string),
cfg["token"].(string))
// Set environment overrides
// Note: the code below that uses `cfg["host"]` to access these values
// is tough to simplify since cfg["host"] is accessing the array "core"
// and viper.GetString("core.host") is expecting a non-array so it
// doens't match
if v := viper.GetString("core.host"); v != "" {
cfg["host"] = v
}
if v := viper.GetString("core.user"); v != "" {
cfg["user"] = v
}
if v := viper.GetString("core.token"); v != "" {
cfg["token"] = v
}
return cfg["host"].(string), cfg["user"].(string), cfg["token"].(string)
}

func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
cmd.Version = version
lab.Init(loadConfig())
cmd.Execute()
}

0 comments on commit 6f23a30

Please sign in to comment.