feat: git options
Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
		
							parent
							
								
									f744acfbb7
								
							
						
					
					
						commit
						e483bee97c
					
				|  | @ -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().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.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().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().Var(&opts.Git, "git", "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") |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // addHiddenFlags marks certain flags as hidden from the executor help text
 | // addHiddenFlags marks certain flags as hidden from the executor help text
 | ||||||
|  |  | ||||||
|  | @ -17,6 +17,10 @@ limitations under the License. | ||||||
| package config | package config | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"errors" | ||||||
|  | 	"fmt" | ||||||
|  | 	"strconv" | ||||||
|  | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | @ -67,6 +71,40 @@ type KanikoGitOptions struct { | ||||||
| 	RecurseSubmodules bool | 	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.
 | // WarmerOptions are options that are set by command line arguments to the cache warmer.
 | ||||||
| type WarmerOptions struct { | type WarmerOptions struct { | ||||||
| 	CacheOptions | 	CacheOptions | ||||||
|  |  | ||||||
|  | @ -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) | ||||||
|  | 	}) | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue