Skip to content

Commit

Permalink
Refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Apr 17, 2018
1 parent 2820bbb commit 496f1ff
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 60 deletions.
40 changes: 39 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions iap.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ 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,
ID: id,
}, nil
}

// GetToken returns JWT token for authz
func (c *IAP) GetToken() (token string, err error) {
sa, err := ioutil.ReadFile(c.SA)
if err != nil {
Expand Down
72 changes: 15 additions & 57 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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:
Expand All @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 496f1ff

Please sign in to comment.