diff --git a/cmd/config/configcobra/cmds.go b/cmd/config/configcobra/cmds.go index 82754e74d..2c32756e6 100644 --- a/cmd/config/configcobra/cmds.go +++ b/cmd/config/configcobra/cmds.go @@ -105,6 +105,7 @@ func NewConfigCommand(name string) *cobra.Command { root.AddCommand(commands.XArgsCommand()) root.AddCommand(commands.WrapCommand()) + root.AddCommand(commands.InitCommand(name)) root.AddCommand(commands.SetCommand(name)) root.AddCommand(commands.ListSettersCommand(name)) root.AddCommand(commands.CreateSetterCommand(name)) diff --git a/cmd/config/docs/commands/init.md b/cmd/config/docs/commands/init.md new file mode 100644 index 000000000..cc5e6f066 --- /dev/null +++ b/cmd/config/docs/commands/init.md @@ -0,0 +1,18 @@ +## init + +[Alpha] Initialize a directory with a Krmfile. + +### Synopsis + +[Alpha] Initialize a directory with a Krmfile. + + DIR: + Path to local directory. + +### Examples + + # create a Krmfile in the local directory + kustomize config init + + # create a Krmfile in my-dir/ + kustomize config init my-dir/ diff --git a/cmd/config/internal/commands/cmdinit.go b/cmd/config/internal/commands/cmdinit.go new file mode 100644 index 000000000..e72a0932d --- /dev/null +++ b/cmd/config/internal/commands/cmdinit.go @@ -0,0 +1,60 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package commands + +import ( + "io/ioutil" + "os" + "path/filepath" + "strings" + + "github.com/spf13/cobra" + "sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands" + "sigs.k8s.io/kustomize/kyaml/errors" + "sigs.k8s.io/kustomize/kyaml/krmfile" +) + +// GetInitRunner returns a command InitRunner. +func GetInitRunner(name string) *InitRunner { + r := &InitRunner{} + c := &cobra.Command{ + Use: "init DIR...", + Args: cobra.RangeArgs(0, 1), + Short: commands.InitShort, + Long: commands.InitLong, + Example: commands.InitExamples, + RunE: r.runE, + } + fixDocs(name, c) + r.Command = c + return r +} + +func InitCommand(name string) *cobra.Command { + return GetInitRunner(name).Command +} + +// InitRunner contains the init function +type InitRunner struct { + Command *cobra.Command +} + +func (r *InitRunner) runE(c *cobra.Command, args []string) error { + var dir string + if len(args) == 0 { + dir = "." + } else { + dir = args[0] + } + filename := filepath.Join(dir, krmfile.KrmfileName) + + if _, err := os.Stat(filename); err == nil || !os.IsNotExist(err) { + return errors.Errorf("directory already initialized with a Krmfile") + } + + return ioutil.WriteFile(filename, []byte(strings.TrimSpace(` +apiVersion: config.k8s.io/v1alpha1 +kind: Krmfile +`)), 0600) +} diff --git a/cmd/config/internal/commands/cmdset.go b/cmd/config/internal/commands/cmdset.go index b0c364bef..6886d16ed 100644 --- a/cmd/config/internal/commands/cmdset.go +++ b/cmd/config/internal/commands/cmdset.go @@ -159,7 +159,6 @@ func lookup(l setters.LookupSetters, c *cobra.Command, args []string) error { } table.Render() - fmt.Println(l.SetterCounts) if len(l.SetterCounts) == 0 { // exit non-0 if no matching setters are found os.Exit(1) diff --git a/cmd/config/internal/commands/e2e/init_test.go b/cmd/config/internal/commands/e2e/init_test.go new file mode 100644 index 000000000..7fd22187d --- /dev/null +++ b/cmd/config/internal/commands/e2e/init_test.go @@ -0,0 +1,33 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package e2e + +import "testing" + +func TestInit(t *testing.T) { + tests := []test{ + { + name: "init", + args: []string{"init"}, + expectedFiles: map[string]string{ + "Krmfile": ` +apiVersion: config.k8s.io/v1alpha1 +kind: Krmfile +`, + }, + }, + + { + name: "init", + args: []string{"init", "."}, + expectedFiles: map[string]string{ + "Krmfile": ` +apiVersion: config.k8s.io/v1alpha1 +kind: Krmfile +`, + }, + }, + } + runTests(t, tests) +} diff --git a/cmd/config/internal/commands/init_test.go b/cmd/config/internal/commands/init_test.go new file mode 100644 index 000000000..e49649712 --- /dev/null +++ b/cmd/config/internal/commands/init_test.go @@ -0,0 +1,84 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package commands_test + +import ( + "bytes" + "io/ioutil" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "sigs.k8s.io/kustomize/cmd/config/internal/commands" +) + +func TestInit_args(t *testing.T) { + d, err := ioutil.TempDir("", "kustomize-cat-test") + if !assert.NoError(t, err) { + return + } + defer os.RemoveAll(d) + + // fmt the files + b := &bytes.Buffer{} + r := commands.GetInitRunner("") + r.Command.SetArgs([]string{d}) + r.Command.SetOut(b) + if !assert.NoError(t, r.Command.Execute()) { + t.FailNow() + } + + actual, err := ioutil.ReadFile(filepath.Join(d, "Krmfile")) + if !assert.NoError(t, err) { + t.FailNow() + } + + if !assert.Equal(t, strings.TrimSpace(` +apiVersion: config.k8s.io/v1alpha1 +kind: Krmfile +`), strings.TrimSpace(string(actual))) { + t.FailNow() + } + + if !assert.Equal(t, "", b.String()) { + t.FailNow() + } +} + +func TestInit_noargs(t *testing.T) { + d, err := ioutil.TempDir("", "kustomize-test-") + if !assert.NoError(t, err) { + return + } + defer os.RemoveAll(d) + + if !assert.NoError(t, os.Chdir(d)) { + t.FailNow() + } + + b := &bytes.Buffer{} + r := commands.GetInitRunner("") + r.Command.SetOut(b) + if !assert.NoError(t, r.Command.Execute()) { + t.FailNow() + } + + actual, err := ioutil.ReadFile(filepath.Join(d, "Krmfile")) + if !assert.NoError(t, err) { + t.FailNow() + } + + if !assert.Equal(t, strings.TrimSpace(` +apiVersion: config.k8s.io/v1alpha1 +kind: Krmfile +`), strings.TrimSpace(string(actual))) { + t.FailNow() + } + + if !assert.Equal(t, "", b.String()) { + t.FailNow() + } +} diff --git a/cmd/config/internal/generateddocs/commands/docs.go b/cmd/config/internal/generateddocs/commands/docs.go index 163399efb..05ce0a9d1 100644 --- a/cmd/config/internal/generateddocs/commands/docs.go +++ b/cmd/config/internal/generateddocs/commands/docs.go @@ -156,6 +156,20 @@ var GrepExamples = ` # look for Resources matching a specific container image kustomize config grep "spec.template.spec.containers[name=nginx].image=nginx:1\.7\.9" my-dir/ | kustomize config tree` +var InitShort = `[Alpha] Initialize a directory with a Krmfile.` +var InitLong = ` +[Alpha] Initialize a directory with a Krmfile. + + DIR: + Path to local directory. +` +var InitExamples = ` + # create a Krmfile in the local directory + kustomize config init + + # create a Krmfile in my-dir/ + kustomize config init my-dir/` + var ListSettersShort = `[Alpha] List setters for Resources.` var ListSettersLong = ` List setters for Resources.