Skip to content

Commit

Permalink
Merge pull request #267 from utlemming/override_url
Browse files Browse the repository at this point in the history
Add flag for overriding API endpoint
  • Loading branch information
mauricio authored Oct 31, 2017
2 parents 1756d62 + da1b905 commit df75d61
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 6 additions & 0 deletions commands/doit.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ var DoitCmd = &Command{
},
}

// ApiURL holds the API URL to use.
var ApiURL string

// Token holds the global authorization token.
var Token string

Expand Down Expand Up @@ -76,12 +79,15 @@ func init() {
DoitCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/doctl/config.yaml)")
DoitCmd.PersistentFlags().StringVarP(&Token, "access-token", "t", "", "API V2 Access Token")
DoitCmd.PersistentFlags().StringVarP(&Output, "output", "o", "text", "output format [text|json]")
DoitCmd.PersistentFlags().StringVarP(&ApiURL, "api-url", "u", "", "Override default API V2 endpoint")
DoitCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
DoitCmd.PersistentFlags().BoolVarP(&Trace, "trace", "", false, "trace api access")

viper.SetEnvPrefix("DIGITALOCEAN")
viper.BindEnv("access-token", "DIGITALOCEAN_ACCESS_TOKEN")
viper.BindPFlag("access-token", DoitCmd.PersistentFlags().Lookup("access-token"))
viper.BindEnv("api-url", "DIGITALOCEAN_API_URL")
viper.BindPFlag("api-url", DoitCmd.PersistentFlags().Lookup("api-url"))
viper.BindPFlag("output", DoitCmd.PersistentFlags().Lookup("output"))
viper.BindEnv("enable-beta", "DIGITALOCEAN_ENABLE_BETA")

Expand Down
11 changes: 9 additions & 2 deletions doit.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,19 @@ func (c *LiveConfig) GetGodoClient(trace bool) (*godo.Client, error) {
oauthClient.Transport = r
}

godoClient, err := godo.New(oauthClient, godo.SetUserAgent(userAgent()))
args := []godo.ClientOpt{godo.SetUserAgent(userAgent())}

apiURL := viper.GetString("api-url")
if apiURL != "" {
args = append(args, godo.SetBaseURL(apiURL))
}

godoClient, err := godo.New(oauthClient, args...)
if err != nil {
return nil, err
}
c.godoClient = godoClient

c.godoClient = godoClient
return c.godoClient, nil
}

Expand Down

0 comments on commit df75d61

Please sign in to comment.