Files
sailpoint-cli/cmd/set/auth.go
2023-02-24 10:02:30 -06:00

79 lines
1.8 KiB
Go

package set
import (
"fmt"
"strings"
"github.com/fatih/color"
"github.com/sailpoint-oss/sailpoint-cli/internal/config"
"github.com/sailpoint-oss/sailpoint-cli/internal/tui"
"github.com/spf13/cobra"
)
func PromptAuth() (string, error) {
items := []tui.Choice{
{Title: "PAT", Description: "Person Access Token - Single User"},
// {Title: "OAuth", Description: "OAuth2.0 Authentication - Sign in via the Web Portal"},
}
choice, err := tui.PromptList(items, "Choose an authentication method to configure")
if err != nil {
return "", err
}
return strings.ToLower(choice.Title), nil
}
func newAuthCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "auth",
Short: "change currently active authentication mode",
Long: "Change Auth Mode Configured (pat, pipeline).",
Example: "sail auth pat | sail auth pat | sail auth pipeline",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var selection string
var err error
err = config.InitConfig()
if err != nil {
return err
}
if len(args) > 0 {
selection = args[0]
} else {
selection, err = PromptAuth()
if err != nil {
return err
}
}
switch strings.ToLower(selection) {
case "pat":
if config.GetAuthType() != "pat" {
config.SetAuthType("pat")
color.Blue("authentication method set to pat")
}
case "oauth":
return fmt.Errorf("oauth not currently supported")
// if config.GetAuthType() != "oauth" {
// config.SetAuthType("oauth")
// color.Blue("authentication method set to oauth")
// }
case "pipeline":
if config.GetAuthType() != "pipeline" {
config.SetAuthType("pipeline")
color.Blue("authentication method set to pipeline")
}
default:
return fmt.Errorf("invalid selection")
}
return nil
},
}
return cmd
}