From ca3cae5b71edd52c5a5157c5ec1ea9a935a5f1ae Mon Sep 17 00:00:00 2001 From: Bryce Kahle Date: Tue, 29 Aug 2017 23:54:23 -0400 Subject: [PATCH] Use envconfig to simplify config loading --- Gopkg.lock | 8 +++++- Gopkg.toml | 4 +++ client.go | 84 +++++++++++++++++++++++------------------------------- main.go | 2 +- 4 files changed, 48 insertions(+), 50 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 982154e..ad6ca61 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1,6 +1,12 @@ # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. +[[projects]] + name = "github.com/kelseyhightower/envconfig" + packages = ["."] + revision = "f611eb38b3875cc3bd991ca91c51d06446afa14c" + version = "v1.3.0" + [[projects]] name = "github.com/onsi/ginkgo" packages = [".","config","internal/codelocation","internal/containernode","internal/failer","internal/leafnodes","internal/remote","internal/spec","internal/spec_iterator","internal/specrunner","internal/suite","internal/testingtproxy","internal/writer","reporters","reporters/stenographer","reporters/stenographer/support/go-colorable","reporters/stenographer/support/go-isatty","types"] @@ -64,6 +70,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "0e66c08581bffd1a15228d9b0d7f6ed95cb264381ec354dd7aaaea7b4baaf307" + inputs-digest = "52fa661dd6d386d2cd5c2563854f16acd19f091ec4af53f78e85670cbc8dab10" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index c4d2a03..f99ba2e 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -21,3 +21,7 @@ [[constraint]] name = "github.com/satori/go.uuid" version = "1.1.0" + +[[constraint]] + name = "github.com/kelseyhightower/envconfig" + version = "1.3.0" diff --git a/client.go b/client.go index 8f42269..194b57c 100644 --- a/client.go +++ b/client.go @@ -5,56 +5,41 @@ import ( "os" "os/user" "path" - "strconv" "time" "github.com/brycekahle/sudolikeaboss/onepass" + "github.com/kelseyhightower/envconfig" log "github.com/sirupsen/logrus" ) -const DEFAULT_TIMEOUT_STRING_SECONDS = "30" -const DEFAULT_HOST = "sudolikeaboss://local" -const DEFAULT_WEBSOCKET_URI = "ws://127.0.0.1:6263/4" -const DEFAULT_WEBSOCKET_PROTOCOL = "" -const DEFAULT_WEBSOCKET_ORIGIN = "chrome-extension://aomjjhallfgjeglblehebfpbcfeobpgk" +type Configuration struct { + TimeoutSecs int `split_words:"true" default:"30"` + DefaultHost string `split_words:"true" default:"sudolikeaboss://local"` + StateDirectory string `split_words:"true"` -func LoadConfiguration() *onepass.Configuration { - defaultHost := os.Getenv("SUDOLIKEABOSS_DEFAULT_HOST") - if defaultHost == "" { - defaultHost = DEFAULT_HOST - } - - websocketURI := os.Getenv("SUDOLIKEABOSS_WEBSOCKET_URI") - if websocketURI == "" { - websocketURI = DEFAULT_WEBSOCKET_URI - } - - websocketProtocol := os.Getenv("SUDOLIKEABOSS_WEBSOCKET_PROTOCOL") - if websocketProtocol == "" { - websocketProtocol = DEFAULT_WEBSOCKET_PROTOCOL + Websocket struct { + URI string `default:"ws://127.0.0.1:6263/4"` + Protocol string + Origin string `default:"chrome-extension://aomjjhallfgjeglblehebfpbcfeobpgk"` } +} - websocketOrigin := os.Getenv("SUDOLIKEABOSS_WEBSOCKET_ORIGIN") - if websocketOrigin == "" { - websocketOrigin = DEFAULT_WEBSOCKET_ORIGIN +func LoadConfiguration() *Configuration { + conf := Configuration{} + err := envconfig.Process("SUDOLIKEABOSS", &conf) + if err != nil { + log.Fatal(err) } - stateDirectory := os.Getenv("SUDOLIKEABOSS_STATE_DIRECTORY") - if stateDirectory == "" { + if conf.StateDirectory == "" { usr, err := user.Current() if err != nil { log.Fatal(err) } - stateDirectory = path.Join(usr.HomeDir, ".sudolikeaboss") + conf.StateDirectory = path.Join(usr.HomeDir, ".sudolikeaboss") } - return &onepass.Configuration{ - WebsocketURI: websocketURI, - WebsocketProtocol: websocketProtocol, - WebsocketOrigin: websocketOrigin, - DefaultHost: defaultHost, - StateDirectory: stateDirectory, - } + return &conf } func retrievePasswordFromOnepassword(configuration *onepass.Configuration, done chan bool) { @@ -105,25 +90,21 @@ func registerWithOnepassword(configuration *onepass.Configuration, done chan boo func runSudolikeaboss() { done := make(chan bool) - configuration := LoadConfiguration() - - timeoutString := os.Getenv("SUDOLIKEABOSS_TIMEOUT_SECS") - if timeoutString == "" { - timeoutString = DEFAULT_TIMEOUT_STRING_SECONDS + conf := LoadConfiguration() + oc := onepass.Configuration{ + WebsocketURI: conf.Websocket.URI, + WebsocketOrigin: conf.Websocket.Origin, + WebsocketProtocol: conf.Websocket.Protocol, + StateDirectory: conf.StateDirectory, + DefaultHost: conf.DefaultHost, } - - timeout, err := strconv.ParseInt(timeoutString, 10, 16) - if err != nil { - os.Exit(1) - } - - go retrievePasswordFromOnepassword(configuration, done) + go retrievePasswordFromOnepassword(&oc, done) // Timeout if necessary select { case <-done: // Do nothing no need - case <-time.After(time.Duration(timeout) * time.Second): + case <-time.After(time.Duration(conf.TimeoutSecs) * time.Second): close(done) os.Exit(1) } @@ -134,9 +115,16 @@ func runSudolikeaboss() { func runSudolikeabossRegistration() { done := make(chan bool) - configuration := LoadConfiguration() + conf := LoadConfiguration() + oc := onepass.Configuration{ + WebsocketURI: conf.Websocket.URI, + WebsocketOrigin: conf.Websocket.Origin, + WebsocketProtocol: conf.Websocket.Protocol, + StateDirectory: conf.StateDirectory, + DefaultHost: conf.DefaultHost, + } - go registerWithOnepassword(configuration, done) + go registerWithOnepassword(&oc, done) // Close the app neatly <-done diff --git a/main.go b/main.go index 5111fa4..6377632 100644 --- a/main.go +++ b/main.go @@ -56,5 +56,5 @@ func main() { }, } - app.Run(os.Args) + _ = app.Run(os.Args) }