Skip to content

Commit

Permalink
✨ interactive template auth (#613)
Browse files Browse the repository at this point in the history
* feature: template_auth_rules - created a interactive CLI when there are no flags given while giving the command

Signed-off-by: Abinand P <[email protected]>

* fix: had a extra line which prints the values of the variables used for testing

* Update cyctl/internal/create/template_auth_rules.go

Co-authored-by: Petar Cvitanović <[email protected]>

* Update cyctl/internal/create/template_auth_rules.go

Co-authored-by: Petar Cvitanović <[email protected]>

* Update cyctl/internal/create/template_auth_rules.go

Co-authored-by: Petar Cvitanović <[email protected]>

* Update cyctl/internal/create/template_auth_rules.go

Co-authored-by: Petar Cvitanović <[email protected]>

* ✍️ formated the code

* fix: moved the variables to the createTemplateAuthRule function for localization

Signed-off-by: Abinand P <[email protected]>

* fix typo

---------

Signed-off-by: Abinand P <[email protected]>
Co-authored-by: Petar Cvitanović <[email protected]>
Co-authored-by: petar-cvit <[email protected]>
  • Loading branch information
3 people authored Nov 7, 2024
1 parent 1df8204 commit cfb9346
Showing 1 changed file with 91 additions and 4 deletions.
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

0 comments on commit cfb9346

Please sign in to comment.