Skip to content

Commit

Permalink
Add SCALR_ACCOUNT to let user specify default account ID
Browse files Browse the repository at this point in the history
  • Loading branch information
buzzy committed Oct 10, 2022
1 parent bf5f452 commit c1f275e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Examples:
Environment variables:
SCALR_HOSTNAME Scalr Hostname, i.e example.scalr.io
SCALR_TOKEN Scalr API Token
SCALR_ACCOUNT Default Scalr Account ID, i.e acc-tq8cgt2hu6hpfuj
Options:
-version Shows current version of this binary
Expand All @@ -40,10 +41,14 @@ Before the CLI can be used, you will need to configure what Scalr URL and [Token
This can be done using environment variables (SCALR_HOSTNAME and SCALR_TOKEN) or a configuration file.
Run the CLI with the -configure flag to run the configuration wizard.

An optional environment variable called SCALR_ACCOUNT can be used to set a default account ID. When running a command that has either an -account or -account-id flag
(and not manually specified with flags), it will automatically be set to that default account ID.

```
user@server ~$ scalr -configure
Scalr Hostname [ex: example.scalr.io]: example.scalr.io
Scalr Token (not echoed!):
Default Scalr Account-ID [ex: acc-tq8cgt2hu6hpfuj]: acc-tq8cgt2hu6hpfuj
Configuration saved in /home/user/.scalr/scalr.conf
```

Expand Down
13 changes: 10 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ func parseCommand(format string, verbose bool) {
//Recursively collect all required fields
requiredFlags = collectRequired(action.RequestBody.Value.Content[contentType].Schema.Value)

//fmt.Printf("%+#v", requiredFlags)
//os.Exit(0)

var collectAttributes func(*openapi3.Schema, string)

//Function to support nested objects
Expand Down Expand Up @@ -175,6 +172,16 @@ func parseCommand(format string, verbose bool) {
//Validate all flags
subFlag.Parse(os.Args[pos+1:])

//If command has -account flag and no value set, use default account-ID
if flag, ok := flags["account"]; ok && *flag.value == "" {
*flag.value = ScalrAccount
}

//If command has -account-id flag and no value set, use default account-ID
if flag, ok := flags["account-id"]; ok && *flag.value == "" {
*flag.value = ScalrAccount
}

var missing []string
var missingBody []string

Expand Down
7 changes: 7 additions & 0 deletions configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ func runConfigure() {
bytepw, _ := term.ReadPassword(int(syscall.Stdin))
conf.Set(string(bytepw), "token")

fmt.Print("\nDefault Scalr Account-ID [ex: acc-tq8cgt2hu6hpfuj]: ")
scanner.Scan()
value := scanner.Text()
if value != "" {
conf.Set(scanner.Text(), "account")
}

home, err := os.UserHomeDir()
checkErr(err)

Expand Down
3 changes: 2 additions & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func printInfo() {

fmt.Print("Environment variables:", "\n")
fmt.Print(" SCALR_HOSTNAME", " ", "Scalr Hostname, i.e example.scalr.io", "\n")
fmt.Print(" SCALR_TOKEN", " ", "Scalr API Token", "\n\n")
fmt.Print(" SCALR_TOKEN", " ", "Scalr API Token", "\n")
fmt.Print(" SCALR_ACCOUNT", " ", "Default Scalr Account ID, i.e acc-tq8cgt2hu6hpfuj", "\n\n")

fmt.Print("Options:", "\n")
fmt.Print(" -version", " ", "Shows current version of this binary", "\n")
Expand Down
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
var (
ScalrHostname string
ScalrToken string
ScalrAccount string
)

const (
Expand All @@ -41,7 +42,7 @@ func main() {
defer func() {
err := recover()
if err != nil {
fmt.Println("Error! " + err.(string))
fmt.Println(err)
os.Exit(1)
}
}()
Expand Down Expand Up @@ -100,10 +101,17 @@ func main() {
ScalrHostname = jsonParsed.Search("hostname").Data().(string)
ScalrToken = jsonParsed.Search("token").Data().(string)

if jsonParsed.Search("account") != nil {
ScalrAccount = jsonParsed.Search("account").Data().(string)
} else {
ScalrAccount = os.Getenv("SCALR_ACCOUNT")
}

} else {
//Read config from Environment
ScalrHostname = os.Getenv("SCALR_HOSTNAME")
ScalrToken = os.Getenv("SCALR_TOKEN")
ScalrAccount = os.Getenv("SCALR_ACCOUNT")
}

if *help {
Expand Down Expand Up @@ -145,7 +153,6 @@ func loadAPI() *openapi3.T {
downloadFile("https://"+ScalrHostname+"/api/iacp/v3/openapi-preview.yml", home+spec)
}

openapi3.SchemaFormatValidationDisabled = true
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true

Expand All @@ -172,7 +179,7 @@ func disableExternalFiles(reader openapi3.ReadFromURIFunc) openapi3.ReadFromURIF

//Skip examples
if strings.Contains(location.Path, "/examples/") {
return
return []byte("value: {}"), nil
}

return reader(loader, location)
Expand Down

0 comments on commit c1f275e

Please sign in to comment.