From e483bee97cc1e39c2e7671843c20d14dd8b05bdf Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 24 Jun 2020 15:31:32 -0300 Subject: [PATCH] feat: git options Signed-off-by: Carlos Alexandro Becker --- cmd/executor/cmd/root.go | 4 +--- pkg/config/options.go | 38 ++++++++++++++++++++++++++++++++++++++ pkg/config/options_test.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 pkg/config/options_test.go diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index 5156322d6..695ddca32 100644 --- a/cmd/executor/cmd/root.go +++ b/cmd/executor/cmd/root.go @@ -175,9 +175,7 @@ func addKanikoOptionsFlags() { RootCmd.PersistentFlags().VarP(&opts.Labels, "label", "", "Set metadata for an image. Set it repeatedly for multiple labels.") RootCmd.PersistentFlags().BoolVarP(&opts.SkipUnusedStages, "skip-unused-stages", "", false, "Build only used stages if defined to true. Otherwise it builds by default all stages, even the unnecessaries ones until it reaches the target stage / end of Dockerfile") RootCmd.PersistentFlags().BoolVarP(&opts.RunV2, "use-new-run", "", false, "Experimental run command to detect file system changes. This new run command does no rely on snapshotting to detect changes.") - RootCmd.PersistentFlags().StringVarP(&opts.Git.Branch, "git-branch", "", "", "Branch to clone if build context is a git repository") - RootCmd.PersistentFlags().BoolVarP(&opts.Git.SingleBranch, "git-single-branch", "", false, "Whether to clone a single branch") - RootCmd.PersistentFlags().BoolVarP(&opts.Git.RecurseSubmodules, "git-recurse-submodules", "", false, "Whether to clone recursing submodules") + RootCmd.PersistentFlags().Var(&opts.Git, "git", "Branch to clone if build context is a git repository") } // addHiddenFlags marks certain flags as hidden from the executor help text diff --git a/pkg/config/options.go b/pkg/config/options.go index 5ba57914d..669b84f58 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -17,6 +17,10 @@ limitations under the License. package config import ( + "errors" + "fmt" + "strconv" + "strings" "time" ) @@ -67,6 +71,40 @@ type KanikoGitOptions struct { RecurseSubmodules bool } +var ErrInvalidGitFlag = errors.New("invalid git flag, must be in the key=value format") + +func (k *KanikoGitOptions) Type() string { + return "gitoptions" +} + +func (k *KanikoGitOptions) String() string { + return fmt.Sprintf("branch=%s,single-branch=%t,recurse-submodules=%t", k.Branch, k.SingleBranch, k.RecurseSubmodules) +} + +func (k *KanikoGitOptions) Set(s string) error { + var parts = strings.SplitN(s, "=", 2) + if len(parts) != 2 { + return fmt.Errorf("%w: %s", ErrInvalidGitFlag, s) + } + switch parts[0] { + case "branch": + k.Branch = parts[1] + case "single-branch": + v, err := strconv.ParseBool(parts[1]) + if err != nil { + return err + } + k.SingleBranch = v + case "recurse-submodules": + v, err := strconv.ParseBool(parts[1]) + if err != nil { + return err + } + k.RecurseSubmodules = v + } + return nil +} + // WarmerOptions are options that are set by command line arguments to the cache warmer. type WarmerOptions struct { CacheOptions diff --git a/pkg/config/options_test.go b/pkg/config/options_test.go new file mode 100644 index 000000000..cc6ac6693 --- /dev/null +++ b/pkg/config/options_test.go @@ -0,0 +1,37 @@ +package config + +import ( + "testing" + + "github.com/GoogleContainerTools/kaniko/testutil" +) + +func TestKanikoGitOptions(t *testing.T) { + t.Run("invalid pair", func(t *testing.T) { + var g = &KanikoGitOptions{} + testutil.CheckError(t, true, g.Set("branch")) + }) + + t.Run("sets values", func(t *testing.T) { + var g = &KanikoGitOptions{} + testutil.CheckNoError(t, g.Set("branch=foo")) + testutil.CheckNoError(t, g.Set("recurse-submodules=true")) + testutil.CheckNoError(t, g.Set("single-branch=true")) + testutil.CheckDeepEqual(t, KanikoGitOptions{ + Branch: "foo", + SingleBranch: true, + RecurseSubmodules: true, + }, *g) + }) + + t.Run("sets bools other than true", func(t *testing.T) { + var g = KanikoGitOptions{} + testutil.CheckError(t, true, g.Set("recurse-submodules=")) + testutil.CheckError(t, true, g.Set("single-branch=zaza")) + testutil.CheckNoError(t, g.Set("recurse-submodules=false")) + testutil.CheckDeepEqual(t, KanikoGitOptions{ + SingleBranch: false, + RecurseSubmodules: false, + }, g) + }) +}