diff --git a/config.go b/config.go index 7483bad..9e1e3fd 100644 --- a/config.go +++ b/config.go @@ -9,6 +9,14 @@ import ( "os/exec" "path/filepath" "runtime" + + homedir "github.com/mitchellh/go-homedir" +) + +const ( + envCredentials = "GOOGLE_APPLICATION_CREDENTIALS" + envClientID = "IAP_CLIENT_ID" + envCurlCommand = "IAP_CURL_BIN" ) type Config struct { @@ -81,7 +89,7 @@ func (cfg *Config) LoadFile(file string) error { return json.NewEncoder(f).Encode(cfg) } -func (cfg *Config) GetEnv(url string) (env Env, err error) { +func (cfg *Config) getEnvFromFile(url string) (env Env, err error) { u1, _ := neturl.Parse(url) for _, service := range cfg.Services { u2, _ := neturl.Parse(service.URL) @@ -93,6 +101,36 @@ func (cfg *Config) GetEnv(url string) (env Env, err error) { return } +func (cfg *Config) GetEnv(url string) (env Env, err error) { + env, _ = cfg.getEnvFromFile(url) + credentials := os.Getenv(envCredentials) + clientID := os.Getenv(envClientID) + binary := os.Getenv(envCurlCommand) + if credentials == "" { + credentials, _ = homedir.Expand(env.Credentials) + } + if clientID == "" { + clientID = env.ClientID + } + if binary == "" { + binary = env.Binary + } + if credentials == "" { + return env, fmt.Errorf("%s is missing", envCredentials) + } + if clientID == "" { + return env, fmt.Errorf("%s is missing", envClientID) + } + if binary == "" { + binary = "curl" + } + return Env{ + Credentials: credentials, + ClientID: clientID, + Binary: binary, + }, nil +} + func (cfg *Config) GetURLs() (list []string) { for _, service := range cfg.Services { list = append(list, service.URL) diff --git a/iap.go b/iap.go index 286ce62..0c80eab 100644 --- a/iap.go +++ b/iap.go @@ -30,10 +30,10 @@ type IAP struct { func newIAP(sa, id string) (*IAP, error) { if sa == "" { - return &IAP{}, errors.New("service account is missing") + return &IAP{}, errors.New("Service Account is missing") } if id == "" { - return &IAP{}, errors.New("IAP ID is missing") + return &IAP{}, errors.New("Client ID is missing") } return &IAP{ SA: sa, @@ -41,6 +41,7 @@ func newIAP(sa, id string) (*IAP, error) { }, nil } +// GetToken returns JWT token for authz func (c *IAP) GetToken() (token string, err error) { sa, err := ioutil.ReadFile(c.SA) if err != nil { diff --git a/main.go b/main.go index cc3ad47..d9e5754 100644 --- a/main.go +++ b/main.go @@ -10,14 +10,6 @@ import ( "path/filepath" "runtime" "strings" - - homedir "github.com/mitchellh/go-homedir" -) - -const ( - envCredentials = "GOOGLE_APPLICATION_CREDENTIALS" - envClientID = "IAP_CLIENT_ID" - envCurlCommand = "IAP_CURL_BIN" ) // CLI represents the attributes for command-line interface @@ -45,35 +37,35 @@ func main() { } func newCLI(args []string) (CLI, error) { - var cli CLI + var c CLI // TODO: make it customizable - cli.stdout = os.Stdout - cli.stderr = os.Stderr + c.stdout = os.Stdout + c.stderr = os.Stderr for _, arg := range args { switch arg { case "--list", "--list-urls": - cli.opt.list = true + c.opt.list = true case "--edit", "--edit-config": - cli.opt.edit = true + c.opt.edit = true default: u, err := url.Parse(arg) if err == nil { - cli.urls = append(cli.urls, *u) + c.urls = append(c.urls, *u) } else { - cli.args = append(cli.args, arg) + c.args = append(c.args, arg) } } } dir, _ := configDir() json := filepath.Join(dir, "config.json") - if err := cli.cfg.LoadFile(json); err != nil { - return cli, err + if err := c.cfg.LoadFile(json); err != nil { + return c, err } - return cli, nil + return c, nil } func (c CLI) exit(msg interface{}) int { @@ -84,6 +76,8 @@ func (c CLI) exit(msg interface{}) int { case error: fmt.Fprintf(c.stderr, "Error: %s\n", m.Error()) return 1 + case int: + return m case nil: return 0 default: @@ -105,13 +99,12 @@ func (c CLI) run() int { return c.exit(errors.New("invalid url or url not given")) } - env, _ := c.cfg.GetEnv(url) - i, err := getInfo(env) + env, err := c.cfg.GetEnv(url) if err != nil { return c.exit(err) } - iap, err := newIAP(i.credentials, i.clientID) + iap, err := newIAP(env.Credentials, env.ClientID) if err != nil { return c.exit(err) } @@ -127,7 +120,7 @@ func (c CLI) run() int { ) args = append(args, url) - return c.exit(runCommand(i.binary, args)) + return c.exit(runCommand(env.Binary, args)) } func (c CLI) getURL() string { @@ -137,41 +130,6 @@ func (c CLI) getURL() string { return c.urls[0].String() } -type info struct { - credentials string - clientID string - binary string -} - -func getInfo(env Env) (info, error) { - credentials := os.Getenv(envCredentials) - clientID := os.Getenv(envClientID) - binary := os.Getenv(envCurlCommand) - if credentials == "" { - credentials, _ = homedir.Expand(env.Credentials) - } - if clientID == "" { - clientID = env.ClientID - } - if binary == "" { - binary = env.Binary - } - if credentials == "" { - return info{}, fmt.Errorf("%s is missing", envCredentials) - } - if clientID == "" { - return info{}, fmt.Errorf("%s is missing", envClientID) - } - if binary == "" { - binary = "curl" - } - return info{ - credentials: credentials, - clientID: clientID, - binary: binary, - }, nil -} - func runCommand(command string, args []string) error { // Check if you have curl command if _, err := exec.LookPath(command); err != nil {