Skip to content

Commit

Permalink
add pack stack subcommand
Browse files Browse the repository at this point in the history
Signed-off-by: anshlykov <andrew.shlykov@ya.ru>
  • Loading branch information
anshlykov committed Nov 5, 2020
1 parent 80ed306 commit 9cb8cb3
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 63 deletions.
3 changes: 3 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/buildpacks/pack/buildpackage"
"github.com/buildpacks/pack/internal/builder/writer"
"github.com/buildpacks/pack/internal/commands"
"github.com/buildpacks/pack/internal/commands/stack"
"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/logging"
)
Expand Down Expand Up @@ -95,6 +96,8 @@ func NewPackCommand(logger ConfigurableLogger) (*cobra.Command, error) {

rootCmd.AddCommand(commands.CompletionCommand(logger))

rootCmd.AddCommand(stack.NewStackCmd(logger))

rootCmd.Version = pack.Version
rootCmd.SetVersionTemplate(`{{.Version}}{{"\n"}}`)
rootCmd.SetOut(logging.GetWriterForLevel(logger, logging.InfoLevel))
Expand Down
18 changes: 18 additions & 0 deletions internal/commands/stack/stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package stack

import (
"github.com/spf13/cobra"

"github.com/buildpacks/pack/logging"
)

func NewStackCmd(logger logging.Logger) *cobra.Command {
command := cobra.Command{
Use: "stack",
Short: "Displays stack information",
RunE: nil,
}

command.AddCommand(newSuggestCmd(logger))
return &command
}
82 changes: 82 additions & 0 deletions internal/commands/stack/suggest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package stack

import (
"bytes"
"html/template"
"sort"

"github.com/spf13/cobra"

"github.com/buildpacks/pack/logging"
)

type suggestedStack struct {
ID string
Description string
Maintainer string
BuildImage string
RunImage string
}

var suggestedStacks = []suggestedStack{
{
ID: "heroku-18",
Description: "The official Heroku stack based on Ubuntu 18.04",
Maintainer: "Heroku",
BuildImage: "heroku/pack:18-build",
RunImage: "heroku/pack:18",
},
{
ID: "io.buildpacks.stacks.bionic",
Description: "A minimal Paketo stack based on Ubuntu 18.04",
Maintainer: "Paketo Project",
BuildImage: "paketobuildpacks/build:base-cnb",
RunImage: "paketobuildpacks/run:base-cnb",
},
{
ID: "io.buildpacks.stacks.bionic",
Description: "A large Paketo stack based on Ubuntu 18.04",
Maintainer: "Paketo Project",
BuildImage: "paketobuildpacks/build:full-cnb",
RunImage: "paketobuildpacks/run:full-cnb",
},
{
ID: "io.paketo.stacks.tiny",
Description: "A tiny Paketo stack based on Ubuntu 18.04, similar to distroless",
Maintainer: "Paketo Project",
BuildImage: "paketobuildpacks/build:tiny-cnb",
RunImage: "paketobuildpacks/run:tiny-cnb",
},
}

func newSuggestCmd(logger logging.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "suggest",
Args: cobra.NoArgs,
Short: "Display list of recommended stacks",
Example: "pack stacks suggest",
Run: func(*cobra.Command, []string) {
Suggest(logger)
},
}

return cmd
}

func Suggest(log logging.Logger) {
sort.Slice(suggestedStacks, func(i, j int) bool { return suggestedStacks[i].ID < suggestedStacks[j].ID })
tmpl := template.Must(template.New("").Parse(`Stacks maintained by the community:
{{- range . }}
Stack ID: {{ .ID }}
Description: {{ .Description }}
Maintainer: {{ .Maintainer }}
Build Image: {{ .BuildImage }}
Run Image: {{ .RunImage }}
{{- end }}
`))

buf := &bytes.Buffer{}
tmpl.Execute(buf, suggestedStacks)
log.Info(buf.String())
}
61 changes: 61 additions & 0 deletions internal/commands/stack/suggest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package stack

import (
"bytes"
"testing"

"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/spf13/cobra"

"github.com/buildpacks/pack/internal/logging"
h "github.com/buildpacks/pack/testhelpers"
)

func TestSuggestStacksCommand(t *testing.T) {
spec.Run(t, "Commands", testSuggestStacksCommand, spec.Parallel(), spec.Report(report.Terminal{}))
}

func testSuggestStacksCommand(t *testing.T, when spec.G, it spec.S) {
var (
command *cobra.Command
outBuf bytes.Buffer
)

it.Before(func() {
command = newSuggestCmd(logging.NewLogWithWriters(&outBuf, &outBuf))
})

when("#SuggestStacks", func() {
it("displays stack information", func() {
command.SetArgs([]string{})
h.AssertNil(t, command.Execute())
h.AssertEq(t, outBuf.String(), `Stacks maintained by the community:
Stack ID: heroku-18
Description: The official Heroku stack based on Ubuntu 18.04
Maintainer: Heroku
Build Image: heroku/pack:18-build
Run Image: heroku/pack:18
Stack ID: io.buildpacks.stacks.bionic
Description: A minimal Paketo stack based on Ubuntu 18.04
Maintainer: Paketo Project
Build Image: paketobuildpacks/build:base-cnb
Run Image: paketobuildpacks/run:base-cnb
Stack ID: io.buildpacks.stacks.bionic
Description: A large Paketo stack based on Ubuntu 18.04
Maintainer: Paketo Project
Build Image: paketobuildpacks/build:full-cnb
Run Image: paketobuildpacks/run:full-cnb
Stack ID: io.paketo.stacks.tiny
Description: A tiny Paketo stack based on Ubuntu 18.04, similar to distroless
Maintainer: Paketo Project
Build Image: paketobuildpacks/build:tiny-cnb
Run Image: paketobuildpacks/run:tiny-cnb
`)
})
})
}
67 changes: 5 additions & 62 deletions internal/commands/suggest_stacks.go
Original file line number Diff line number Diff line change
@@ -1,83 +1,26 @@
package commands

import (
"bytes"
"html/template"
"sort"

"github.com/spf13/cobra"

"github.com/buildpacks/pack/internal/commands/stack"

"github.com/buildpacks/pack/logging"
)

type suggestedStack struct {
ID string
Description string
Maintainer string
BuildImage string
RunImage string
}

var suggestedStacks = []suggestedStack{
{
ID: "heroku-18",
Description: "The official Heroku stack based on Ubuntu 18.04",
Maintainer: "Heroku",
BuildImage: "heroku/pack:18-build",
RunImage: "heroku/pack:18",
},
{
ID: "io.buildpacks.stacks.bionic",
Description: "A minimal Paketo stack based on Ubuntu 18.04",
Maintainer: "Paketo Project",
BuildImage: "paketobuildpacks/build:base-cnb",
RunImage: "paketobuildpacks/run:base-cnb",
},
{
ID: "io.buildpacks.stacks.bionic",
Description: "A large Paketo stack based on Ubuntu 18.04",
Maintainer: "Paketo Project",
BuildImage: "paketobuildpacks/build:full-cnb",
RunImage: "paketobuildpacks/run:full-cnb",
},
{
ID: "io.paketo.stacks.tiny",
Description: "A tiny Paketo stack based on Ubuntu 18.04, similar to distroless",
Maintainer: "Paketo Project",
BuildImage: "paketobuildpacks/build:tiny-cnb",
RunImage: "paketobuildpacks/run:tiny-cnb",
},
}

func SuggestStacks(logger logging.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "suggest-stacks",
Args: cobra.NoArgs,
Short: "Display list of recommended stacks",
Example: "pack suggest-stacks",
Run: func(*cobra.Command, []string) {
suggestStacks(logger)
logger.Warn("Command 'pack suggest-stacks' has been deprecated, please use 'pack stack suggest' instead")
stack.Suggest(logger)
},
Hidden: true,
}

AddHelpFlag(cmd, "suggest-stacks")
return cmd
}

func suggestStacks(log logging.Logger) {
sort.Slice(suggestedStacks, func(i, j int) bool { return suggestedStacks[i].ID < suggestedStacks[j].ID })
tmpl := template.Must(template.New("").Parse(`Stacks maintained by the community:
{{- range . }}
Stack ID: {{ .ID }}
Description: {{ .Description }}
Maintainer: {{ .Maintainer }}
Build Image: {{ .BuildImage }}
Run Image: {{ .RunImage }}
{{- end }}
`))

buf := &bytes.Buffer{}
tmpl.Execute(buf, suggestedStacks)
log.Info(buf.String())
}
3 changes: 2 additions & 1 deletion internal/commands/suggest_stacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func testSuggestStacksCommand(t *testing.T, when spec.G, it spec.S) {
it("displays stack information", func() {
command.SetArgs([]string{})
h.AssertNil(t, command.Execute())
h.AssertEq(t, outBuf.String(), `Stacks maintained by the community:
h.AssertEq(t, outBuf.String(), "\x1b\x5b\x33\x33\x3b\x31\x6dWarning: \x1b\x5b\x30\x6dCommand 'pack suggest-stacks' has been deprecated, please use 'pack stack suggest' instead\n"+
`Stacks maintained by the community:
Stack ID: heroku-18
Description: The official Heroku stack based on Ubuntu 18.04
Expand Down

0 comments on commit 9cb8cb3

Please sign in to comment.