diff --git a/main.go b/main.go index 7e68c4a..35478f1 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ package main import ( + "context" "fmt" "io/ioutil" "os" @@ -121,6 +122,19 @@ func readJSON(file string) (string, error) { return "", nil } +// Attempts to read JSON credentials from default locations if unset. +// This is redundant as sgauth performs this resolution automatically, but +// necessary for proper caching in case the default location changes. +func resolveDefaultJSON(settings *sgauth.Settings) { + if settings.CredentialsJSON != "" { + return + } + credentials, err := sgauth.FindJSONCredentials(context.Background(), settings) + if err == nil { + settings.CredentialsJSON = string(credentials.JSON) + } +} + // Default 3LO authorization handler. Prints the authorization URL on stdout // and reads the verification code from stdin. func defaultAuthorizeFlowHandler(authorizeUrl string) (string, error) { @@ -291,6 +305,7 @@ func main() { CredentialsJSON: json, Audience: audience, } + resolveDefaultJSON(settings) taskArgs := getTaskArgs(cmd, curlcli, url, format, remainingArgs...) task(settings, taskArgs...) @@ -353,6 +368,7 @@ func main() { OAuthFlowHandler: defaultAuthorizeFlowHandler, State: "state", } + resolveDefaultJSON(settings) taskArgs := getTaskArgs(cmd, curlcli, url, format, remainingArgs...) task(settings, taskArgs...)