Skip to content

Commit cfb9346

Browse files
✨ interactive template auth (#613)
* 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]>
1 parent 1df8204 commit cfb9346

File tree

1 file changed

+91
-4
lines changed

1 file changed

+91
-4
lines changed

cyctl/internal/create/template_auth_rules.go

+91-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package create
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"log"
78
"strings"
89

910
"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1"
1011
"github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1/client"
1112
"github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig"
13+
"github.com/manifoldco/promptui"
1214
"github.com/spf13/cobra"
1315
v1Spec "k8s.io/api/core/v1"
1416
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -28,6 +30,74 @@ var (
2830
password string
2931
)
3032

33+
func getTemplateAuthRulesFromPrompt() (string, string, string, string, string, error) {
34+
RepoPrompt := promptui.Prompt{
35+
Label: "repo",
36+
}
37+
repoValue, err := RepoPrompt.Run()
38+
if err != nil {
39+
return "", "", "", "", "", err
40+
}
41+
42+
usernameNamePrompt := promptui.Prompt{
43+
Label: "Username secret name",
44+
Validate: func(input string) error {
45+
if input == "" {
46+
return errors.New("username secret name cannot be empty")
47+
}
48+
return nil
49+
},
50+
}
51+
usernameName, err := usernameNamePrompt.Run()
52+
if err != nil {
53+
return "", "", "", "", "", err
54+
}
55+
56+
usernameKeyPrompt := promptui.Prompt{
57+
Label: "Username secret key",
58+
Validate: func(input string) error {
59+
if input == "" {
60+
return errors.New("the username secret key cannot be empty")
61+
}
62+
return nil
63+
},
64+
}
65+
usernameKey, err := usernameKeyPrompt.Run()
66+
if err != nil {
67+
return "", "", "", "", "", err
68+
}
69+
70+
passwordNamePrompt := promptui.Prompt{
71+
Label: "Password secret name",
72+
Validate: func(input string) error {
73+
if input == "" {
74+
return errors.New("the password secret name cannot be empty")
75+
}
76+
return nil
77+
},
78+
}
79+
passwordName, err := passwordNamePrompt.Run()
80+
if err != nil {
81+
return "", "", "", "", "", err
82+
}
83+
84+
passwordKeyPrompt := promptui.Prompt{
85+
Label: "Password secret key",
86+
Validate: func(input string) error {
87+
if input == "" {
88+
return errors.New("password secret key cannot be empty")
89+
}
90+
return nil
91+
},
92+
}
93+
passwordKey, err := passwordKeyPrompt.Run()
94+
if err != nil {
95+
return "", "", "", "", "", err
96+
}
97+
98+
return usernameName, usernameKey, passwordName, passwordKey, repoValue, nil
99+
}
100+
31101
func validateSecretKeySelector(username, password string) (string, string, string, string, error) {
32102
usernameName, usernameKey := splitNameKey(username)
33103
passwordName, passwordKey := splitNameKey(password)
@@ -50,10 +120,27 @@ func splitNameKey(input string) (string, string) {
50120

51121
// createTemplateAuthRule allows you to create TemplateAuthRule Custom Resource.
52122
func createTemplateAuthRule(clientset *client.CyclopsV1Alpha1Client, templateAuthRuleName string) {
53-
usernameName, usernameKey, passwordName, passwordKey, err := validateSecretKeySelector(username, password)
54-
if err != nil {
55-
fmt.Println(err)
56-
return
123+
var (
124+
usernameName string
125+
passwordName string
126+
usernameKey string
127+
passwordKey string
128+
)
129+
130+
if username == "" && password == "" && repo == "" {
131+
var err error
132+
usernameName, usernameKey, passwordName, passwordKey, repo, err = getTemplateAuthRulesFromPrompt()
133+
if err != nil {
134+
log.Fatal(err)
135+
return
136+
}
137+
} else {
138+
var err error
139+
usernameName, usernameKey, passwordName, passwordKey, err = validateSecretKeySelector(username, password)
140+
if err != nil {
141+
log.Fatal(err)
142+
return
143+
}
57144
}
58145

59146
var localObjectNameRef, localObjectPasswordRef v1Spec.LocalObjectReference

0 commit comments

Comments
 (0)