Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactive template auth #613

Merged
merged 10 commits into from
Nov 7, 2024
95 changes: 91 additions & 4 deletions cyctl/internal/create/template_auth_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package create

import (
"encoding/json"
"errors"
"fmt"
"log"
"strings"

"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1"
"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1/client"
"github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
v1Spec "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -28,6 +30,74 @@ var (
password string
)

func getTemplateAuthRulesFromPrompt() (string, string, string, string, string, error) {
RepoPrompt := promptui.Prompt{
Label: "repo",
}
repoValue, err := RepoPrompt.Run()
if err != nil {
return "", "", "", "", "", err
}

usernameNamePrompt := promptui.Prompt{
Label: "Username secret name",
Validate: func(input string) error {
if input == "" {
return errors.New("username secret name cannot be empty")
}
return nil
},
}
usernameName, err := usernameNamePrompt.Run()
if err != nil {
return "", "", "", "", "", err
}

usernameKeyPrompt := promptui.Prompt{
Label: "Username secret key",
Validate: func(input string) error {
if input == "" {
return errors.New("the username secret key cannot be empty")
}
return nil
},
}
usernameKey, err := usernameKeyPrompt.Run()
if err != nil {
return "", "", "", "", "", err
}

passwordNamePrompt := promptui.Prompt{
Label: "Password secret name",
Validate: func(input string) error {
if input == "" {
return errors.New("the password secret name cannot be empty")
}
return nil
},
}
passwordName, err := passwordNamePrompt.Run()
if err != nil {
return "", "", "", "", "", err
}

passwordKeyPrompt := promptui.Prompt{
Label: "Password secret key",
Validate: func(input string) error {
if input == "" {
return errors.New("password secret key cannot be empty")
}
return nil
},
}
passwordKey, err := passwordKeyPrompt.Run()
if err != nil {
return "", "", "", "", "", err
}

return usernameName, usernameKey, passwordName, passwordKey, repoValue, nil
}

func validateSecretKeySelector(username, password string) (string, string, string, string, error) {
usernameName, usernameKey := splitNameKey(username)
passwordName, passwordKey := splitNameKey(password)
Expand All @@ -50,10 +120,27 @@ func splitNameKey(input string) (string, string) {

// createTemplateAuthRule allows you to create TemplateAuthRule Custom Resource.
func createTemplateAuthRule(clientset *client.CyclopsV1Alpha1Client, templateAuthRuleName string) {
usernameName, usernameKey, passwordName, passwordKey, err := validateSecretKeySelector(username, password)
if err != nil {
fmt.Println(err)
return
var (
usernameName string
passwordName string
usernameKey string
passwordKey string
)

if username == "" && password == "" && repo == "" {
var err error
usernameName, usernameKey, passwordName, passwordKey, repo, err = getTemplateAuthRulesFromPrompt()
if err != nil {
log.Fatal(err)
return
}
} else {
var err error
usernameName, usernameKey, passwordName, passwordKey, err = validateSecretKeySelector(username, password)
if err != nil {
log.Fatal(err)
return
}
}

var localObjectNameRef, localObjectPasswordRef v1Spec.LocalObjectReference
Expand Down
Loading