Skip to content

Commit

Permalink
Merged suggest stacks and added new flavor logging
Browse files Browse the repository at this point in the history
Signed-off-by: John Murphy <johnmurphy@salesforce.com>
  • Loading branch information
John Murphy committed May 28, 2019
1 parent 5823783 commit 6bcc8ef
Show file tree
Hide file tree
Showing 29 changed files with 444 additions and 275 deletions.
11 changes: 3 additions & 8 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"testing"
"time"

"github.com/buildpack/lifecycle/metadata"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
Expand All @@ -28,6 +27,7 @@ import (
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

"github.com/buildpack/lifecycle/metadata"
"github.com/buildpack/pack/archive"
"github.com/buildpack/pack/cache"
"github.com/buildpack/pack/lifecycle"
Expand Down Expand Up @@ -440,9 +440,7 @@ func testAcceptance(t *testing.T, when spec.G, it spec.S) {
cmd := packCmd("build", repoName, "-p", filepath.Join("testdata", "mock_app"))
output, err := h.RunE(cmd)
h.AssertNotNil(t, err)
h.AssertContains(t, output, `Please select a default builder with:
pack set-default-builder <builder image>`)
h.AssertContains(t, output, `Please select a default builder with:`)
h.AssertMatch(t, output, `Cloud Foundry:\s+'cloudfoundry/cnb:bionic'`)
h.AssertMatch(t, output, `Cloud Foundry:\s+'cloudfoundry/cnb:cflinuxfs3'`)
h.AssertMatch(t, output, `Heroku:\s+'heroku/buildpacks'`)
Expand Down Expand Up @@ -495,9 +493,7 @@ func testAcceptance(t *testing.T, when spec.G, it spec.S) {
cmd := packCmd("run", "-p", filepath.Join("testdata", "mock_app"))
output, err := h.RunE(cmd)
h.AssertNotNil(t, err)
h.AssertContains(t, output, `Please select a default builder with:
pack set-default-builder <builder image>`)
h.AssertContains(t, output, `Please select a default builder with:`)
h.AssertMatch(t, output, `Cloud Foundry:\s+'cloudfoundry/cnb:bionic'`)
h.AssertMatch(t, output, `Cloud Foundry:\s+'cloudfoundry/cnb:cflinuxfs3'`)
h.AssertMatch(t, output, `Heroku:\s+'heroku/buildpacks'`)
Expand Down Expand Up @@ -720,7 +716,6 @@ func testAcceptance(t *testing.T, when spec.G, it spec.S) {

cmd = packCmd("inspect-builder", builder)
output = h.Run(t, cmd)

expected, err := ioutil.ReadFile(filepath.Join("testdata", "inspect_builder_output.txt"))
h.AssertNil(t, err)
h.AssertEq(t, output, fmt.Sprintf(string(expected), builder, lifecycleVersion, runImageMirror, lifecycleVersion, runImageMirror))
Expand Down
32 changes: 16 additions & 16 deletions acceptance/testdata/inspect_builder_output.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Inspecting builder: '%s'

Remote
------
Remote
------

Stack: pack.test.stack
Stack: pack.test.stack

Lifecycle Version: %s
Lifecycle Version: %-6s

Run Images:
Run Images:
some-registry.com/pack-test/run1 (user-configured)
pack-test/run
pack-test/run
%s

Buildpacks:
Expand All @@ -18,21 +18,21 @@ Buildpacks:
read/env read-env-version false
noop.buildpack noop.buildpack.version true

Detection Order:
Group #1:
Detection Order:
Group #1:
simple/layers@simple-layers-version
read/env@read-env-version (optional)

Local
-----
Local
-----

Stack: pack.test.stack
Stack: pack.test.stack

Lifecycle Version: %s
Lifecycle Version: %-6s

Run Images:
Run Images:
some-registry.com/pack-test/run1 (user-configured)
pack-test/run
pack-test/run
%s

Buildpacks:
Expand All @@ -41,7 +41,7 @@ Buildpacks:
read/env read-env-version false
noop.buildpack noop.buildpack.version true

Detection Order:
Group #1:
Detection Order:
Group #1:
simple/layers@simple-layers-version
read/env@read-env-version (optional)
4 changes: 2 additions & 2 deletions app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (i *Image) Run(ctx context.Context, docker *client.Client, ports []string)
ctx,
docker,
ctr.ID,
logging.NewWriter(i.Logger.Debug),
logging.NewWriter(i.Logger.Error),
i.Logger.Writer(),
logging.GetErrorWriter(i.Logger),
); err != nil {
return errors.Wrap(err, "run container")
}
Expand Down
4 changes: 2 additions & 2 deletions build/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func (p *Phase) Run(context context.Context) error {
context,
p.docker,
p.ctr.ID,
logging.NewWriter(p.logger.Debug).WithPrefix(p.name),
logging.NewWriter(p.logger.Error).WithPrefix(p.name),
logging.NewPrefixWriter(p.logger.Writer()).WithPrefix(p.name),
logging.NewPrefixWriter(logging.GetErrorWriter(p.logger)).WithPrefix(p.name),
)
}

Expand Down
43 changes: 30 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package pack

import (
"os"
"path/filepath"

"github.com/docker/docker/client"

"github.com/buildpack/pack/lifecycle"

"github.com/buildpack/pack/build"
"github.com/buildpack/pack/buildpack"
"github.com/buildpack/pack/config"
"github.com/buildpack/pack/image"
"github.com/buildpack/pack/lifecycle"
"github.com/buildpack/pack/logging"
)

Expand Down Expand Up @@ -44,21 +44,38 @@ func NewClient(
}
}

func DefaultClient(config *config.Config, logger logging.Logger) (*Client, error) {
type ClientOption func(c *Client)

// WithLogger supply your own logger.
func WithLogger(l logging.Logger) ClientOption {
return func(c *Client) {
c.logger = l
}
}

func DefaultClient(config *config.Config, opts ...ClientOption) (*Client, error) {
dockerClient, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.38"))
if err != nil {
return nil, err
}

downloader := NewDownloader(logger, filepath.Join(config.Path(), "download-cache"))
var client Client

return &Client{
config: config,
logger: logger,
imageFetcher: image.NewFetcher(logger, dockerClient),
buildpackFetcher: buildpack.NewFetcher(downloader),
lifecycleFetcher: lifecycle.NewFetcher(downloader),
lifecycle: build.NewLifecycle(dockerClient, logger),
docker: dockerClient,
}, nil
for _, opt := range opts {
opt(&client)
}

if client.logger == nil {
client.logger = logging.New(os.Stderr)
}

downloader := NewDownloader(client.logger, filepath.Join(config.Path(), "download-cache"))
client.config = config
client.imageFetcher = image.NewFetcher(client.logger, dockerClient)
client.buildpackFetcher = buildpack.NewFetcher(downloader)
client.lifecycleFetcher = lifecycle.NewFetcher(downloader)
client.lifecycle = build.NewLifecycle(dockerClient, client.logger)
client.docker = dockerClient

return &client, nil
}
20 changes: 9 additions & 11 deletions cmd/pack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package main
import (
"os"

"github.com/apex/log"
"github.com/fatih/color"
"github.com/spf13/cobra"

"github.com/buildpack/pack"
"github.com/buildpack/pack/commands"
"github.com/buildpack/pack/config"
clilogger "github.com/buildpack/pack/internal/logging"
"github.com/buildpack/pack/logging"
"github.com/spf13/cobra"
)

var (
Expand All @@ -20,24 +21,21 @@ var (

func main() {
// create logger with defaults
handler := clilogger.NewLogHandler(os.Stderr)
logger := clilogger.NewLogWithWriter(handler)
logger := clilogger.NewLogWithWriters()

cobra.EnableCommandSorting = false
rootCmd := &cobra.Command{
Use: "pack",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if fs := cmd.Flags(); fs != nil {
if flag, err := fs.GetBool("no-color"); err != nil {
handler.NoColor = flag
color.NoColor = flag
}
if flag, err := fs.GetBool("quiet"); err != nil {
if flag {
logger.Level = log.ErrorLevel
}
logger.WantQuiet(flag)
}
if flag, err := fs.GetBool("timestamps"); err != nil {
handler.WantTime = flag
logger.WantTime(flag)
}
}

Expand All @@ -47,7 +45,7 @@ func main() {
}
rootCmd.PersistentFlags().Bool("no-color", false, "Disable color output")
rootCmd.PersistentFlags().Bool("timestamps", false, "Enable timestamps in output")
rootCmd.PersistentFlags().Bool("quiet", false, "Show less output")
rootCmd.PersistentFlags().BoolP("quiet", "q", false, "Show less output")
commands.AddHelpFlag(rootCmd, "pack")

rootCmd.AddCommand(commands.Build(logger, &cfg, &packClient))
Expand Down Expand Up @@ -80,7 +78,7 @@ func initConfig(logger logging.Logger) config.Config {
}

func initClient(cfg *config.Config, logger logging.Logger) pack.Client {
client, err := pack.DefaultClient(cfg, logger)
client, err := pack.DefaultClient(cfg, pack.WithLogger(logger))
if err != nil {
exitError(logger, err)
}
Expand Down
2 changes: 1 addition & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type BuildFlags struct {
Buildpacks []string
}

func Build(logger logging.LoggerWithWriter, config *config.Config, packClient *pack.Client) *cobra.Command {
func Build(logger logging.Logger, config *config.Config, packClient *pack.Client) *cobra.Command {
var flags BuildFlags
ctx := createCancellableContext()

Expand Down
23 changes: 10 additions & 13 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,17 @@ func createCancellableContext() context.Context {
return ctx
}

func suggestSettingBuilder(logger logging.LoggerWithWriter, client PackClient) {
out := logger.Writer()
_, _ = fmt.Fprintln(out, "Please select a default builder with:")
_, _ = fmt.Fprintln(out)
_, _ = fmt.Fprintln(out,"\tpack set-default-builder <builder image>")
_, _ = fmt.Fprintln(out)
func suggestSettingBuilder(logger logging.Logger, client PackClient) {
logger.Info("Please select a default builder with:")
logger.Info("")
logger.Info("\tpack set-default-builder <builder image>")
logger.Info("")
suggestBuilders(logger, client)
}

func suggestBuilders(logger logging.LoggerWithWriter, client PackClient) {
out := logger.Writer()
_, _ = fmt.Fprintln(out, "Suggested builders:")
_, _ = fmt.Fprintln(out)
tw := tabwriter.NewWriter(out, 10, 10, 5, ' ', tabwriter.TabIndent)
func suggestBuilders(logger logging.Logger, client PackClient) {
logger.Info("Suggested builders:")
tw := tabwriter.NewWriter(logger.Writer(), 10, 10, 5, ' ', tabwriter.TabIndent)
for _, i := range rand.Perm(len(suggestedBuilders)) {
builders := suggestedBuilders[i]
for _, builder := range builders {
Expand Down Expand Up @@ -124,8 +121,8 @@ func getBuilderDescription(builderName string, client PackClient) string {
return desc
}

func suggestStacks(logger *logging.Logger) {
logger.Info(`
func suggestStacks(log logging.Logger) {
log.Info(`
Stacks maintained by the Cloud Native Buildpacks project:
Stack ID: io.buildpacks.stacks.bionic
Expand Down
Loading

0 comments on commit 6bcc8ef

Please sign in to comment.