Skip to content

Commit

Permalink
Skaffold init asks user to write skaffold.yaml
Browse files Browse the repository at this point in the history
Fix #1090

Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Oct 4, 2018
1 parent 8f96420 commit 37b2255
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
66 changes: 48 additions & 18 deletions cmd/skaffold/app/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ import (
// an image we parse out from a kubernetes manifest
const NoDockerfile = "None (image not built from these sources)"

var outfile string
var skipBuild bool
var cliArtifacts []string
var (
cliArtifacts []string
skipBuild bool
force bool
)

// NewCmdInit describes the CLI command to generate a skaffold configuration.
func NewCmdInit(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Expand All @@ -57,18 +60,16 @@ func NewCmdInit(out io.Writer) *cobra.Command {
return doInit(out)
},
}
AddInitFlags(cmd)
return cmd
}

func AddInitFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&outfile, "file", "f", "", "File to write generated skaffold config")
cmd.Flags().StringVarP(&opts.ConfigurationFile, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file")
cmd.Flags().BoolVar(&skipBuild, "skip-build", false, "Skip generating build artifacts in skaffold config")
cmd.Flags().BoolVar(&force, "force", false, "Force the generation of the skaffold config")
cmd.Flags().StringArrayVarP(&cliArtifacts, "artifact", "a", nil, "'='-delimited dockerfile/image pair to generate build artifact\n(example: --artifact=/web/Dockerfile.web=gcr.io/web-project/image)")
return cmd
}

func doInit(out io.Writer) error {
rootDir := "."

var potentialConfigs, k8sConfigs, dockerfiles, images []string
err := filepath.Walk(rootDir, func(path string, f os.FileInfo, e error) error {
if f.IsDir() {
Expand All @@ -90,11 +91,13 @@ func doInit(out io.Writer) error {
if err != nil {
return err
}

for _, file := range potentialConfigs {
config, err := schema.ParseConfig(file, true)
if err == nil && config != nil {
out.Write([]byte(fmt.Sprintf("pre-existing skaffold yaml %s found: exiting\n", file)))
return nil
if !force {
config, err := schema.ParseConfig(file, true)
if err == nil && config != nil {
return fmt.Errorf("pre-existing %s found", file)
}
}

logrus.Debugf("%s is not a valid skaffold configuration: continuing", file)
Expand Down Expand Up @@ -133,14 +136,41 @@ func doInit(out io.Writer) error {
if err != nil {
return err
}
if outfile != "" {
if err := ioutil.WriteFile(outfile, cfg, 0644); err != nil {
return errors.Wrap(err, "writing config to file")
}
} else {

if opts.ConfigurationFile == "-" {
out.Write(cfg)
return nil
}

if !force {
fmt.Fprintln(out, string(cfg))

reader := bufio.NewReader(os.Stdin)
confirmLoop:
for {
fmt.Fprintf(out, "Do you want to write this configuration to %s? [y/n]: ", opts.ConfigurationFile)

response, err := reader.ReadString('\n')
if err != nil {
return errors.Wrap(err, "reading user confirmation")
}

response = strings.ToLower(strings.TrimSpace(response))
switch response {
case "y", "yes":
break confirmLoop
case "n", "no":
return nil
}
}
}

if err := ioutil.WriteFile(opts.ConfigurationFile, cfg, 0644); err != nil {
return errors.Wrap(err, "writing config to file")
}

fmt.Fprintf(out, "Configuration %s was written\n", opts.ConfigurationFile)

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion integration/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func TestInit(t *testing.T) {
t.Errorf("error removing generated skaffold yaml: %v", err)
}
}()
initArgs := []string{"init", "-f", generatedYaml}
initArgs := []string{"init", "--force", "-f", generatedYaml}
initArgs = append(initArgs, test.args...)
initCmd := exec.Command("skaffold", initArgs...)
initCmd.Dir = test.dir
Expand Down

0 comments on commit 37b2255

Please sign in to comment.