diff --git a/cmd/flux/create_source_git.go b/cmd/flux/create_source_git.go index 8c8c4a8f..4ca684bc 100644 --- a/cmd/flux/create_source_git.go +++ b/cmd/flux/create_source_git.go @@ -96,7 +96,7 @@ var ( sourceGitRSABits flags.RSAKeyBits = 2048 sourceGitECDSACurve = flags.ECDSACurve{Curve: elliptic.P384()} sourceGitSecretRef string - sourceGitImplementation string + sourceGitImplementation flags.GitImplementation ) func init() { @@ -110,7 +110,7 @@ func init() { createSourceGitCmd.Flags().Var(&sourceGitRSABits, "ssh-rsa-bits", sourceGitRSABits.Description()) createSourceGitCmd.Flags().Var(&sourceGitECDSACurve, "ssh-ecdsa-curve", sourceGitECDSACurve.Description()) createSourceGitCmd.Flags().StringVarP(&sourceGitSecretRef, "secret-ref", "", "", "the name of an existing secret containing SSH or basic credentials") - createSourceGitCmd.Flags().StringVar(&sourceGitImplementation, "git-implementation", "", "the git implementation to use, can be 'go-git' or 'libgit2'") + createSourceGitCmd.Flags().Var(&sourceGitImplementation, "git-implementation", sourceGitImplementation.Description()) createSourceCmd.AddCommand(createSourceGitCmd) } @@ -141,10 +141,6 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { return err } - if !utils.ContainsItemString([]string{sourcev1.GoGitImplementation, sourcev1.LibGit2Implementation, ""}, sourceGitImplementation) { - return fmt.Errorf("Invalid git implementation %q", sourceGitImplementation) - } - gitRepository := sourcev1.GitRepository{ ObjectMeta: metav1.ObjectMeta{ Name: name, @@ -156,11 +152,14 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { Interval: metav1.Duration{ Duration: interval, }, - Reference: &sourcev1.GitRepositoryRef{}, - GitImplementation: sourceGitImplementation, + Reference: &sourcev1.GitRepositoryRef{}, }, } + if sourceGitImplementation != "" { + gitRepository.Spec.GitImplementation = sourceGitImplementation.String() + } + if sourceGitSemver != "" { gitRepository.Spec.Reference.SemVer = sourceGitSemver } else if sourceGitTag != "" { diff --git a/docs/cmd/flux_create_source_git.md b/docs/cmd/flux_create_source_git.md index 57532d16..2744ad05 100644 --- a/docs/cmd/flux_create_source_git.md +++ b/docs/cmd/flux_create_source_git.md @@ -56,7 +56,7 @@ flux create source git [name] [flags] ``` --branch string git branch (default "master") - --git-implementation string the git implementation to use, can be 'go-git' or 'libgit2' + --git-implementation gitImplementation the Git implementation to use, available options are: (go-git, libgit2) -h, --help help for git -p, --password string basic authentication password --secret-ref string the name of an existing secret containing SSH or basic credentials diff --git a/internal/flags/git_implementation.go b/internal/flags/git_implementation.go new file mode 100644 index 00000000..b6896303 --- /dev/null +++ b/internal/flags/git_implementation.go @@ -0,0 +1,55 @@ +/* +Copyright 2021 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. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package flags + +import ( + "fmt" + "strings" + + sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" + + "github.com/fluxcd/flux2/internal/utils" +) + +var supportedGitImplementations = []string{sourcev1.GoGitImplementation, sourcev1.LibGit2Implementation} + +type GitImplementation string + +func (i *GitImplementation) String() string { + return string(*i) +} + +func (i *GitImplementation) Set(str string) error { + if str == "" { + return nil + } + if !utils.ContainsItemString(supportedGitImplementations, str) { + return fmt.Errorf("unsupported Git implementation '%s', must be one of: %s", + str, strings.Join(supportedGitImplementations, ", ")) + + } + *i = GitImplementation(str) + return nil +} + +func (i *GitImplementation) Type() string { + return "gitImplementation" +} + +func (i *GitImplementation) Description() string { + return fmt.Sprintf("the Git implementation to use, available options are: (%s)", strings.Join(supportedGitImplementations, ", ")) +} diff --git a/internal/flags/git_implementation_test.go b/internal/flags/git_implementation_test.go new file mode 100644 index 00000000..6372d4bf --- /dev/null +++ b/internal/flags/git_implementation_test.go @@ -0,0 +1,47 @@ +/* +Copyright 2021 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. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package flags + +import ( + "testing" + + sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" +) + +func TestGitImplementation_Set(t *testing.T) { + tests := []struct { + name string + str string + expect string + expectErr bool + }{ + {"supported", sourcev1.GoGitImplementation, sourcev1.GoGitImplementation, false}, + {"unsupported", "unsupported", "", true}, + {"empty", "", "", false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var i GitImplementation + if err := i.Set(tt.str); (err != nil) != tt.expectErr { + t.Errorf("Set() error = %v, expectErr %v", err, tt.expectErr) + } + if str := i.String(); str != tt.expect { + t.Errorf("Set() = %v, expect %v", str, tt.expect) + } + }) + } +}