Skip to content

Commit f67a31b

Browse files
Interactive-cli (#138)
* get repo and runtime name from user - install and uninstall * added silent flag to Config * ensureRepo adjustments added ensureGitToken * small fix * small string change * ajdust to runtime upgrade * fromAPI true * - change silent flag location - add promptSummaryToUser * - add summary prompt to install and upgrade * prompt summary to git-source create * improve code duplicates * colors changes * runtime names selection * small fix * revert git-source commands * improve summary * bump version * lint error * comment out summary * bump version * comment out summary functions * comment in summary * bump version * move silent flag to runtime command * text styles to const convention * generated doc files set Silent to true in git-source commands
1 parent 2579aa5 commit f67a31b

32 files changed

+298
-64
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.130
1+
VERSION=v0.0.131
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

cmd/commands/common.go

Lines changed: 163 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import (
2424
"github.com/Masterminds/semver/v3"
2525
"github.com/argoproj-labs/argocd-autopilot/pkg/git"
2626
"github.com/codefresh-io/cli-v2/pkg/config"
27+
"github.com/codefresh-io/cli-v2/pkg/log"
2728
"github.com/codefresh-io/cli-v2/pkg/store"
2829
"github.com/codefresh-io/cli-v2/pkg/util"
30+
"github.com/manifoldco/promptui"
2931

3032
"github.com/spf13/cobra"
3133
"github.com/spf13/pflag"
@@ -40,6 +42,14 @@ var (
4042
ingressPatch []byte
4143

4244
cfConfig *config.Config
45+
46+
GREEN = "\033[32m"
47+
BLUE = "\033[34m"
48+
BOLD = "\033[1m"
49+
UNDERLINE = "\033[4m"
50+
COLOR_RESET = "\033[0m"
51+
UNDERLINE_RESET = "\033[24m"
52+
BOLD_RESET = "\033[22m"
4353
)
4454

4555
func postInitCommands(commands []*cobra.Command) {
@@ -64,21 +74,168 @@ func IsValid(s string) (bool, error) {
6474
return regexp.MatchString(`^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$`, s)
6575
}
6676

67-
func ensureRepo(cmd *cobra.Command, args []string, cloneOpts *git.CloneOptions) error {
77+
func ensureRepo(cmd *cobra.Command, runtimeName string, cloneOpts *git.CloneOptions, fromAPI bool) error {
6878
ctx := cmd.Context()
6979
if cloneOpts.Repo == "" {
70-
runtimeData, err := cfConfig.NewClient().V2().Runtime().Get(ctx, args[0])
80+
if fromAPI {
81+
runtimeData, err := cfConfig.NewClient().V2().Runtime().Get(ctx, runtimeName)
82+
if err != nil {
83+
return fmt.Errorf("failed getting runtime repo information: %w", err)
84+
}
85+
if runtimeData.Repo != nil {
86+
cloneOpts.Repo = *runtimeData.Repo
87+
die(cmd.Flags().Set("repo", *runtimeData.Repo))
88+
return nil
89+
}
90+
}
91+
if !store.Get().Silent {
92+
return getRepoFromUserInput(cmd, cloneOpts)
93+
}
94+
}
95+
return nil
96+
}
97+
98+
func getRepoFromUserInput(cmd *cobra.Command, cloneOpts *git.CloneOptions) error {
99+
repoPrompt := promptui.Prompt{
100+
Label: "Repository URL",
101+
}
102+
repoInput, err := repoPrompt.Run()
103+
if err != nil {
104+
return fmt.Errorf("Prompt error: %w", err)
105+
}
106+
cloneOpts.Repo = repoInput
107+
die(cmd.Flags().Set("repo", repoInput))
108+
return nil
109+
}
110+
111+
func ensureRuntimeName(ctx context.Context, args []string, runtimeName *string) error {
112+
if len(args) > 0 {
113+
*runtimeName = args[0]
114+
}
115+
116+
if *runtimeName == "" {
117+
if !store.Get().Silent {
118+
return getRuntimeNameFromUserSelect(ctx, runtimeName)
119+
}
120+
log.G(ctx).Fatal("must enter a runtime name")
121+
}
122+
123+
return nil
124+
}
125+
126+
func getRuntimeNameFromUserInput(runtimeName *string) error {
127+
runtimeNamePrompt := promptui.Prompt{
128+
Label: "Runtime name",
129+
Default: "codefresh",
130+
}
131+
runtimeNameInput, err := runtimeNamePrompt.Run()
132+
if err != nil {
133+
return fmt.Errorf("Prompt error: %w", err)
134+
}
135+
*runtimeName = runtimeNameInput
136+
return nil
137+
}
138+
139+
func getRuntimeNameFromUserSelect(ctx context.Context, runtimeName *string) error {
140+
if !store.Get().Silent {
141+
runtimes, err := cfConfig.NewClient().V2().Runtime().List(ctx)
71142
if err != nil {
72-
return fmt.Errorf("failed getting runtime repo information: %w", err)
143+
return err
144+
}
145+
146+
if len(runtimes) == 0 {
147+
return fmt.Errorf("No runtimes were found")
148+
}
149+
150+
runtimeNames := make([]string, len(runtimes))
151+
152+
for index, rt := range runtimes {
153+
runtimeNames[index] = rt.Metadata.Name
73154
}
74-
if runtimeData.Repo != nil {
75-
cloneOpts.Repo = *runtimeData.Repo
76-
die(cmd.Flags().Set("repo", *runtimeData.Repo))
155+
156+
templates := &promptui.SelectTemplates{
157+
Selected: "{{ . | yellow }} ",
77158
}
159+
160+
prompt := promptui.Select{
161+
Label: "\033[34mSelect runtime\033[0m",
162+
Items: runtimeNames,
163+
Templates: templates,
164+
}
165+
166+
_, result, err := prompt.Run()
167+
if err != nil {
168+
return fmt.Errorf("Prompt error: %w", err)
169+
}
170+
171+
*runtimeName = result
172+
}
173+
return nil
174+
}
175+
176+
func ensureGitToken(cmd *cobra.Command, cloneOpts *git.CloneOptions) error {
177+
if cloneOpts.Auth.Password == "" && !store.Get().Silent {
178+
return getGitTokenFromUserInput(cmd, cloneOpts)
78179
}
79180
return nil
80181
}
81182

183+
func getGitTokenFromUserInput(cmd *cobra.Command, cloneOpts *git.CloneOptions) error {
184+
gitTokenPrompt := promptui.Prompt{
185+
Label: "Git provider api token",
186+
}
187+
gitTokenInput, err := gitTokenPrompt.Run()
188+
if err != nil {
189+
return fmt.Errorf("Prompt error: %w", err)
190+
}
191+
cloneOpts.Auth.Password = gitTokenInput
192+
die(cmd.Flags().Set("git-token", gitTokenInput))
193+
return nil
194+
}
195+
196+
func getApprovalFromUser(ctx context.Context, finalParameters map[string]string, description string) (bool, error) {
197+
if !store.Get().Silent {
198+
isApproved, err := promptSummaryToUser(ctx, finalParameters, description)
199+
if err != nil {
200+
return false, fmt.Errorf("%w", err)
201+
}
202+
203+
if !isApproved {
204+
log.G(ctx).Printf("%v command was cancelled by user", description)
205+
return false, nil
206+
}
207+
}
208+
return true, nil
209+
}
210+
211+
func promptSummaryToUser(ctx context.Context, finalParameters map[string]string, description string) (bool, error) {
212+
templates := &promptui.SelectTemplates{
213+
Selected: "{{ . | yellow }} ",
214+
}
215+
promptStr := fmt.Sprintf("%v%v%vSummary%v%v%v", GREEN, BOLD, UNDERLINE, COLOR_RESET, BOLD_RESET, UNDERLINE_RESET)
216+
labelStr := fmt.Sprintf("%vDo you wish to continue to %v ?%v", BLUE, description, COLOR_RESET)
217+
218+
for key, value := range finalParameters {
219+
promptStr += fmt.Sprintf("\n%v%v: %v%v", GREEN, key, COLOR_RESET, value)
220+
}
221+
log.G(ctx).Printf(promptStr)
222+
prompt := promptui.Select{
223+
Label: labelStr,
224+
Items: []string{"Yes", "No"},
225+
Templates: templates,
226+
}
227+
228+
_, result, err := prompt.Run()
229+
if err != nil {
230+
return false, fmt.Errorf("Prompt error: %w", err)
231+
}
232+
233+
if result == "Yes" {
234+
return true, nil
235+
}
236+
return false, nil
237+
}
238+
82239
func verifyLatestVersion(ctx context.Context) error {
83240
latestVersionString, err := cfConfig.NewClient().V2().CliReleases().GetLatest(ctx)
84241
if err != nil {

cmd/commands/git-source.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func NewGitSourceCreateCommand() *cobra.Command {
121121
`),
122122
PreRunE: func(cmd *cobra.Command, args []string) error {
123123
ctx := cmd.Context()
124+
store.Get().Silent = true
124125

125126
if len(args) < 1 {
126127
log.G(ctx).Fatal("must enter runtime name")
@@ -133,8 +134,8 @@ func NewGitSourceCreateCommand() *cobra.Command {
133134
if gsCloneOpts.Repo == "" {
134135
log.G(ctx).Fatal("must enter a valid value to --git-src-repo. Example: https://github.com/owner/repo-name/path/to/workflow")
135136
}
136-
137-
err := ensureRepo(cmd, args, insCloneOpts)
137+
138+
err := ensureRepo(cmd, args[0], insCloneOpts, true)
138139
if err != nil {
139140
return err
140141
}
@@ -458,6 +459,7 @@ func NewGitSourceDeleteCommand() *cobra.Command {
458459
`),
459460
PreRunE: func(cmd *cobra.Command, args []string) error {
460461
ctx := cmd.Context()
462+
store.Get().Silent = true
461463

462464
if len(args) < 1 {
463465
log.G(ctx).Fatal("must enter runtime name")
@@ -467,7 +469,7 @@ func NewGitSourceDeleteCommand() *cobra.Command {
467469
log.G(ctx).Fatal("must enter git-source name")
468470
}
469471

470-
err := ensureRepo(cmd, args, insCloneOpts)
472+
err := ensureRepo(cmd, args[0], insCloneOpts, true)
471473
if err != nil {
472474
return err
473475
}
@@ -527,6 +529,7 @@ func NewGitSourceEditCommand() *cobra.Command {
527529
`),
528530
PreRunE: func(cmd *cobra.Command, args []string) error {
529531
ctx := cmd.Context()
532+
store.Get().Silent = true
530533

531534
if len(args) < 1 {
532535
log.G(ctx).Fatal("must enter a runtime name")
@@ -539,8 +542,8 @@ func NewGitSourceEditCommand() *cobra.Command {
539542
if gsCloneOpts.Repo == "" {
540543
log.G(ctx).Fatal("must enter a valid value to --git-src-repo. Example: https://github.com/owner/repo-name/path/to/workflow")
541544
}
542-
543-
err := ensureRepo(cmd, args, insCloneOpts)
545+
546+
err := ensureRepo(cmd, args[0], insCloneOpts, true)
544547
if err != nil {
545548
return err
546549
}

cmd/commands/root.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
func NewRoot() *cobra.Command {
2626
s := store.Get()
27-
var silent bool
2827

2928
cmd := &cobra.Command{
3029
Use: s.BinaryName,
@@ -48,8 +47,7 @@ variables in advanced to simplify the use of those commands.
4847
}
4948

5049
cfConfig = config.AddFlags(cmd.PersistentFlags())
51-
cmd.PersistentFlags().BoolVar(&silent, "silent", false, "Disables the command wizard")
52-
50+
5351
cmd.AddCommand(NewVersionCommand())
5452
cmd.AddCommand(NewConfigCommand())
5553
cmd.AddCommand(NewRuntimeCommand())

0 commit comments

Comments
 (0)