From 0151d0965be7d8a53c463f61dab95fa09189adbd Mon Sep 17 00:00:00 2001 From: Cyril Diagne Date: Sat, 4 Jan 2020 10:47:28 +0100 Subject: [PATCH] add --dry-run flag --- cmd/deploy.go | 32 +++++++++++++++++++++++++------- cmd/dev.go | 14 +++++++++++--- docs/cli.md | 18 ++++++++++++++---- 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/cmd/deploy.go b/cmd/deploy.go index 68d8d90..81f47c2 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -33,12 +33,15 @@ var deployCmd = &cobra.Command{ panic(err) } + // Check if running a dry run. + dryRun, _ := cmd.Flags().GetBool("dry-run") + if cfg.Deployer.Remote != nil { - if err := deployWithRemote(manifest); err != nil { + if err := deployWithRemote(manifest, dryRun); err != nil { fmt.Println("ERROR:", err) } } else if cfg.Deployer.Skaffold != nil { - if err := deployWithSkaffold(manifest); err != nil { + if err := deployWithSkaffold(manifest, dryRun); err != nil { fmt.Println("ERROR:", err) } } @@ -47,16 +50,16 @@ var deployCmd = &cobra.Command{ func init() { RootCmd.AddCommand(deployCmd) + deployCmd.Flags().Bool("dry-run", false, "Just generate the config files.") } -func deployWithRemote(manifest *latest.Manifest) error { +func deployWithRemote(manifest *latest.Manifest, dryRun bool) error { // Create destination tar file - output, err := ioutil.TempFile("", "*.rar") + output, err := ioutil.TempFile("", "*.tar") fmt.Println("Building context tar:", output.Name()) if err != nil { return err } - defer os.Remove(output.Name()) // Open .dockerignore file if it exists dockerignore, err := os.Open(".dockerignore") @@ -66,7 +69,16 @@ func deployWithRemote(manifest *latest.Manifest) error { defer dockerignore.Close() // Tar context folder. - utils.Tar("./", output.Name(), output, dockerignore) + source := "./" + utils.Tar(source, output.Name(), output, dockerignore) + + if dryRun { + fmt.Println("Dry run: Skipping remote deployment.") + return nil + } + + // Defer the deletion of the temp tar file. + defer os.Remove(output.Name()) fmt.Println("Sending to deployer:", cfg.Deployer.Remote.URL) @@ -115,7 +127,7 @@ func deployWithRemote(manifest *latest.Manifest) error { return nil } -func deployWithSkaffold(manifest *latest.Manifest) error { +func deployWithSkaffold(manifest *latest.Manifest, dryRun bool) error { folder := cfg.Deployer.Skaffold.ConfigFolder registry := cfg.Deployer.Skaffold.DockerRegistry @@ -130,6 +142,12 @@ func deployWithSkaffold(manifest *latest.Manifest) error { if err != nil { return err } + fmt.Println("Config files have been written in:", folder) + + if dryRun { + fmt.Println("Dry run: Skipping execution.") + return nil + } // Run command. args := []string{"run", "-f", skaffoldFile} diff --git a/cmd/dev.go b/cmd/dev.go index 5e1f28c..b686d36 100644 --- a/cmd/dev.go +++ b/cmd/dev.go @@ -29,10 +29,11 @@ var devCmd = &cobra.Command{ } if cfg.Deployer.Remote != nil { - panic("dev is not yet supported with remote deployers.") + panic("dev is not yet supported on remote deployers") } else if cfg.Deployer.Skaffold != nil { // Start dev with Skaffold. - if err := devWithSkaffold(*manifest); err != nil { + dryRun, _ := cmd.Flags().GetBool("dry-run") + if err := devWithSkaffold(*manifest, dryRun); err != nil { fmt.Println("ERROR:", err) } } @@ -41,9 +42,10 @@ var devCmd = &cobra.Command{ func init() { RootCmd.AddCommand(devCmd) + devCmd.Flags().Bool("dry-run", false, "Just generate the config files.") } -func devWithSkaffold(manifest latest.Manifest) error { +func devWithSkaffold(manifest latest.Manifest, dryRun bool) error { folder := cfg.Deployer.Skaffold.ConfigFolder registry := cfg.Deployer.Skaffold.DockerRegistry @@ -58,6 +60,12 @@ func devWithSkaffold(manifest latest.Manifest) error { if err != nil { return err } + fmt.Println("Config files have been written in:", folder) + + if dryRun { + fmt.Println("Dry run: Skipping execution.") + return nil + } // Run command. args := []string{"dev", "-f", skaffoldFile} diff --git a/docs/cli.md b/docs/cli.md index 0fe350e..e1b9a28 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -3,7 +3,7 @@ ## โ†’ Init ```bash -kuda init [-n, --namespace] [-d, --docker-registry] +kuda init [flags] ``` Initializes the local configuration. @@ -22,7 +22,8 @@ Initializes the local configuration. ```bash kuda init -d gcr.io/my-gcp-project skaffold ``` -