/
cmdinit.go
63 lines (54 loc) · 1.53 KB
/
cmdinit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 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/cmd/config/runner"
"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,
Deprecated: "setter commands and substitutions will no longer be available in kustomize v5.\n" +
"See discussion in https://github.com/kubernetes-sigs/kustomize/issues/3953.",
}
runner.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)
}