Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggest-builders command #152

Merged
merged 4 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,18 @@ func testAcceptance(t *testing.T, when spec.G, it spec.S) {
})
})

when("pack suggest-builders", func() {
it("displays suggested builders", func() {
cmd := packCmd("suggest-builders")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("suggest-builders command failed: %s: %s", output, err)
}
h.AssertContains(t, string(output), "Suggested builders:")
h.AssertContains(t, string(output), "cloudfoundry/cnb:bionic")
})
})

when("pack set-default-builder", func() {
it("sets the default-stack-id in ~/.pack/config.toml", func() {
cmd := packCmd("set-default-builder", "some/builder")
Expand Down
1 change: 1 addition & 0 deletions cmd/pack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func main() {
rootCmd.AddCommand(commands.SetRunImagesMirrors(&logger))
rootCmd.AddCommand(commands.InspectBuilder(&logger, &cfg, &packClient))
rootCmd.AddCommand(commands.SetDefaultBuilder(&logger))
rootCmd.AddCommand(commands.SuggestBuilders(&logger))

rootCmd.AddCommand(commands.Version(&logger, Version))

Expand Down
47 changes: 0 additions & 47 deletions commands/build.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package commands

import (
"fmt"
"math/rand"
"text/tabwriter"
"time"

"github.com/spf13/cobra"

"github.com/buildpack/pack"
Expand All @@ -14,25 +9,6 @@ import (
"github.com/buildpack/pack/style"
)

type suggestedBuilder struct {
name string
image string
info string
}

var suggestedBuilders = [][]suggestedBuilder{
{
{"Cloud Foundry", "cloudfoundry/cnb:bionic", "small base image with Java & Node.js"},
{"Cloud Foundry", "cloudfoundry/cnb:cflinuxfs3", "larger base image with Java, Node.js & Python"},
},
{
{"Heroku", "heroku/buildpacks", "heroku-18 base image with official Heroku buildpacks"},
},
}

func init() {
rand.Seed(time.Now().UnixNano())
}

func Build(logger *logging.Logger, fetcher pack.ImageFetcher) *cobra.Command {
var buildFlags pack.BuildFlags
Expand Down Expand Up @@ -82,29 +58,6 @@ func Build(logger *logging.Logger, fetcher pack.ImageFetcher) *cobra.Command {
return cmd
}

func suggestSettingBuilder(logger *logging.Logger) {
logger.Info("Please select a default builder with:\n")
logger.Info("\tpack set-default-builder <builder image>\n")
suggestBuilders(logger)
}

func suggestBuilders(logger *logging.Logger) {
logger.Info("Suggested builders:\n")
tw := tabwriter.NewWriter(logger.RawWriter(), 10, 10, 5, ' ', tabwriter.TabIndent)
for len(suggestedBuilders) > 0 {
n := rand.Intn(len(suggestedBuilders))
builders := suggestedBuilders[n]
for _, builder := range builders {
tw.Write([]byte(fmt.Sprintf("\t%s:\t%s\t%s\t\n", builder.name, style.Symbol(builder.image), builder.info)))
}
suggestedBuilders = append(suggestedBuilders[:n], suggestedBuilders[n+1:]...)
}
tw.Flush()
logger.Info("")
logger.Tip("Learn more about a specific builder with:\n")
logger.Info("\tpack inspect-builder [builder image]")
}

func buildCommandFlags(cmd *cobra.Command, buildFlags *pack.BuildFlags) {
cmd.Flags().StringVarP(&buildFlags.AppDir, "path", "p", "", "Path to app dir (defaults to current working directory)")
cmd.Flags().StringVar(&buildFlags.Builder, "builder", "", "Builder (defaults to builder configured by 'set-default-builder')")
Expand Down
45 changes: 45 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package commands
import (
"context"
"fmt"
"github.com/buildpack/pack/style"
"math/rand"
"os"
"os/signal"
"syscall"
"text/tabwriter"

"github.com/docker/docker/client"
"github.com/spf13/cobra"
Expand All @@ -20,6 +23,23 @@ type PackClient interface {
Rebase(context.Context, pack.RebaseOptions) error
}


type suggestedBuilder struct {
name string
image string
info string
}

var suggestedBuilders = [][]suggestedBuilder{
{
{"Cloud Foundry", "cloudfoundry/cnb:bionic", "small base image with Java & Node.js"},
{"Cloud Foundry", "cloudfoundry/cnb:cflinuxfs3", "larger base image with Java, Node.js & Python"},
},
{
{"Heroku", "heroku/buildpacks", "heroku-18 base image with official Heroku buildpacks"},
},
}

func AddHelpFlag(cmd *cobra.Command, commandName string) {
cmd.Flags().BoolP("help", "h", false, fmt.Sprintf("Help for '%s'", commandName))
}
Expand Down Expand Up @@ -59,3 +79,28 @@ func createCancellableContext() context.Context {
func dockerClient() (*client.Client, error){
return client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.38"))
}



func suggestSettingBuilder(logger *logging.Logger) {
logger.Info("Please select a default builder with:\n")
logger.Info("\tpack set-default-builder <builder image>\n")
suggestBuilders(logger)
}

func suggestBuilders(logger *logging.Logger) {
logger.Info("Suggested builders:\n")

tw := tabwriter.NewWriter(logger.RawWriter(), 10, 10, 5, ' ', tabwriter.TabIndent)
for _, i := range rand.Perm(len(suggestedBuilders)) {
builders := suggestedBuilders[i]
for _, builder := range builders {
_, _ = tw.Write([]byte(fmt.Sprintf("\t%s:\t%s\t%s\t\n", builder.name, style.Symbol(builder.image), builder.info)))
}
}
_ = tw.Flush()

logger.Info("")
logger.Tip("Learn more about a specific builder with:\n")
logger.Info("\tpack inspect-builder [builder image]")
}
22 changes: 22 additions & 0 deletions commands/suggest_builders.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package commands

import (
"github.com/buildpack/pack/logging"
"github.com/spf13/cobra"
)

func SuggestBuilders(logger *logging.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "suggest-builders",
Short: "Display list of recommended builders",
Args: cobra.NoArgs,
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
suggestBuilders(logger)

return nil
}),
}

AddHelpFlag(cmd, "suggest-builders")
return cmd
}
41 changes: 41 additions & 0 deletions commands/suggest_builders_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package commands_test

import (
"bytes"
"testing"

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

"github.com/buildpack/pack/commands"
"github.com/buildpack/pack/logging"
h "github.com/buildpack/pack/testhelpers"
)

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

func testSuggestBuildersCommand(t *testing.T, when spec.G, it spec.S) {

var (
command *cobra.Command
logger *logging.Logger
outBuf bytes.Buffer
)

it.Before(func() {
logger = logging.NewLogger(&outBuf, &outBuf, false, false)
command = commands.SuggestBuilders(logger)
})

when("#SuggestBuilders", func() {
it("display suggested builders", func() {
command.SetArgs([]string{})
h.AssertNil(t, command.Execute())
h.AssertContains(t, outBuf.String(), "Suggested builders:")
h.AssertContains(t, outBuf.String(), "cloudfoundry/cnb:bionic")
})
})
}