Skip to content

Commit

Permalink
Move command names to code constants
Browse files Browse the repository at this point in the history
Signed-off-by: Cornelius Weig <cornelius.weig@tngtech.com>
  • Loading branch information
Cornelius Weig committed Feb 1, 2019
1 parent a272f39 commit 322d158
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 30 deletions.
5 changes: 3 additions & 2 deletions cmd/skaffold/app/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -35,11 +36,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)
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/skaffold/app/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ import (
"context"
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

// 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)
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/skaffold/app/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/skaffold/app/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -29,11 +30,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)
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/skaffold/app/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ 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"
)

// 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)
Expand Down
17 changes: 9 additions & 8 deletions integration/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -146,7 +147,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 {
Expand All @@ -160,7 +161,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)
})
}
}
Expand All @@ -169,7 +170,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 {
Expand All @@ -185,7 +186,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) {
Expand All @@ -195,7 +196,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 {
Expand Down Expand Up @@ -227,7 +228,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")
Expand Down Expand Up @@ -300,7 +301,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"
runCmd.Stdin = bytes.NewReader(out)
err = util.RunCmd(runCmd)
Expand Down Expand Up @@ -429,7 +430,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 {
Expand Down
6 changes: 6 additions & 0 deletions pkg/skaffold/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ const (

// A regex matching valid repository names (https://github.com/docker/distribution/blob/master/reference/reference.go)
RepositoryComponentRegex string = `^[a-z\d]+(?:(?:[_.]|__|-+)[a-z\d]+)*$`

CommandBuild = "build"
CommandDelete = "delete"
CommandDeploy = "deploy"
CommandDev = "dev"
CommandRun = "run"
)

var DefaultKubectlManifests = []string{"k8s/*.yaml"}
Expand Down
3 changes: 2 additions & 1 deletion pkg/skaffold/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/local"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"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"
Expand Down Expand Up @@ -92,7 +93,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")
Expand Down
23 changes: 12 additions & 11 deletions pkg/skaffold/schema/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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"},
}, {
Expand Down Expand Up @@ -276,36 +277,36 @@ 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,
}},
},
},
expected: []string{"activated"},
}, {
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,
}},
},
},
Expand Down

0 comments on commit 322d158

Please sign in to comment.