Skip to content

Commit

Permalink
Merge pull request #1315 from buildpacks/feature/pack-client-lib
Browse files Browse the repository at this point in the history
Restructure pack client to pkg package
  • Loading branch information
jromero authored Nov 2, 2021
2 parents c845240 + 7ee83ee commit 3e1f522
Show file tree
Hide file tree
Showing 305 changed files with 3,187 additions and 2,995 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ PACK_GITSHA1=$(shell git rev-parse --short=7 HEAD)
PACK_VERSION?=0.0.0
TEST_TIMEOUT?=1200s
UNIT_TIMEOUT?=$(TEST_TIMEOUT)
NO_DOCKER?=

clean_build := $(strip ${PACK_BUILD})
clean_sha := $(strip ${PACK_GITSHA1})
Expand Down Expand Up @@ -99,6 +100,9 @@ test: unit acceptance
ifeq ($(TEST_COVERAGE), 1)
unit: GOTESTFLAGS:=$(GOTESTFLAGS) -coverprofile=./out/tests/coverage-unit.txt -covermode=atomic
endif
ifeq ($(NO_DOCKER),)
unit: GOTESTFLAGS:=$(GOTESTFLAGS) --tags=example
endif
unit: out
@echo "> Running unit/integration tests..."
$(GOCMD) test $(GOTESTFLAGS) -timeout=$(UNIT_TIMEOUT) ./...
Expand Down
13 changes: 6 additions & 7 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/buildpacks/pack/acceptance/config"
"github.com/buildpacks/pack/acceptance/invoke"
"github.com/buildpacks/pack/acceptance/managers"
pubcfg "github.com/buildpacks/pack/config"
"github.com/buildpacks/pack/internal/cache"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/pkg/archive"
Expand Down Expand Up @@ -306,7 +305,7 @@ func testWithoutSpecificBuilderRequirement(
pack.JustRunSuccessfully(
"buildpack", "package", packageName,
"-c", aggregatePackageToml,
"--pull-policy", pubcfg.PullNever.String())
"--pull-policy", "never")

assertImage.ExistsLocally(packageName)
})
Expand All @@ -330,7 +329,7 @@ func testWithoutSpecificBuilderRequirement(
output, err := pack.Run(
"buildpack", "package", packageName,
"-c", aggregatePackageToml,
"--pull-policy", pubcfg.PullNever.String(),
"--pull-policy", "never",
)

assert.NotNil(err)
Expand Down Expand Up @@ -2341,7 +2340,7 @@ include = [ "*.jar", "media/mountain.jpg", "/media/person.png", ]
"-p", filepath.Join("testdata", "mock_app"),
"--builder", builderName,
"--run-image", runBefore,
"--pull-policy", pubcfg.PullNever.String(),
"--pull-policy", "never",
)
origID = h.ImageID(t, repoName)
assertImage.RunsWithOutput(
Expand Down Expand Up @@ -2378,7 +2377,7 @@ include = [ "*.jar", "media/mountain.jpg", "/media/person.png", ]
output := pack.RunSuccessfully(
"rebase", repoName,
"--run-image", runAfter,
"--pull-policy", pubcfg.PullNever.String(),
"--pull-policy", "never",
)

assert.Contains(output, fmt.Sprintf("Successfully rebased image '%s'", repoName))
Expand All @@ -2404,7 +2403,7 @@ include = [ "*.jar", "media/mountain.jpg", "/media/person.png", ]
})

it("prefers the local mirror", func() {
output := pack.RunSuccessfully("rebase", repoName, "--pull-policy", pubcfg.PullNever.String())
output := pack.RunSuccessfully("rebase", repoName, "--pull-policy", "never")

assertOutput := assertions.NewOutputAssertionManager(t, output)
assertOutput.ReportsSelectingRunImageMirrorFromLocalConfig(localRunImageMirror)
Expand All @@ -2426,7 +2425,7 @@ include = [ "*.jar", "media/mountain.jpg", "/media/person.png", ]
})

it("selects the best mirror", func() {
output := pack.RunSuccessfully("rebase", repoName, "--pull-policy", pubcfg.PullNever.String())
output := pack.RunSuccessfully("rebase", repoName, "--pull-policy", "never")

assertOutput := assertions.NewOutputAssertionManager(t, output)
assertOutput.ReportsSelectingRunImageMirror(runImageMirror)
Expand Down
2 changes: 1 addition & 1 deletion acceptance/config/asset_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"testing"

acceptanceOS "github.com/buildpacks/pack/acceptance/os"
"github.com/buildpacks/pack/internal/blob"
"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/pkg/blob"
h "github.com/buildpacks/pack/testhelpers"
)

Expand Down
8 changes: 4 additions & 4 deletions acceptance/config/github_asset_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"github.com/pkg/errors"
"golang.org/x/oauth2"

"github.com/buildpacks/pack/internal/blob"
"github.com/buildpacks/pack/logging"
"github.com/buildpacks/pack/pkg/blob"
"github.com/buildpacks/pack/pkg/logging"
)

const (
Expand Down Expand Up @@ -376,7 +376,7 @@ func (f *GithubAssetFetcher) writeCacheManifest(owner, repo string, op func(cach
func (f *GithubAssetFetcher) downloadAndSave(assetURI, destPath string) error {
f.testObject.Helper()

downloader := blob.NewDownloader(logging.New(&testWriter{t: f.testObject}), f.cacheDir)
downloader := blob.NewDownloader(logging.NewSimpleLogger(&testWriter{t: f.testObject}), f.cacheDir)

assetBlob, err := downloader.Download(f.ctx, assetURI)
if err != nil {
Expand Down Expand Up @@ -405,7 +405,7 @@ func (f *GithubAssetFetcher) downloadAndSave(assetURI, destPath string) error {
func (f *GithubAssetFetcher) downloadAndExtractTgz(assetURI, destDir string) error {
f.testObject.Helper()

downloader := blob.NewDownloader(logging.New(&testWriter{t: f.testObject}), f.cacheDir)
downloader := blob.NewDownloader(logging.NewSimpleLogger(&testWriter{t: f.testObject}), f.cacheDir)

assetBlob, err := downloader.Download(f.ctx, assetURI)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/buildpacks/pack"
"github.com/buildpacks/pack/internal/commands"
cfg "github.com/buildpacks/pack/internal/config"
ilogging "github.com/buildpacks/pack/internal/logging"
"github.com/buildpacks/pack/pkg/client"
"github.com/buildpacks/pack/pkg/logging"
h "github.com/buildpacks/pack/testhelpers"
)

Expand Down Expand Up @@ -76,8 +76,8 @@ func BenchmarkBuild(b *testing.B) {

func createCmd(b *testing.B, docker *dockerCli.Client) *cobra.Command {
outBuf := bytes.Buffer{}
logger := ilogging.NewLogWithWriters(&outBuf, &outBuf)
packClient, err := pack.NewClient(pack.WithLogger(logger), pack.WithDockerClient(docker), pack.WithExperimental(true))
logger := logging.NewLogWithWriters(&outBuf, &outBuf)
packClient, err := client.NewClient(client.WithLogger(logger), client.WithDockerClient(docker), client.WithExperimental(true))
if err != nil {
b.Error(errors.Wrap(err, "creating packClient"))
}
Expand Down
2 changes: 1 addition & 1 deletion builder/config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/pkg/errors"

"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/pkg/dist"
)

// Config is a builder configuration file
Expand Down
2 changes: 1 addition & 1 deletion builder/detection_order.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package builder

import (
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/pkg/dist"
)

type DetectionOrderEntry struct {
Expand Down
111 changes: 0 additions & 111 deletions buildpack_downloader.go

This file was deleted.

4 changes: 2 additions & 2 deletions buildpackage/config_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"github.com/BurntSushi/toml"
"github.com/pkg/errors"

"github.com/buildpacks/pack/internal/buildpack"
"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/pkg/buildpack"
"github.com/buildpacks/pack/pkg/dist"
)

const defaultOS = "linux"
Expand Down
53 changes: 24 additions & 29 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/buildpacks/pack"
"github.com/buildpacks/pack/buildpackage"
builderwriter "github.com/buildpacks/pack/internal/builder/writer"
"github.com/buildpacks/pack/internal/commands"
"github.com/buildpacks/pack/internal/config"
imagewriter "github.com/buildpacks/pack/internal/inspectimage/writer"
ilogging "github.com/buildpacks/pack/internal/logging"
"github.com/buildpacks/pack/logging"
"github.com/buildpacks/pack/internal/term"
"github.com/buildpacks/pack/pkg/client"
"github.com/buildpacks/pack/pkg/logging"
)

// ConfigurableLogger defines behavior required by the PackCommand
Expand Down Expand Up @@ -46,7 +46,7 @@ func NewPackCommand(logger ConfigurableLogger) (*cobra.Command, error) {
color.Disable(flag)
}

_, canDisplayColor := ilogging.IsTerminal(logging.GetWriterForLevel(logger, logging.InfoLevel))
_, canDisplayColor := term.IsTerminal(logging.GetWriterForLevel(logger, logging.InfoLevel))
if !canDisplayColor {
color.Disable(true)
}
Expand All @@ -72,34 +72,34 @@ func NewPackCommand(logger ConfigurableLogger) (*cobra.Command, error) {

commands.AddHelpFlag(rootCmd, "pack")

rootCmd.AddCommand(commands.Build(logger, cfg, &packClient))
rootCmd.AddCommand(commands.NewBuilderCommand(logger, cfg, &packClient))
rootCmd.AddCommand(commands.NewBuildpackCommand(logger, cfg, &packClient, buildpackage.NewConfigReader()))
rootCmd.AddCommand(commands.NewConfigCommand(logger, cfg, cfgPath, &packClient))
rootCmd.AddCommand(commands.InspectImage(logger, imagewriter.NewFactory(), cfg, &packClient))
rootCmd.AddCommand(commands.Build(logger, cfg, packClient))
rootCmd.AddCommand(commands.NewBuilderCommand(logger, cfg, packClient))
rootCmd.AddCommand(commands.NewBuildpackCommand(logger, cfg, packClient, buildpackage.NewConfigReader()))
rootCmd.AddCommand(commands.NewConfigCommand(logger, cfg, cfgPath, packClient))
rootCmd.AddCommand(commands.InspectImage(logger, imagewriter.NewFactory(), cfg, packClient))
rootCmd.AddCommand(commands.NewStackCommand(logger))
rootCmd.AddCommand(commands.Rebase(logger, cfg, &packClient))
rootCmd.AddCommand(commands.Rebase(logger, cfg, packClient))

rootCmd.AddCommand(commands.InspectBuildpack(logger, cfg, &packClient))
rootCmd.AddCommand(commands.InspectBuilder(logger, cfg, &packClient, builderwriter.NewFactory()))
rootCmd.AddCommand(commands.InspectBuildpack(logger, cfg, packClient))
rootCmd.AddCommand(commands.InspectBuilder(logger, cfg, packClient, builderwriter.NewFactory()))

rootCmd.AddCommand(commands.SetDefaultBuilder(logger, cfg, cfgPath, &packClient))
rootCmd.AddCommand(commands.SetDefaultBuilder(logger, cfg, cfgPath, packClient))
rootCmd.AddCommand(commands.SetRunImagesMirrors(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.SuggestBuilders(logger, &packClient))
rootCmd.AddCommand(commands.SuggestBuilders(logger, packClient))
rootCmd.AddCommand(commands.TrustBuilder(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.UntrustBuilder(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.ListTrustedBuilders(logger, cfg))
rootCmd.AddCommand(commands.CreateBuilder(logger, cfg, &packClient))
rootCmd.AddCommand(commands.PackageBuildpack(logger, cfg, &packClient, buildpackage.NewConfigReader()))
rootCmd.AddCommand(commands.CreateBuilder(logger, cfg, packClient))
rootCmd.AddCommand(commands.PackageBuildpack(logger, cfg, packClient, buildpackage.NewConfigReader()))
rootCmd.AddCommand(commands.SuggestStacks(logger))

if cfg.Experimental {
rootCmd.AddCommand(commands.AddBuildpackRegistry(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.ListBuildpackRegistries(logger, cfg))
rootCmd.AddCommand(commands.RegisterBuildpack(logger, cfg, &packClient))
rootCmd.AddCommand(commands.RegisterBuildpack(logger, cfg, packClient))
rootCmd.AddCommand(commands.SetDefaultRegistry(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.RemoveRegistry(logger, cfg, cfgPath))
rootCmd.AddCommand(commands.YankBuildpack(logger, cfg, &packClient))
rootCmd.AddCommand(commands.YankBuildpack(logger, cfg, packClient))
}

packHome, err := config.PackHome()
Expand All @@ -108,10 +108,10 @@ func NewPackCommand(logger ConfigurableLogger) (*cobra.Command, error) {
}

rootCmd.AddCommand(commands.CompletionCommand(logger, packHome))
rootCmd.AddCommand(commands.Report(logger, pack.Version, cfgPath))
rootCmd.AddCommand(commands.Version(logger, pack.Version))
rootCmd.AddCommand(commands.Report(logger, packClient.Version(), cfgPath))
rootCmd.AddCommand(commands.Version(logger, packClient.Version()))

rootCmd.Version = pack.Version
rootCmd.Version = packClient.Version()
rootCmd.SetVersionTemplate(`{{.Version}}{{"\n"}}`)
rootCmd.SetOut(logging.GetWriterForLevel(logger, logging.InfoLevel))
rootCmd.SetErr(logging.GetWriterForLevel(logger, logging.ErrorLevel))
Expand All @@ -132,15 +132,10 @@ func initConfig() (config.Config, string, error) {
return cfg, path, nil
}

func initClient(logger logging.Logger, cfg config.Config) (pack.Client, error) {
func initClient(logger logging.Logger, cfg config.Config) (*client.Client, error) {
dc, err := tryInitSSHDockerClient()
if err != nil {
return pack.Client{}, err
}
client, err := pack.NewClient(pack.WithLogger(logger), pack.WithExperimental(cfg.Experimental), pack.WithRegistryMirrors(cfg.RegistryMirrors), pack.WithDockerClient(dc))
if err != nil {
return pack.Client{}, err
return nil, err
}

return *client, nil
return client.NewClient(client.WithLogger(logger), client.WithExperimental(cfg.Experimental), client.WithRegistryMirrors(cfg.RegistryMirrors), client.WithDockerClient(dc))
}
Loading

0 comments on commit 3e1f522

Please sign in to comment.