Skip to content

Commit dcae805

Browse files
[BE]:Add runtime install validation (#414)
* Handle validation check * Fix * Fix * Update * Refactor * Refactor * Fix bug related with silent mode * Up version * Lint * Add ingress validation * Add ingress validation * Add repository validation * Up version * Valid ingressHost protocol * Valid ingressHost protocol * Update message
1 parent 52c43b9 commit dcae805

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
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.365
1+
VERSION=v0.0.366
22

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

cmd/commands/common.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/codefresh-io/cli-v2/pkg/util"
3535

3636
"github.com/argoproj-labs/argocd-autopilot/pkg/git"
37+
autoPilotUtil "github.com/argoproj-labs/argocd-autopilot/pkg/util"
3738
"github.com/manifoldco/promptui"
3839
"github.com/spf13/cobra"
3940
"github.com/spf13/pflag"
@@ -83,7 +84,11 @@ func IsValidName(s string) (bool, error) {
8384
}
8485

8586
func isValidIngressHost(ingressHost string) (bool, error) {
86-
return regexp.MatchString(`^(http|https)://`, ingressHost)
87+
pattern := `^https://`
88+
if store.Get().InsecureIngressHost {
89+
pattern = `^(http|https)://`
90+
}
91+
return regexp.MatchString(pattern, ingressHost)
8792
}
8893

8994
func askUserIfToInstallDemoResources(cmd *cobra.Command, sampleInstall *bool) error {
@@ -149,6 +154,13 @@ func ensureRepo(cmd *cobra.Command, runtimeName string, cloneOpts *git.CloneOpti
149154
func getRepoFromUserInput(cmd *cobra.Command) error {
150155
repoPrompt := promptui.Prompt{
151156
Label: "Repository URL",
157+
Validate: func(value string) error {
158+
host, orgRepo, _, _, _, _, _ := autoPilotUtil.ParseGitUrl(value)
159+
if host != "" && orgRepo != "" {
160+
return nil
161+
}
162+
return fmt.Errorf("Invalid URL for Git repository")
163+
},
152164
}
153165
repoInput, err := repoPrompt.Run()
154166
if err != nil {
@@ -225,7 +237,7 @@ func validateRuntimeName(runtime string) error {
225237
return fmt.Errorf("failed to validate runtime name: %w", err)
226238
}
227239
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")
240+
return fmt.Errorf("Runtime name must start with a lower-case character, and can include up to 62 lower-case characters and numbers")
229241
}
230242
return nil
231243
}
@@ -276,10 +288,10 @@ func inferProviderFromRepo(opts *git.CloneOptions) {
276288
}
277289

278290
func ensureGitToken(cmd *cobra.Command, cloneOpts *git.CloneOptions, verify bool) error {
279-
errMessage := "Value from environment variable TOKEN is not valid, enter another value"
291+
errMessage := "Value stored in environment variable TOKEN is invalid; enter a valid runtime token"
280292
if cloneOpts.Auth.Password == "" && !store.Get().Silent {
281293
err := getGitTokenFromUserInput(cmd)
282-
errMessage = "Entered git token is not valid, enter another value please"
294+
errMessage = "Invalid runtime token; enter a valid token"
283295
if err != nil {
284296
return err
285297
}
@@ -440,7 +452,7 @@ func validateIngressHost(ingressHost string) error {
440452
if err != nil {
441453
err = fmt.Errorf("could not verify ingress host: %w", err)
442454
} else if !isValid {
443-
err = fmt.Errorf("ingress host must begin with protocol 'http://' or 'https://'")
455+
err = fmt.Errorf("Ingress host must begin with a protocol, either http:// or https://")
444456
}
445457

446458
return err
@@ -484,6 +496,11 @@ func setIngressHost(ctx context.Context, opts *RuntimeInstallOptions) error {
484496
if err != nil {
485497
return err
486498
}
499+
_, err := http.Get(opts.IngressHost)
500+
if err != nil {
501+
opts.IngressHost = ""
502+
return err
503+
}
487504
}
488505

489506
if opts.IngressHost == "" {

cmd/commands/runtime.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func runtimeInstallCommandPreRunHandler(cmd *cobra.Command, opts *RuntimeInstall
332332
return err
333333
}
334334

335-
err = ensureIngressHost(cmd, opts)
335+
err = getIngressHost(cmd, opts)
336336
handleCliStep(reporter.InstallStepPreCheckEnsureIngressHost, "Getting ingressHost", err, true, false)
337337
if err != nil {
338338
return err
@@ -393,14 +393,31 @@ func runtimeInstallCommandPreRunHandler(cmd *cobra.Command, opts *RuntimeInstall
393393
return nil
394394
}
395395

396+
func getIngressHost(cmd *cobra.Command, opts *RuntimeInstallOptions) error {
397+
var err error
398+
if store.Get().Silent {
399+
err = ensureIngressHost(cmd, opts)
400+
} else {
401+
handleValidationFailsWithRepeat(func() error {
402+
err = ensureIngressHost(cmd, opts)
403+
if isValidationError(err) {
404+
fmt.Println("Could not resolve the URL for ingress host; enter a valid URL")
405+
return err
406+
}
407+
return nil
408+
})
409+
}
410+
return err
411+
}
412+
396413
func getGitToken(cmd *cobra.Command, opts *RuntimeInstallOptions) error {
397414
var err error
398415
if store.Get().Silent {
399416
err = ensureGitToken(cmd, opts.InsCloneOpts, true)
400417
} else {
401418
handleValidationFailsWithRepeat(func() error {
402419
err = ensureGitToken(cmd, opts.InsCloneOpts, true)
403-
if isValidationError(err) && !store.Get().Silent {
420+
if isValidationError(err) {
404421
fmt.Println(err)
405422
return err
406423
}

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.365/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.366/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.365/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.366/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.365
8+
version: 0.0.366
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

0 commit comments

Comments
 (0)