Skip to content

Commit

Permalink
Fixes non-interactive mode in set-account command
Browse files Browse the repository at this point in the history
Signed-off-by: nagesh bansal <[email protected]>
  • Loading branch information
Nageshbansal committed Dec 12, 2023
1 parent 2999756 commit 96a158c
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions pkg/cmd/config/setAccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,43 @@ var setAccountCmd = &cobra.Command{
err error
)

// prompts for account details
promptEndpoint := promptui.Prompt{
Label: "Host endpoint where litmus is installed",
}
authInput.Endpoint, err = promptEndpoint.Run()
nonInteractive, err := cmd.Flags().GetBool("non-interactive")
utils.PrintError(err)

if nonInteractive {
authInput.Endpoint, err = cmd.Flags().GetString("endpoint")
utils.PrintError(err)

authInput.Username, err = cmd.Flags().GetString("username")
utils.PrintError(err)

authInput.Password, err = cmd.Flags().GetString("password")
utils.PrintError(err)

} else {
// prompts for account details
promptEndpoint := promptui.Prompt{
Label: "Host endpoint where litmus is installed",
}
authInput.Endpoint, err = promptEndpoint.Run()
utils.PrintError(err)

promptUsername := promptui.Prompt{
Label: "Username [Default: " + utils.DefaultUsername + "]",
Default: utils.DefaultUsername,
}
authInput.Username, err = promptUsername.Run()
utils.PrintError(err)

promptPassword := promptui.Prompt{
Label: "Password",
Mask: '*',
}
pass, err := promptPassword.Run()
utils.PrintError(err)
authInput.Password = pass
}

// Validate and format the endpoint URL
ep := strings.TrimRight(authInput.Endpoint, "/")
newURL, err := url.Parse(ep)
Expand All @@ -78,21 +108,6 @@ var setAccountCmd = &cobra.Command{
authInput.Endpoint = newUrl.String()
}

promptUsername := promptui.Prompt{
Label: "Username [Default: " + utils.DefaultUsername + "]",
Default: utils.DefaultUsername,
}
authInput.Username, err = promptUsername.Run()
utils.PrintError(err)

promptPassword := promptui.Prompt{
Label: "Password",
Mask: '*',
}
pass, err := promptPassword.Run()
utils.PrintError(err)
authInput.Password = pass

if authInput.Endpoint != "" && authInput.Username != "" && authInput.Password != "" {
exists := config.FileExists(configFilePath)
var lgt int
Expand Down Expand Up @@ -192,7 +207,7 @@ var setAccountCmd = &cobra.Command{

func init() {
ConfigCmd.AddCommand(setAccountCmd)

setAccountCmd.Flags().BoolP("non-interactive", "n", false, "Set it to true for non interactive mode | Note: Always set the boolean flag as --non-interactive=Boolean")
setAccountCmd.Flags().StringP("endpoint", "e", "", "Account endpoint. Mandatory")
setAccountCmd.Flags().StringP("username", "u", "", "Account username. Mandatory")
setAccountCmd.Flags().StringP("password", "p", "", "Account password. Mandatory")
Expand Down

0 comments on commit 96a158c

Please sign in to comment.