Skip to content

Commit

Permalink
adds pack set-default-stack command
Browse files Browse the repository at this point in the history
[#38]
  • Loading branch information
ekcasey committed Oct 11, 2018
1 parent 0708d2a commit 1eb3543
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
34 changes: 34 additions & 0 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,40 @@ func testPack(t *testing.T, when spec.G, it spec.S) {
})
}, spec.Parallel(), spec.Report(report.Terminal{}))

when("pack update-stack", func() {
type config struct {
DefaultStackID string `toml:"default-stack-id"`
}

it.Before(func() {
cmd := exec.Command(pack, "add-stack", "my.custom.stack", "--run-image", "my-org/run", "--build-image", "my-org/build")
cmd.Env = append(os.Environ(), "HOME="+homeDir)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("add-stack command failed: %s: %s", output, err)
}
})

it("sets the default-stack-id in ~/.pack/config.toml", func() {
cmd := exec.Command(
pack,
"set-default-stack",
"my.custom.stack",
)
cmd.Env = append(os.Environ(), "HOME="+homeDir)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("set-default-stack command failed: %s: %s", output, err)
}
assertEq(t, string(output), "my.custom.stack is now the default stack\n")

var config config
_, err = toml.DecodeFile(filepath.Join(homeDir, ".pack", "config.toml"), &config)
assertNil(t, err)
assertEq(t, config.DefaultStackID, "my.custom.stack")
})
}, spec.Parallel(), spec.Report(report.Terminal{}))

when("pack delete-stack", func() {
type config struct {
Stacks []struct {
Expand Down
20 changes: 20 additions & 0 deletions cmd/pack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {
addStackCommand,
updateStackCommand,
deleteStackCommand,
setDefaultStackCommand,
} {
rootCmd.AddCommand(f())
}
Expand Down Expand Up @@ -160,6 +161,25 @@ func addStackCommand() *cobra.Command {
return addStackCommand
}

func setDefaultStackCommand() *cobra.Command {
return &cobra.Command{
Use: "set-default-stack <stack-name>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.New(filepath.Join(os.Getenv("HOME"), ".pack"))
if err != nil {
return err
}
err = cfg.SetDefaultStack(args[0])
if err != nil {
return err
}
fmt.Printf("%s is now the default stack\n", args[0])
return nil
},
}
}

func updateStackCommand() *cobra.Command {
flags := struct {
BuildImages []string
Expand Down
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ func (c *Config) Delete(stackID string) error {
}
return fmt.Errorf(`"%s" does not exist. Please pass in a valid stack ID.`, stackID)
}
func (c *Config) SetDefaultStack(stackID string) error {
for _, s := range c.Stacks {
if s.ID == stackID {
c.DefaultStackID = stackID
return c.save()
}
}
return fmt.Errorf(`"%s" does not exist. Please pass in a valid stack ID.`, stackID)
}

func ImageByRegistry(registry string, images []string) (string, error) {
if len(images) == 0 {
Expand Down
44 changes: 44 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,40 @@ default-stack-id = "my.stack"
})
})

when("Config#SetDefaultStack", func() {
var subject *config.Config
it.Before(func() {
assertNil(t, ioutil.WriteFile(filepath.Join(tmpDir, "config.toml"), []byte(`
default-stack-id = "old.default.stack"
[[stacks]]
id = "some.stack"
`), 0666))
var err error
subject, err = config.New(tmpDir)
assertNil(t, err)
})

when("the stack exists", func() {
it("sets the default-stack-id", func() {
err := subject.SetDefaultStack("some.stack")
assertNil(t, err)
b, err := ioutil.ReadFile(filepath.Join(tmpDir, "config.toml"))
assertNil(t, err)
assertContains(t, string(b), `default-stack-id = "some.stack"`)
})
})

when("the stack doesn't exist", func() {
it("returns an error and leaves the original default", func() {
err := subject.SetDefaultStack("some.missing.stack")
assertError(t, err, `"some.missing.stack" does not exist. Please pass in a valid stack ID.`)
b, err := ioutil.ReadFile(filepath.Join(tmpDir, "config.toml"))
assertNil(t, err)
assertContains(t, string(b), `default-stack-id = "old.default.stack"`)
})
})
})

when("Config#Add", func() {
var subject *config.Config
it.Before(func() {
Expand Down Expand Up @@ -475,3 +509,13 @@ func assertEq(t *testing.T, actual, expected interface{}) {
t.Fatal(diff)
}
}

func assertError(t *testing.T, actual error, expected string) {
t.Helper()
if actual == nil {
t.Fatalf("Expected an error but got nil")
}
if actual.Error() != expected {
t.Fatalf(`Expected error to equal "%s", got "%s"`, expected, actual.Error())
}
}

0 comments on commit 1eb3543

Please sign in to comment.