Skip to content

Commit

Permalink
Moving options to pkg/commands/options. (ko-build#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
n3wscott authored and jonjohnsonjr committed Apr 26, 2019
1 parent b0a1702 commit fba1d3d
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 47 deletions.
51 changes: 26 additions & 25 deletions cmd/ko/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"github.com/google/ko/pkg/commands/options"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
5 changes: 3 additions & 2 deletions cmd/ko/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"fmt"
"github.com/google/ko/pkg/commands/options"
gb "go/build"
"log"
"os"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions cmd/ko/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"fmt"
"github.com/google/ko/pkg/commands/options"
"io"
"io/ioutil"
"log"
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/ko/binary.go → pkg/commands/options/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.")
}
4 changes: 2 additions & 2 deletions cmd/ko/debug.go → pkg/commands/options/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.")
}
6 changes: 3 additions & 3 deletions cmd/ko/filestuff.go → pkg/commands/options/filestuff.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package options

import (
"log"
Expand All @@ -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")
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cmd/ko/flatname.go → pkg/commands/options/flatname.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/ko/local.go → pkg/commands/options/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.")
}
4 changes: 2 additions & 2 deletions cmd/ko/tags.go → pkg/commands/options/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.")
}

0 comments on commit fba1d3d

Please sign in to comment.