From 684dd1a3ddfea4c2712cdc153a92230066c144cc Mon Sep 17 00:00:00 2001 From: Cornelius Weig Date: Fri, 1 Feb 2019 21:23:25 +0100 Subject: [PATCH] Move command names to code constants Signed-off-by: Cornelius Weig --- cmd/skaffold/app/cmd/build.go | 5 +++-- cmd/skaffold/app/cmd/delete.go | 5 +++-- cmd/skaffold/app/cmd/deploy.go | 5 +++-- cmd/skaffold/app/cmd/dev.go | 5 +++-- cmd/skaffold/app/cmd/run.go | 5 +++-- integration/run_test.go | 17 +++++++++-------- pkg/skaffold/constants/constants.go | 6 ++++++ pkg/skaffold/runner/runner.go | 3 ++- pkg/skaffold/schema/profiles_test.go | 23 ++++++++++++----------- 9 files changed, 44 insertions(+), 30 deletions(-) diff --git a/cmd/skaffold/app/cmd/build.go b/cmd/skaffold/app/cmd/build.go index 219f0f77fd3..1b0c35ee6b3 100644 --- a/cmd/skaffold/app/cmd/build.go +++ b/cmd/skaffold/app/cmd/build.go @@ -23,6 +23,7 @@ import ( "github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/flags" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -36,11 +37,11 @@ var ( // NewCmdBuild describes the CLI command to build artifacts. func NewCmdBuild(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "build", + Use: constants.CommandBuild, Short: "Builds the artifacts", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - opts.Command = "build" + opts.Command = constants.CommandBuild return runBuild(out) }, } diff --git a/cmd/skaffold/app/cmd/delete.go b/cmd/skaffold/app/cmd/delete.go index 1a06377d619..76558756de8 100644 --- a/cmd/skaffold/app/cmd/delete.go +++ b/cmd/skaffold/app/cmd/delete.go @@ -20,6 +20,7 @@ import ( "context" "io" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -27,11 +28,11 @@ import ( // NewCmdDelete describes the CLI command to delete deployed resources. func NewCmdDelete(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "delete", + Use: constants.CommandDelete, Short: "Delete the deployed resources", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - opts.Command = "delete" + opts.Command = constants.CommandDelete return delete(out) }, } diff --git a/cmd/skaffold/app/cmd/deploy.go b/cmd/skaffold/app/cmd/deploy.go index 71c8d72fdcf..17df204dd8e 100644 --- a/cmd/skaffold/app/cmd/deploy.go +++ b/cmd/skaffold/app/cmd/deploy.go @@ -19,18 +19,19 @@ package cmd import ( "io" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/spf13/cobra" ) // NewCmdDeploy describes the CLI command to deploy artifacts. func NewCmdDeploy(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "deploy", + Use: constants.CommandDeploy, Short: "Deploys the artifacts", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { // Same actions as `skaffold run`, but with pre-built images. - opts.Command = "deploy" + opts.Command = constants.CommandDeploy return run(out) }, } diff --git a/cmd/skaffold/app/cmd/dev.go b/cmd/skaffold/app/cmd/dev.go index 1cecff0920a..3e78a231bf6 100644 --- a/cmd/skaffold/app/cmd/dev.go +++ b/cmd/skaffold/app/cmd/dev.go @@ -23,6 +23,7 @@ import ( "github.com/GoogleContainerTools/skaffold/pkg/skaffold/color" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner" "github.com/pkg/errors" "github.com/rivo/tview" @@ -33,11 +34,11 @@ import ( // NewCmdDev describes the CLI command to run a pipeline in development mode. func NewCmdDev(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "dev", + Use: constants.CommandDev, Short: "Runs a pipeline file in development mode", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - opts.Command = "dev" + opts.Command = constants.CommandDev return dev(out, opts.ExperimentalGUI) }, } diff --git a/cmd/skaffold/app/cmd/run.go b/cmd/skaffold/app/cmd/run.go index 65bd174ea76..666bb0d5da0 100644 --- a/cmd/skaffold/app/cmd/run.go +++ b/cmd/skaffold/app/cmd/run.go @@ -21,6 +21,7 @@ import ( "io" "github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/tips" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -28,11 +29,11 @@ import ( // NewCmdRun describes the CLI command to run a pipeline. func NewCmdRun(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "run", + Use: constants.CommandRun, Short: "Runs a pipeline file", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - opts.Command = "run" + opts.Command = constants.CommandRun err := run(out) if err == nil { tips.PrintForRun(out, opts) diff --git a/integration/run_test.go b/integration/run_test.go index d6d20c82945..045615a40f7 100644 --- a/integration/run_test.go +++ b/integration/run_test.go @@ -32,6 +32,7 @@ import ( "time" "github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd/config" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" kubernetesutil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" "github.com/GoogleContainerTools/skaffold/testutil" @@ -156,7 +157,7 @@ func TestRun(t *testing.T) { ns, deleteNs := setupNamespace(t) defer deleteNs() - runSkaffold(t, "run", test.dir, ns.Name, test.filename, test.env) + runSkaffold(t, constants.CommandRun, test.dir, ns.Name, test.filename, test.env) for _, p := range test.pods { if err := kubernetesutil.WaitForPodReady(context.Background(), client.CoreV1().Pods(ns.Name), p); err != nil { @@ -170,7 +171,7 @@ func TestRun(t *testing.T) { } } - runSkaffold(t, "delete", test.dir, ns.Name, test.filename, test.env) + runSkaffold(t, constants.CommandDelete, test.dir, ns.Name, test.filename, test.env) }) } } @@ -179,7 +180,7 @@ func TestDeploy(t *testing.T) { ns, deleteNs := setupNamespace(t) defer deleteNs() - runSkaffold(t, "deploy", "examples/kustomize", ns.Name, "", nil, "--images", "index.docker.io/library/busybox:1") + runSkaffold(t, constants.CommandDeploy, "examples/kustomize", ns.Name, "", nil, "--images", "index.docker.io/library/busybox:1") depName := "kustomize-test" if err := kubernetesutil.WaitForDeploymentToStabilize(context.Background(), client, ns.Name, depName, 10*time.Minute); err != nil { @@ -195,7 +196,7 @@ func TestDeploy(t *testing.T) { t.Fatalf("Wrong image name in kustomized deployment: %s", dep.Spec.Template.Spec.Containers[0].Image) } - runSkaffold(t, "delete", "examples/kustomize", ns.Name, "", nil) + runSkaffold(t, constants.CommandDelete, "examples/kustomize", ns.Name, "", nil) } func TestDev(t *testing.T) { @@ -205,7 +206,7 @@ func TestDev(t *testing.T) { run(t, "examples/test-dev-job", "touch", "foo") defer run(t, "examples/test-dev-job", "rm", "foo") - go runSkaffoldNoFail("dev", "examples/test-dev-job", ns.Name, "", nil) + go runSkaffoldNoFail(constants.CommandDev, "examples/test-dev-job", ns.Name, "", nil) jobName := "test-dev-job" if err := kubernetesutil.WaitForJobToStabilize(context.Background(), client, ns.Name, jobName, 10*time.Minute); err != nil { @@ -237,7 +238,7 @@ func TestDevSync(t *testing.T) { ns, deleteNs := setupNamespace(t) defer deleteNs() - go runSkaffoldNoFail("dev", "examples/test-file-sync", ns.Name, "", nil) + go runSkaffoldNoFail(constants.CommandDev, "examples/test-file-sync", ns.Name, "", nil) if err := kubernetesutil.WaitForPodReady(context.Background(), client.CoreV1().Pods(ns.Name), "test-file-sync"); err != nil { t.Fatalf("Timed out waiting for pod ready") @@ -310,7 +311,7 @@ func TestFix(t *testing.T) { t.Fatalf("testing error: %v", err) } - runCmd := exec.Command("skaffold", "run", "--namespace", ns.Name, "-f", "-") + runCmd := exec.Command("skaffold", constants.CommandRun, "--namespace", ns.Name, "-f", "-") runCmd.Dir = "testdata/fix" runCmd.Stdin = bytes.NewReader(out) err = util.RunCmd(runCmd) @@ -439,7 +440,7 @@ func TestInit(t *testing.T) { t.Fatalf("running init: %v, output: %s", err, out) } - runCmd := exec.Command("skaffold", "run", "-f", generatedYaml) + runCmd := exec.Command("skaffold", constants.CommandRun, "-f", generatedYaml) runCmd.Dir = test.dir out, err = util.RunCmdOut(runCmd) if err != nil { diff --git a/pkg/skaffold/constants/constants.go b/pkg/skaffold/constants/constants.go index 6ec5d46eb09..65798669c40 100644 --- a/pkg/skaffold/constants/constants.go +++ b/pkg/skaffold/constants/constants.go @@ -60,6 +60,12 @@ const ( DefaultCloudBuildDockerImage = "gcr.io/cloud-builders/docker" DefaultCloudBuildMavenImage = "gcr.io/cloud-builders/mvn" DefaultCloudBuildGradleImage = "gcr.io/cloud-builders/gradle" + + CommandBuild = "build" + CommandDelete = "delete" + CommandDeploy = "deploy" + CommandDev = "dev" + CommandRun = "run" ) var DefaultKubectlManifests = []string{"k8s/*.yaml"} diff --git a/pkg/skaffold/runner/runner.go b/pkg/skaffold/runner/runner.go index d9ff0c6f2b0..c562e34b0fc 100644 --- a/pkg/skaffold/runner/runner.go +++ b/pkg/skaffold/runner/runner.go @@ -33,6 +33,7 @@ import ( "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/color" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes" kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context" @@ -93,7 +94,7 @@ func NewForConfig(opts *config.SkaffoldOptions, cfg *latest.SkaffoldPipeline) (* return nil, errors.Wrap(err, "parsing test config") } - forceDeploy := opts.Command == "dev" || opts.ForceDeploy + forceDeploy := opts.Command == constants.CommandDev || opts.ForceDeploy deployer, err := getDeployer(&cfg.Deploy, kubeContext, opts.Namespace, forceDeploy, defaultRepo) if err != nil { return nil, errors.Wrap(err, "parsing deploy config") diff --git a/pkg/skaffold/schema/profiles_test.go b/pkg/skaffold/schema/profiles_test.go index 526d0662358..e85945a15c4 100644 --- a/pkg/skaffold/schema/profiles_test.go +++ b/pkg/skaffold/schema/profiles_test.go @@ -21,6 +21,7 @@ import ( "testing" cfg "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" "github.com/GoogleContainerTools/skaffold/testutil" yamlpatch "github.com/krishicks/yaml-patch" @@ -228,7 +229,7 @@ func TestActivatedProfiles(t *testing.T) { { description: "Selected on the command line", opts: &cfg.SkaffoldOptions{ - Command: "dev", + Command: constants.CommandDev, Profiles: []string{"activated", "also-activated"}, }, profiles: []latest.Profile{ @@ -240,12 +241,12 @@ func TestActivatedProfiles(t *testing.T) { }, { description: "Auto-activated by command", opts: &cfg.SkaffoldOptions{ - Command: "dev", + Command: constants.CommandDev, }, profiles: []latest.Profile{ - {Name: "run-profile", Activation: []latest.Activation{{Command: "run"}}}, - {Name: "dev-profile", Activation: []latest.Activation{{Command: "dev"}}}, - {Name: "non-run-profile", Activation: []latest.Activation{{Command: "!run"}}}, + {Name: "run-profile", Activation: []latest.Activation{{Command: constants.CommandRun}}}, + {Name: "dev-profile", Activation: []latest.Activation{{Command: constants.CommandDev}}}, + {Name: "non-run-profile", Activation: []latest.Activation{{Command: "!" + constants.CommandRun}}}, }, expected: []string{"dev-profile", "non-run-profile"}, }, { @@ -276,21 +277,21 @@ func TestActivatedProfiles(t *testing.T) { }, { description: "AND between activation criteria", opts: &cfg.SkaffoldOptions{ - Command: "dev", + Command: constants.CommandDev, }, profiles: []latest.Profile{ { Name: "activated", Activation: []latest.Activation{{ Env: "KEY=VALUE", KubeContext: "prod-context", - Command: "dev", + Command: constants.CommandDev, }}, }, { Name: "not-activated", Activation: []latest.Activation{{ Env: "KEY=VALUE", KubeContext: "prod-context", - Command: "build", + Command: constants.CommandBuild, }}, }, }, @@ -298,14 +299,14 @@ func TestActivatedProfiles(t *testing.T) { }, { description: "OR between activations", opts: &cfg.SkaffoldOptions{ - Command: "dev", + Command: constants.CommandDev, }, profiles: []latest.Profile{ { Name: "activated", Activation: []latest.Activation{{ - Command: "run", + Command: constants.CommandRun, }, { - Command: "dev", + Command: constants.CommandDev, }}, }, },