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 22, 2019
1 parent bd686dc commit 70c2006
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 33 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/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -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)
},
}
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 @@ -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"
Expand All @@ -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)
},
}
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
3 changes: 2 additions & 1 deletion integration/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os/exec"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

Expand Down Expand Up @@ -55,7 +56,7 @@ func TestBuild(t *testing.T) {

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
buildCmd := exec.Command("skaffold", append([]string{"build"}, test.args...)...)
buildCmd := exec.Command("skaffold", append([]string{constants.CommandBuild}, test.args...)...)
buildCmd.Dir = test.dir

out, err := util.RunCmdOut(buildCmd)
Expand Down
11 changes: 5 additions & 6 deletions integration/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ package integration

import (
"context"
"time"

meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"testing"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
kubernetesutil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

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 @@ -49,5 +48,5 @@ 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)
}
3 changes: 2 additions & 1 deletion integration/dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
kubernetesutil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
Expand All @@ -36,7 +37,7 @@ func TestDev(t *testing.T) {
defer Run(t, "examples/test-dev-job", "rm", "foo")

cancel := make(chan bool)
go RunSkaffoldNoFail(cancel, "dev", "examples/test-dev-job", ns.Name, "", nil)
go RunSkaffoldNoFail(cancel, constants.CommandDev, "examples/test-dev-job", ns.Name, "", nil)
defer func() { cancel <- true }()

jobName := "test-dev-job"
Expand Down
3 changes: 2 additions & 1 deletion integration/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os/exec"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

Expand All @@ -37,7 +38,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)

Expand Down
5 changes: 3 additions & 2 deletions integration/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
kubernetesutil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
)

Expand Down Expand Up @@ -113,7 +114,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 @@ -127,7 +128,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)
})
}
}
6 changes: 6 additions & 0 deletions pkg/skaffold/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ const (
DefaultCloudBuildMavenImage = "gcr.io/cloud-builders/mvn"
DefaultCloudBuildGradleImage = "gcr.io/cloud-builders/gradle"

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

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

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 @@ -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"
Expand Down Expand Up @@ -94,7 +95,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 @@ -22,6 +22,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 @@ -270,7 +271,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 @@ -282,12 +283,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 @@ -318,36 +319,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 70c2006

Please sign in to comment.