@@ -24,8 +24,10 @@ import (
24
24
"github.com/Masterminds/semver/v3"
25
25
"github.com/argoproj-labs/argocd-autopilot/pkg/git"
26
26
"github.com/codefresh-io/cli-v2/pkg/config"
27
+ "github.com/codefresh-io/cli-v2/pkg/log"
27
28
"github.com/codefresh-io/cli-v2/pkg/store"
28
29
"github.com/codefresh-io/cli-v2/pkg/util"
30
+ "github.com/manifoldco/promptui"
29
31
30
32
"github.com/spf13/cobra"
31
33
"github.com/spf13/pflag"
40
42
ingressPatch []byte
41
43
42
44
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"
43
53
)
44
54
45
55
func postInitCommands (commands []* cobra.Command ) {
@@ -64,21 +74,168 @@ func IsValid(s string) (bool, error) {
64
74
return regexp .MatchString (`^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$` , s )
65
75
}
66
76
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 {
68
78
ctx := cmd .Context ()
69
79
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 )
71
142
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
73
154
}
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 }} " ,
77
158
}
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 )
78
179
}
79
180
return nil
80
181
}
81
182
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
+
82
239
func verifyLatestVersion (ctx context.Context ) error {
83
240
latestVersionString , err := cfConfig .NewClient ().V2 ().CliReleases ().GetLatest (ctx )
84
241
if err != nil {
0 commit comments