From 047b51a83377bb82abd9253e8289177ef9c03190 Mon Sep 17 00:00:00 2001 From: Viktor Nagy Date: Fri, 5 Jul 2024 18:46:55 +0200 Subject: [PATCH] Refactored visibility validation; extended description; added default value; updated license date --- cmd/flux/bootstrap_gitlab.go | 8 +++++- internal/flags/gitlab_visibility.go | 35 +++++++++++++++--------- internal/flags/gitlab_visibility_test.go | 2 +- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/cmd/flux/bootstrap_gitlab.go b/cmd/flux/bootstrap_gitlab.go index a11f3d40db..11919e2d3e 100644 --- a/cmd/flux/bootstrap_gitlab.go +++ b/cmd/flux/bootstrap_gitlab.go @@ -96,7 +96,13 @@ type gitlabFlags struct { deployTokenAuth bool } -var gitlabArgs gitlabFlags +func NewGitlabFlags() gitlabFlags { + return gitlabFlags{ + visibility: flags.GitLabVisibility(gitprovider.RepositoryVisibilityPrivate), + } +} + +var gitlabArgs = NewGitlabFlags() func init() { bootstrapGitLabCmd.Flags().StringVar(&gitlabArgs.owner, "owner", "", "GitLab user or group name") diff --git a/internal/flags/gitlab_visibility.go b/internal/flags/gitlab_visibility.go index 91bc328f3a..6195b14f00 100644 --- a/internal/flags/gitlab_visibility.go +++ b/internal/flags/gitlab_visibility.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Flux authors +Copyright 2024 The Flux authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,17 +20,27 @@ import ( "fmt" "strings" - "github.com/fluxcd/flux2/v2/internal/utils" "github.com/fluxcd/go-git-providers/gitprovider" + "github.com/fluxcd/go-git-providers/validation" ) -var supportedGitLabVisibilities = []string{ - string(gitprovider.RepositoryVisibilityPublic), - string(gitprovider.RepositoryVisibilityInternal), - string(gitprovider.RepositoryVisibilityPrivate), +var supportedGitLabVisibilities = map[gitprovider.RepositoryVisibility]struct{}{ + gitprovider.RepositoryVisibilityPublic: {}, + gitprovider.RepositoryVisibilityInternal: {}, + gitprovider.RepositoryVisibilityPrivate: {}, } -type GitLabVisibility string +// ValidateRepositoryVisibility validates a given RepositoryVisibility. +// Use as errs.Append(ValidateRepositoryVisibility(visibility), visibility, "FieldName"). +func ValidateRepositoryVisibility(r gitprovider.RepositoryVisibility) error { + _, ok := supportedGitLabVisibilities[r] + if !ok { + return validation.ErrFieldEnumInvalid + } + return nil +} + +type GitLabVisibility gitprovider.RepositoryVisibility func (d *GitLabVisibility) String() string { return string(*d) @@ -40,12 +50,11 @@ func (d *GitLabVisibility) Set(str string) error { if strings.TrimSpace(str) == "" { str = string(gitprovider.RepositoryVisibilityPrivate) } - if !utils.ContainsItemString(supportedGitLabVisibilities, str) { - return fmt.Errorf("unsupported visibility '%s', must be one of: %s", - str, strings.Join(supportedGitLabVisibilities, ", ")) - + var visibility = gitprovider.RepositoryVisibility(str) + if ValidateRepositoryVisibility(visibility) != nil { + return fmt.Errorf("unsupported visibility '%s'", str) } - *d = GitLabVisibility(str) + *d = GitLabVisibility(visibility) return nil } @@ -54,5 +63,5 @@ func (d *GitLabVisibility) Type() string { } func (d *GitLabVisibility) Description() string { - return fmt.Sprintf("visibility, available options are: (%s)", strings.Join(supportedGitLabVisibilities, ", ")) + return fmt.Sprintf("specifies the visibility of the repository. Valid values are public, private, internal") } diff --git a/internal/flags/gitlab_visibility_test.go b/internal/flags/gitlab_visibility_test.go index c672524e72..bf360f6747 100644 --- a/internal/flags/gitlab_visibility_test.go +++ b/internal/flags/gitlab_visibility_test.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Flux authors +Copyright 2024 The Flux authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.