mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Support initializing a directory with a Krmfile
This commit is contained in:
committed by
Phillip Wittrock
parent
0e5e2648b3
commit
075846c731
@@ -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))
|
||||
|
||||
18
cmd/config/docs/commands/init.md
Normal file
18
cmd/config/docs/commands/init.md
Normal file
@@ -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/
|
||||
60
cmd/config/internal/commands/cmdinit.go
Normal file
60
cmd/config/internal/commands/cmdinit.go
Normal file
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
33
cmd/config/internal/commands/e2e/init_test.go
Normal file
33
cmd/config/internal/commands/e2e/init_test.go
Normal file
@@ -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)
|
||||
}
|
||||
84
cmd/config/internal/commands/init_test.go
Normal file
84
cmd/config/internal/commands/init_test.go
Normal file
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user