Skip to content

Commit

Permalink
Refactored visibility validation; extended description; added default…
Browse files Browse the repository at this point in the history
… value; updated license date
  • Loading branch information
nagyv committed Jul 5, 2024
1 parent 82c150a commit 047b51a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
8 changes: 7 additions & 1 deletion cmd/flux/bootstrap_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
35 changes: 22 additions & 13 deletions internal/flags/gitlab_visibility.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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")
}
2 changes: 1 addition & 1 deletion internal/flags/gitlab_visibility_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 047b51a

Please sign in to comment.