Skip to content

Commit 6b90528

Browse files
[BE]:Handle validation check (#411)
* Handle validation check * Fix * Fix * Update * Refactor * Refactor * Fix bug related with silent mode * Up version * Lint
1 parent 0b6c556 commit 6b90528

File tree

5 files changed

+62
-37
lines changed

5 files changed

+62
-37
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.363
1+
VERSION=v0.0.364
22

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

cmd/commands/common.go

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,27 @@ func getRuntimeNameFromUserSelect(ctx context.Context) (string, error) {
215215
}
216216

217217
func getRuntimeNameFromUserInput() (string, error) {
218-
return getValueFromUserInput("Runtime name", "codefresh")
218+
runtimeName, err := getValueFromUserInput("Runtime name", "codefresh", validateRuntimeName)
219+
return runtimeName, err
219220
}
220221

221-
func getValueFromUserInput(label, defaultValue string) (string, error) {
222-
prompt := promptui.Prompt{
223-
Label: label,
224-
Default: defaultValue,
225-
// Validate: func (value string) error {
226-
// if value == "" {
227-
// return fmt.Errorf("Must supply value for \"%s\"", label)
228-
// }
222+
func validateRuntimeName(runtime string) error {
223+
isValid, err := IsValidName(runtime)
224+
if err != nil {
225+
return fmt.Errorf("failed to validate runtime name: %w", err)
226+
}
227+
if !isValid {
228+
return fmt.Errorf("runtime name cannot have any uppercase letters, must start with a character, end with character or number, and be shorter than 63 chars")
229+
}
230+
return nil
231+
}
229232

230-
// return nil
231-
// },
232-
Pointer: promptui.PipeCursor,
233+
func getValueFromUserInput(label, defaultValue string, validate promptui.ValidateFunc) (string, error) {
234+
prompt := promptui.Prompt{
235+
Label: label,
236+
Default: defaultValue,
237+
Validate: validate,
238+
Pointer: promptui.PipeCursor,
233239
}
234240

235241
return prompt.Run()
@@ -270,8 +276,10 @@ func inferProviderFromRepo(opts *git.CloneOptions) {
270276
}
271277

272278
func ensureGitToken(cmd *cobra.Command, cloneOpts *git.CloneOptions, verify bool) error {
279+
errMessage := "Value from environment variable TOKEN is not valid, enter another value"
273280
if cloneOpts.Auth.Password == "" && !store.Get().Silent {
274281
err := getGitTokenFromUserInput(cmd)
282+
errMessage = "Entered git token is not valid, enter another value please"
275283
if err != nil {
276284
return err
277285
}
@@ -280,10 +288,11 @@ func ensureGitToken(cmd *cobra.Command, cloneOpts *git.CloneOptions, verify bool
280288
if verify {
281289
err := cfgit.VerifyToken(cmd.Context(), cloneOpts.Provider, cloneOpts.Auth.Password, cfgit.RuntimeToken)
282290
if err != nil {
283-
return fmt.Errorf("failed to verify git token: %w", err)
291+
// in case when we get invalid value from env variable TOKEN we clean
292+
cloneOpts.Auth.Password = ""
293+
return fmt.Errorf(errMessage)
284294
}
285295
}
286-
287296
return nil
288297
}
289298

@@ -485,12 +494,7 @@ func setIngressHost(ctx context.Context, opts *RuntimeInstallOptions) error {
485494
}
486495

487496
func getIngressHostFromUserInput(foundIngressHost string) (string, error) {
488-
ingressHostInput, err := getValueFromUserInput("Ingress host", foundIngressHost)
489-
if err != nil {
490-
return "", err
491-
}
492-
493-
err = validateIngressHost(ingressHostInput)
497+
ingressHostInput, err := getValueFromUserInput("Ingress host", foundIngressHost, validateIngressHost)
494498
if err != nil {
495499
return "", err
496500
}
@@ -592,6 +596,22 @@ func askUserIfToProceedWithInsecure(ctx context.Context) error {
592596
return nil
593597
}
594598

599+
type Callback func() error
600+
601+
func handleValidationFailsWithRepeat(callback Callback) {
602+
var err error
603+
for {
604+
err = callback()
605+
if !isValidationError(err) {
606+
break
607+
}
608+
}
609+
}
610+
611+
func isValidationError(err error) bool {
612+
return err != nil && err != promptui.ErrInterrupt
613+
}
614+
595615
func setIscRepo(ctx context.Context, suggestedSharedConfigRepo string) (string, error) {
596616
setIscRepoResponse, err := cfConfig.NewClient().V2().Runtime().SetSharedConfigRepo(ctx, suggestedSharedConfigRepo)
597617
if err != nil {

cmd/commands/runtime.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func runtimeInstallCommandPreRunHandler(cmd *cobra.Command, opts *RuntimeInstall
346346

347347
inferProviderFromRepo(opts.InsCloneOpts)
348348

349-
err = ensureGitToken(cmd, opts.InsCloneOpts, true)
349+
err = getGitToken(cmd, opts)
350350
handleCliStep(reporter.InstallStepPreCheckEnsureGitToken, "Getting git token", err, true, false)
351351
if err != nil {
352352
return err
@@ -393,6 +393,23 @@ func runtimeInstallCommandPreRunHandler(cmd *cobra.Command, opts *RuntimeInstall
393393
return nil
394394
}
395395

396+
func getGitToken(cmd *cobra.Command, opts *RuntimeInstallOptions) error {
397+
var err error
398+
if store.Get().Silent {
399+
err = ensureGitToken(cmd, opts.InsCloneOpts, true)
400+
} else {
401+
handleValidationFailsWithRepeat(func() error {
402+
err = ensureGitToken(cmd, opts.InsCloneOpts, true)
403+
if isValidationError(err) && !store.Get().Silent {
404+
fmt.Println(err)
405+
return err
406+
}
407+
return nil
408+
})
409+
}
410+
return err
411+
}
412+
396413
func runtimeUninstallCommandPreRunHandler(cmd *cobra.Command, args []string, opts *RuntimeUninstallOptions) error {
397414
var err error
398415
handleCliStep(reporter.UninstallPhasePreCheckStart, "Starting pre checks", nil, true, false)
@@ -2632,18 +2649,6 @@ func createAnalyticsReporter(ctx context.Context, flow reporter.FlowType, disabl
26322649
reporter.Init(user, flow)
26332650
}
26342651

2635-
func validateRuntimeName(runtime string) error {
2636-
var err error
2637-
isValid, err := IsValidName(runtime)
2638-
if err != nil {
2639-
err = fmt.Errorf("failed to check the validity of the runtime name: %w", err)
2640-
} else if !isValid {
2641-
err = fmt.Errorf("runtime name cannot have any uppercase letters, must start with a character, end with character or number, and be shorter than 63 chars")
2642-
}
2643-
2644-
return err
2645-
}
2646-
26472652
func getVersionIfExists(versionStr string) (*semver.Version, error) {
26482653
if versionStr != "" {
26492654
log.G().Infof("vesionStr: %s", versionStr)

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

2424
```bash
2525
# download and extract the binary
26-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.363/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.364/cf-linux-amd64.tar.gz | tar zx
2727

2828
# move the binary to your $PATH
2929
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -36,7 +36,7 @@ cf version
3636

3737
```bash
3838
# download and extract the binary
39-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.363/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.364/cf-darwin-amd64.tar.gz | tar zx
4040

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: "{{ namespace }}"
66
spec:
77
defVersion: 1.0.1
8-
version: 0.0.363
8+
version: 0.0.364
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

0 commit comments

Comments
 (0)