diff --git a/cmd/flux/create_source_git.go b/cmd/flux/create_source_git.go index 5a321c08..e915268b 100644 --- a/cmd/flux/create_source_git.go +++ b/cmd/flux/create_source_git.go @@ -49,6 +49,8 @@ type sourceGitFlags struct { branch string tag string semver string + refName string + commit string username string password string keyAlgorithm flags.PublicKeyAlgorithm @@ -129,6 +131,8 @@ func init() { createSourceGitCmd.Flags().StringVar(&sourceGitArgs.branch, "branch", "", "git branch") createSourceGitCmd.Flags().StringVar(&sourceGitArgs.tag, "tag", "", "git tag") createSourceGitCmd.Flags().StringVar(&sourceGitArgs.semver, "tag-semver", "", "git tag semver range") + createSourceGitCmd.Flags().StringVar(&sourceGitArgs.refName, "ref-name", "", " git reference name") + createSourceGitCmd.Flags().StringVar(&sourceGitArgs.commit, "commit", "", "git commit") createSourceGitCmd.Flags().StringVarP(&sourceGitArgs.username, "username", "u", "", "basic authentication username") createSourceGitCmd.Flags().StringVarP(&sourceGitArgs.password, "password", "p", "", "basic authentication password") createSourceGitCmd.Flags().Var(&sourceGitArgs.keyAlgorithm, "ssh-key-algorithm", sourceGitArgs.keyAlgorithm.Description()) @@ -168,8 +172,8 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("git URL scheme '%s' not supported, can be: ssh, http and https", u.Scheme) } - if sourceGitArgs.branch == "" && sourceGitArgs.tag == "" && sourceGitArgs.semver == "" { - return fmt.Errorf("a Git ref is required, use one of the following: --branch, --tag or --tag-semver") + if sourceGitArgs.branch == "" && sourceGitArgs.tag == "" && sourceGitArgs.semver == "" && sourceGitArgs.commit == "" && sourceGitArgs.refName == "" { + return fmt.Errorf("a Git ref is required, use one of the following: --branch, --tag, --commit, --ref-name or --tag-semver") } if sourceGitArgs.caFile != "" && u.Scheme == "ssh" { @@ -214,7 +218,12 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { gitRepository.Spec.Timeout = &metav1.Duration{Duration: createSourceArgs.fetchTimeout} } - if sourceGitArgs.semver != "" { + if sourceGitArgs.commit != "" { + gitRepository.Spec.Reference.Commit = sourceGitArgs.commit + gitRepository.Spec.Reference.Branch = sourceGitArgs.branch + } else if sourceGitArgs.refName != "" { + gitRepository.Spec.Reference.Name = sourceGitArgs.refName + } else if sourceGitArgs.semver != "" { gitRepository.Spec.Reference.SemVer = sourceGitArgs.semver } else if sourceGitArgs.tag != "" { gitRepository.Spec.Reference.Tag = sourceGitArgs.tag diff --git a/cmd/flux/create_source_git_test.go b/cmd/flux/create_source_git_test.go index 227567b2..5517c811 100644 --- a/cmd/flux/create_source_git_test.go +++ b/cmd/flux/create_source_git_test.go @@ -98,6 +98,41 @@ func TestCreateSourceGitExport(t *testing.T) { command, assertGoldenFile("testdata/create_source_git/export.golden"), }, + { + name: "no args", + args: "create secret git", + assert: assertError("name is required"), + }, + { + name: "source with commit", + args: "create source git podinfo --namespace=flux-system --url=https://github.com/stefanprodan/podinfo --commit=c88a2f41 --interval=1m0s --export", + assert: assertGoldenFile("./testdata/create_source_git/source-git-commit.yaml"), + }, + { + name: "source with ref name", + args: "create source git podinfo --namespace=flux-system --url=https://github.com/stefanprodan/podinfo --ref-name=refs/heads/main --interval=1m0s --export", + assert: assertGoldenFile("testdata/create_source_git/source-git-refname.yaml"), + }, + { + name: "source with branch name and commit", + args: "create source git podinfo --namespace=flux-system --url=https://github.com/stefanprodan/podinfo --branch=main --commit=c88a2f41 --interval=1m0s --export", + assert: assertGoldenFile("testdata/create_source_git/source-git-branch-commit.yaml"), + }, + { + name: "source with semver", + args: "create source git podinfo --namespace=flux-system --url=https://github.com/stefanprodan/podinfo --tag-semver=v1.01 --interval=1m0s --export", + assert: assertGoldenFile("testdata/create_source_git/source-git-semver.yaml"), + }, + { + name: "source with git tag", + args: "create source git podinfo --namespace=flux-system --url=https://github.com/stefanprodan/podinfo --tag=test --interval=1m0s --export", + assert: assertGoldenFile("testdata/create_source_git/source-git-tag.yaml"), + }, + { + name: "source with git branch", + args: "create source git podinfo --namespace=flux-system --url=https://github.com/stefanprodan/podinfo --branch=test --interval=1m0s --export", + assert: assertGoldenFile("testdata/create_source_git/source-git-branch.yaml"), + }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { diff --git a/cmd/flux/testdata/create_source_git/source-git-branch-commit.yaml b/cmd/flux/testdata/create_source_git/source-git-branch-commit.yaml new file mode 100644 index 00000000..e767a58d --- /dev/null +++ b/cmd/flux/testdata/create_source_git/source-git-branch-commit.yaml @@ -0,0 +1,12 @@ +--- +apiVersion: source.toolkit.fluxcd.io/v1beta2 +kind: GitRepository +metadata: + name: podinfo + namespace: flux-system +spec: + interval: 1m0s + ref: + branch: main + commit: c88a2f41 + url: https://github.com/stefanprodan/podinfo diff --git a/cmd/flux/testdata/create_source_git/source-git-branch.yaml b/cmd/flux/testdata/create_source_git/source-git-branch.yaml new file mode 100644 index 00000000..8d4ea097 --- /dev/null +++ b/cmd/flux/testdata/create_source_git/source-git-branch.yaml @@ -0,0 +1,11 @@ +--- +apiVersion: source.toolkit.fluxcd.io/v1beta2 +kind: GitRepository +metadata: + name: podinfo + namespace: flux-system +spec: + interval: 1m0s + ref: + branch: test + url: https://github.com/stefanprodan/podinfo diff --git a/cmd/flux/testdata/create_source_git/source-git-commit.yaml b/cmd/flux/testdata/create_source_git/source-git-commit.yaml new file mode 100644 index 00000000..4600b3cd --- /dev/null +++ b/cmd/flux/testdata/create_source_git/source-git-commit.yaml @@ -0,0 +1,11 @@ +--- +apiVersion: source.toolkit.fluxcd.io/v1beta2 +kind: GitRepository +metadata: + name: podinfo + namespace: flux-system +spec: + interval: 1m0s + ref: + commit: c88a2f41 + url: https://github.com/stefanprodan/podinfo diff --git a/cmd/flux/testdata/create_source_git/source-git-refname.yaml b/cmd/flux/testdata/create_source_git/source-git-refname.yaml new file mode 100644 index 00000000..ce45f216 --- /dev/null +++ b/cmd/flux/testdata/create_source_git/source-git-refname.yaml @@ -0,0 +1,11 @@ +--- +apiVersion: source.toolkit.fluxcd.io/v1beta2 +kind: GitRepository +metadata: + name: podinfo + namespace: flux-system +spec: + interval: 1m0s + ref: + name: refs/heads/main + url: https://github.com/stefanprodan/podinfo diff --git a/cmd/flux/testdata/create_source_git/source-git-semver.yaml b/cmd/flux/testdata/create_source_git/source-git-semver.yaml new file mode 100644 index 00000000..f5162811 --- /dev/null +++ b/cmd/flux/testdata/create_source_git/source-git-semver.yaml @@ -0,0 +1,11 @@ +--- +apiVersion: source.toolkit.fluxcd.io/v1beta2 +kind: GitRepository +metadata: + name: podinfo + namespace: flux-system +spec: + interval: 1m0s + ref: + semver: v1.01 + url: https://github.com/stefanprodan/podinfo diff --git a/cmd/flux/testdata/create_source_git/source-git-tag.yaml b/cmd/flux/testdata/create_source_git/source-git-tag.yaml new file mode 100644 index 00000000..b550fd84 --- /dev/null +++ b/cmd/flux/testdata/create_source_git/source-git-tag.yaml @@ -0,0 +1,11 @@ +--- +apiVersion: source.toolkit.fluxcd.io/v1beta2 +kind: GitRepository +metadata: + name: podinfo + namespace: flux-system +spec: + interval: 1m0s + ref: + tag: test + url: https://github.com/stefanprodan/podinfo