From 737e19bde6f2f39080d577f8c2525e91aa62a3a3 Mon Sep 17 00:00:00 2001 From: Scott Nichols Date: Fri, 26 Apr 2019 15:20:31 -0700 Subject: [PATCH] Moving options to pkg/commands/options. --- cmd/ko/commands.go | 51 ++++++++++--------- cmd/ko/publish.go | 5 +- cmd/ko/resolve.go | 13 ++--- {cmd/ko => pkg/commands/options}/binary.go | 4 +- {cmd/ko => pkg/commands/options}/debug.go | 4 +- {cmd/ko => pkg/commands/options}/filestuff.go | 6 +-- {cmd/ko => pkg/commands/options}/flatname.go | 6 +-- {cmd/ko => pkg/commands/options}/local.go | 4 +- {cmd/ko => pkg/commands/options}/tags.go | 4 +- 9 files changed, 50 insertions(+), 47 deletions(-) rename {cmd/ko => pkg/commands/options}/binary.go (92%) rename {cmd/ko => pkg/commands/options}/debug.go (93%) rename {cmd/ko => pkg/commands/options}/filestuff.go (96%) rename {cmd/ko => pkg/commands/options}/flatname.go (93%) rename {cmd/ko => pkg/commands/options}/local.go (92%) rename {cmd/ko => pkg/commands/options}/tags.go (92%) diff --git a/cmd/ko/commands.go b/cmd/ko/commands.go index 7a0d1c4dec..adc9c686e1 100644 --- a/cmd/ko/commands.go +++ b/cmd/ko/commands.go @@ -15,6 +15,7 @@ package main import ( + "github.com/google/ko/pkg/commands/options" "log" "os" "os/exec" @@ -65,12 +66,12 @@ func addKubeCommands(topLevel *cobra.Command) { }) koApplyFlags := []string{} - lo := &LocalOptions{} - bo := &BinaryOptions{} - no := &NameOptions{} - fo := &FilenameOptions{} - ta := &TagsOptions{} - do := &DebugOptions{} + lo := &options.LocalOptions{} + bo := &options.BinaryOptions{} + no := &options.NameOptions{} + fo := &options.FilenameOptions{} + ta := &options.TagsOptions{} + do := &options.DebugOptions{} apply := &cobra.Command{ Use: "apply -f FILENAME", Short: "Apply the input files with image references resolved to built/pushed image digests.", @@ -154,11 +155,11 @@ func addKubeCommands(topLevel *cobra.Command) { } }, } - addLocalArg(apply, lo) - addNamingArgs(apply, no) - addFileArg(apply, fo) - addTagsArg(apply, ta) - addDebugArg(apply, do) + options.AddLocalArg(apply, lo) + options.AddNamingArgs(apply, no) + options.AddFileArg(apply, fo) + options.AddTagsArg(apply, ta) + options.AddDebugArg(apply, do) // Collect the ko-specific apply flags before registering the kubectl global // flags so that we can ignore them when passing kubectl global flags through @@ -202,11 +203,11 @@ func addKubeCommands(topLevel *cobra.Command) { resolveFilesToWriter(fo, no, lo, ta, do, os.Stdout) }, } - addLocalArg(resolve, lo) - addNamingArgs(resolve, no) - addFileArg(resolve, fo) - addTagsArg(resolve, ta) - addDebugArg(resolve, do) + options.AddLocalArg(resolve, lo) + options.AddNamingArgs(resolve, no) + options.AddFileArg(resolve, fo) + options.AddTagsArg(resolve, ta) + options.AddDebugArg(resolve, do) topLevel.AddCommand(resolve) publish := &cobra.Command{ @@ -243,10 +244,10 @@ func addKubeCommands(topLevel *cobra.Command) { publishImages(args, no, lo, ta, do) }, } - addLocalArg(publish, lo) - addNamingArgs(publish, no) - addTagsArg(publish, ta) - addDebugArg(publish, do) + options.AddLocalArg(publish, lo) + options.AddNamingArgs(publish, no) + options.AddTagsArg(publish, ta) + options.AddDebugArg(publish, do) topLevel.AddCommand(publish) run := &cobra.Command{ @@ -293,11 +294,11 @@ func addKubeCommands(topLevel *cobra.Command) { UnknownFlags: true, }, } - addLocalArg(run, lo) - addNamingArgs(run, no) - addImageArg(run, bo) - addTagsArg(run, ta) - addDebugArg(run, do) + options.AddLocalArg(run, lo) + options.AddNamingArgs(run, no) + options.AddImageArg(run, bo) + options.AddTagsArg(run, ta) + options.AddDebugArg(run, do) topLevel.AddCommand(run) } diff --git a/cmd/ko/publish.go b/cmd/ko/publish.go index 867bb70b63..701448f37a 100644 --- a/cmd/ko/publish.go +++ b/cmd/ko/publish.go @@ -16,6 +16,7 @@ package main import ( "fmt" + "github.com/google/ko/pkg/commands/options" gb "go/build" "log" "os" @@ -38,7 +39,7 @@ func qualifyLocalImport(importpath, gopathsrc, pwd string) (string, error) { return filepath.Join(strings.TrimPrefix(pwd, gopathsrc+string(filepath.Separator)), importpath), nil } -func publishImages(importpaths []string, no *NameOptions, lo *LocalOptions, ta *TagsOptions, do *DebugOptions) map[string]name.Reference { +func publishImages(importpaths []string, no *options.NameOptions, lo *options.LocalOptions, ta *options.TagsOptions, do *options.DebugOptions) map[string]name.Reference { opt, err := gobuildOptions(do) if err != nil { log.Fatalf("error setting up builder options: %v", err) @@ -74,7 +75,7 @@ func publishImages(importpaths []string, no *NameOptions, lo *LocalOptions, ta * var pub publish.Interface repoName := os.Getenv("KO_DOCKER_REPO") - namer := makeNamer(no) + namer := options.MakeNamer(no) if lo.Local || repoName == publish.LocalDomain { pub = publish.NewDaemon(namer, ta.Tags) diff --git a/cmd/ko/resolve.go b/cmd/ko/resolve.go index 9933afdcfc..f0256fd160 100644 --- a/cmd/ko/resolve.go +++ b/cmd/ko/resolve.go @@ -16,6 +16,7 @@ package main import ( "fmt" + "github.com/google/ko/pkg/commands/options" "io" "io/ioutil" "log" @@ -30,7 +31,7 @@ import ( "github.com/mattmoor/dep-notify/pkg/graph" ) -func gobuildOptions(do *DebugOptions) ([]build.Option, error) { +func gobuildOptions(do *options.DebugOptions) ([]build.Option, error) { creationTime, err := getCreationTime() if err != nil { return nil, err @@ -47,7 +48,7 @@ func gobuildOptions(do *DebugOptions) ([]build.Option, error) { return opts, nil } -func makeBuilder(do *DebugOptions) (*build.Caching, error) { +func makeBuilder(do *options.DebugOptions) (*build.Caching, error) { opt, err := gobuildOptions(do) if err != nil { log.Fatalf("error setting up builder options: %v", err) @@ -76,11 +77,11 @@ func makeBuilder(do *DebugOptions) (*build.Caching, error) { return build.NewCaching(innerBuilder) } -func makePublisher(no *NameOptions, lo *LocalOptions, ta *TagsOptions) (publish.Interface, error) { +func makePublisher(no *options.NameOptions, lo *options.LocalOptions, ta *options.TagsOptions) (publish.Interface, error) { // Create the publish.Interface that we will use to publish image references // to either a docker daemon or a container image registry. innerPublisher, err := func() (publish.Interface, error) { - namer := makeNamer(no) + namer := options.MakeNamer(no) repoName := os.Getenv("KO_DOCKER_REPO") if lo.Local || repoName == publish.LocalDomain { @@ -107,7 +108,7 @@ func makePublisher(no *NameOptions, lo *LocalOptions, ta *TagsOptions) (publish. // resolvedFuture represents a "future" for the bytes of a resolved file. type resolvedFuture chan []byte -func resolveFilesToWriter(fo *FilenameOptions, no *NameOptions, lo *LocalOptions, ta *TagsOptions, do *DebugOptions, out io.WriteCloser) { +func resolveFilesToWriter(fo *options.FilenameOptions, no *options.NameOptions, lo *options.LocalOptions, ta *options.TagsOptions, do *options.DebugOptions, out io.WriteCloser) { defer out.Close() builder, err := makeBuilder(do) if err != nil { @@ -124,7 +125,7 @@ func resolveFilesToWriter(fo *FilenameOptions, no *NameOptions, lo *LocalOptions // watcher and leave `fs` open to stream the names of yaml files // affected by code changes (including the modification of existing or // creation of new yaml files). - fs := enumerateFiles(fo) + fs := options.EnumerateFiles(fo) // This tracks filename -> []importpath var sm sync.Map diff --git a/cmd/ko/binary.go b/pkg/commands/options/binary.go similarity index 92% rename from cmd/ko/binary.go rename to pkg/commands/options/binary.go index 100b5f1849..efe164470a 100644 --- a/cmd/ko/binary.go +++ b/pkg/commands/options/binary.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package options import ( "github.com/spf13/cobra" @@ -24,7 +24,7 @@ type BinaryOptions struct { Path string } -func addImageArg(cmd *cobra.Command, lo *BinaryOptions) { +func AddImageArg(cmd *cobra.Command, lo *BinaryOptions) { cmd.Flags().StringVarP(&lo.Path, "image", "i", lo.Path, "The import path of the binary to publish.") } diff --git a/cmd/ko/debug.go b/pkg/commands/options/debug.go similarity index 93% rename from cmd/ko/debug.go rename to pkg/commands/options/debug.go index cfd481582d..878254f9ca 100644 --- a/cmd/ko/debug.go +++ b/pkg/commands/options/debug.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package options import ( "github.com/spf13/cobra" @@ -23,7 +23,7 @@ type DebugOptions struct { DisableOptimizations bool } -func addDebugArg(cmd *cobra.Command, do *DebugOptions) { +func AddDebugArg(cmd *cobra.Command, do *DebugOptions) { cmd.Flags().BoolVar(&do.DisableOptimizations, "disable-optimizations", do.DisableOptimizations, "Disable optimizations when building Go code. Useful when you want to interactively debug the created container.") } diff --git a/cmd/ko/filestuff.go b/pkg/commands/options/filestuff.go similarity index 96% rename from cmd/ko/filestuff.go rename to pkg/commands/options/filestuff.go index e492ced47f..4a5c499619 100644 --- a/cmd/ko/filestuff.go +++ b/pkg/commands/options/filestuff.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package options import ( "log" @@ -30,7 +30,7 @@ type FilenameOptions struct { Watch bool } -func addFileArg(cmd *cobra.Command, fo *FilenameOptions) { +func AddFileArg(cmd *cobra.Command, fo *FilenameOptions) { // From pkg/kubectl cmd.Flags().StringSliceVarP(&fo.Filenames, "filename", "f", fo.Filenames, "Filename, directory, or URL to files to use to create the resource") @@ -41,7 +41,7 @@ func addFileArg(cmd *cobra.Command, fo *FilenameOptions) { } // Based heavily on pkg/kubectl -func enumerateFiles(fo *FilenameOptions) chan string { +func EnumerateFiles(fo *FilenameOptions) chan string { files := make(chan string) go func() { // When we're done enumerating files, close the channel diff --git a/cmd/ko/flatname.go b/pkg/commands/options/flatname.go similarity index 93% rename from cmd/ko/flatname.go rename to pkg/commands/options/flatname.go index 1fed878fb2..98574750d9 100644 --- a/cmd/ko/flatname.go +++ b/pkg/commands/options/flatname.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package options import ( "crypto/md5" @@ -31,7 +31,7 @@ type NameOptions struct { BaseImportPaths bool } -func addNamingArgs(cmd *cobra.Command, no *NameOptions) { +func AddNamingArgs(cmd *cobra.Command, no *NameOptions) { cmd.Flags().BoolVarP(&no.PreserveImportPaths, "preserve-import-paths", "P", no.PreserveImportPaths, "Whether to preserve the full import path after KO_DOCKER_REPO.") cmd.Flags().BoolVarP(&no.BaseImportPaths, "base-import-paths", "B", no.BaseImportPaths, @@ -52,7 +52,7 @@ func baseImportPaths(importpath string) string { return filepath.Base(importpath) } -func makeNamer(no *NameOptions) publish.Namer { +func MakeNamer(no *NameOptions) publish.Namer { if no.PreserveImportPaths { return preserveImportPath } else if no.BaseImportPaths { diff --git a/cmd/ko/local.go b/pkg/commands/options/local.go similarity index 92% rename from cmd/ko/local.go rename to pkg/commands/options/local.go index 1b99e255ab..bfe468e213 100644 --- a/cmd/ko/local.go +++ b/pkg/commands/options/local.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package options import ( "github.com/spf13/cobra" @@ -24,7 +24,7 @@ type LocalOptions struct { Local bool } -func addLocalArg(cmd *cobra.Command, lo *LocalOptions) { +func AddLocalArg(cmd *cobra.Command, lo *LocalOptions) { cmd.Flags().BoolVarP(&lo.Local, "local", "L", lo.Local, "Whether to publish images to a local docker daemon vs. a registry.") } diff --git a/cmd/ko/tags.go b/pkg/commands/options/tags.go similarity index 92% rename from cmd/ko/tags.go rename to pkg/commands/options/tags.go index 1111516f5d..22c1d98081 100644 --- a/cmd/ko/tags.go +++ b/pkg/commands/options/tags.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package options import ( "github.com/spf13/cobra" @@ -23,7 +23,7 @@ type TagsOptions struct { Tags []string } -func addTagsArg(cmd *cobra.Command, ta *TagsOptions) { +func AddTagsArg(cmd *cobra.Command, ta *TagsOptions) { cmd.Flags().StringSliceVarP(&ta.Tags, "tags", "t", []string{"latest"}, "Which tags to use for the produced image instead of the default 'latest' tag.") }