Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Add baseURL support
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewatters committed Apr 14, 2020
1 parent fe130fe commit 8a691e4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
28 changes: 24 additions & 4 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ package cmd

import (
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/mgutz/ansi"
"github.com/spf13/cobra"
"net/url"
"os"
)

// Configuration options bound via Cobra
Expand All @@ -41,21 +43,39 @@ var loginCmd = &cobra.Command{
Short: "Login to the Opsani API",
Long: `Login to the Opsani API and persist access credentials.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Logging into api.opsani.com")
u, err := url.Parse(baseURL)
if err != nil {
panic(err)
}
baseURLDescription := u.Hostname()
if port := u.Port(); port != "" && port != "80" && port != "443" {
baseURLDescription = baseURLDescription + ":" + port
}
fmt.Println("Logging into", baseURLDescription)

whiteBold := ansi.ColorCode("white+b")
if loginConfig.Username == "" {
survey.AskOne(&survey.Input{
err := survey.AskOne(&survey.Input{
Message: "Username:",
}, &loginConfig.Username, survey.WithValidator(survey.Required))
if err == terminal.InterruptErr {
os.Exit(0)
} else if err != nil {
panic(err)
}
} else {
fmt.Printf("%si %sUsername: %s%s%s%s\n", ansi.Blue, whiteBold, ansi.Reset, ansi.LightCyan, loginConfig.Username, ansi.Reset)
}

if loginConfig.Password == "" {
survey.AskOne(&survey.Password{
err := survey.AskOne(&survey.Password{
Message: "Password:",
}, &loginConfig.Password, survey.WithValidator(survey.Required))
if err == terminal.InterruptErr {
os.Exit(0)
} else if err != nil {
panic(err)
}
}
},
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/viper"
)

var baseURL string
var cfgFile string
var printVersion bool

Expand Down Expand Up @@ -71,6 +72,8 @@ func init() {
fmt.Println(err)
os.Exit(1)
}
rootCmd.PersistentFlags().StringVar(&baseURL, "base-url", "https://api.opsani.com/", "Base URL for accessing the Opsani API")
rootCmd.PersistentFlags().MarkHidden("base-url")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", fmt.Sprintf("Location of config file (default \"%s\")", filepath.Join(home, ".opsani", "config.yaml")))
rootCmd.PersistentFlags().BoolVarP(&printVersion, "version", "v", false, "Print version information and quit")
}
Expand Down

0 comments on commit 8a691e4

Please sign in to comment.