forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request buildpacks#693 from buildpacks/567-list-trusted-bu…
…ilders Add list-trusted-builders command
- Loading branch information
Showing
6 changed files
with
204 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package commands | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/buildpacks/pack/internal/config" | ||
"github.com/buildpacks/pack/logging" | ||
) | ||
|
||
func ListTrustedBuilders(logger logging.Logger, cfg config.Config) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list-trusted-builders", | ||
Short: "List Trusted Builders", | ||
Long: "List Trusted Builders.\n\nShow the builders that are either trusted by default or have been explicitly trusted locally using `trust-builder`", | ||
RunE: logError(logger, func(cmd *cobra.Command, args []string) error { | ||
logger.Info("Trusted Builders:") | ||
|
||
var trustedBuilders []string | ||
for _, builder := range suggestedBuilders { | ||
trustedBuilders = append(trustedBuilders, builder.Image) | ||
} | ||
|
||
for _, builder := range cfg.TrustedBuilders { | ||
trustedBuilders = append(trustedBuilders, builder.Name) | ||
} | ||
|
||
sort.Strings(trustedBuilders) | ||
|
||
for _, builder := range trustedBuilders { | ||
logger.Infof(" %s", builder) | ||
} | ||
|
||
return nil | ||
}), | ||
} | ||
|
||
AddHelpFlag(cmd, "list-trusted-builders") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package commands_test | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/buildpacks/pack/internal/config" | ||
|
||
"github.com/heroku/color" | ||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/buildpacks/pack/internal/commands" | ||
ilogging "github.com/buildpacks/pack/internal/logging" | ||
"github.com/buildpacks/pack/logging" | ||
h "github.com/buildpacks/pack/testhelpers" | ||
) | ||
|
||
func TestListTrustedBuilders(t *testing.T) { | ||
color.Disable(true) | ||
defer color.Disable(false) | ||
spec.Run(t, "Commands", testListTrustedBuildersCommand, spec.Random(), spec.Report(report.Terminal{})) | ||
} | ||
|
||
func testListTrustedBuildersCommand(t *testing.T, when spec.G, it spec.S) { | ||
var ( | ||
command *cobra.Command | ||
logger logging.Logger | ||
outBuf bytes.Buffer | ||
tempPackHome string | ||
) | ||
|
||
it.Before(func() { | ||
var err error | ||
|
||
logger = ilogging.NewLogWithWriters(&outBuf, &outBuf) | ||
command = commands.ListTrustedBuilders(logger, config.Config{}) | ||
|
||
tempPackHome, err = ioutil.TempDir("", "pack-home") | ||
h.AssertNil(t, err) | ||
h.AssertNil(t, os.Setenv("PACK_HOME", tempPackHome)) | ||
}) | ||
|
||
it.After(func() { | ||
h.AssertNil(t, os.Unsetenv("PACK_HOME")) | ||
h.AssertNil(t, os.RemoveAll(tempPackHome)) | ||
}) | ||
|
||
when("#ListTrustedBuilders", func() { | ||
it("succeeds", func() { | ||
h.AssertNil(t, command.Execute()) | ||
}) | ||
|
||
it("shows header", func() { | ||
h.AssertNil(t, command.Execute()) | ||
|
||
h.AssertContains(t, outBuf.String(), "Trusted Builders:") | ||
}) | ||
|
||
it("shows suggested builders and locally trusted builder in alphabetical order", func() { | ||
builderName := "great-builder-" + h.RandString(8) | ||
|
||
h.AssertNil(t, command.Execute()) | ||
h.AssertNotContains(t, outBuf.String(), builderName) | ||
h.AssertContainsAllInOrder(t, | ||
outBuf, | ||
"gcr.io/buildpacks/builder", | ||
"gcr.io/paketo-buildpacks/builder:base", | ||
"gcr.io/paketo-buildpacks/builder:full-cf", | ||
"gcr.io/paketo-buildpacks/builder:tiny", | ||
"heroku/buildpacks:18", | ||
) | ||
|
||
listTrustedBuildersCommand := commands.ListTrustedBuilders( | ||
logger, | ||
config.Config{ | ||
TrustedBuilders: []config.TrustedBuilder{{Name: builderName}}, | ||
}, | ||
) | ||
|
||
outBuf.Reset() | ||
|
||
h.AssertNil(t, listTrustedBuildersCommand.Execute()) | ||
|
||
h.AssertContainsAllInOrder(t, | ||
outBuf, | ||
"gcr.io/buildpacks/builder", | ||
"gcr.io/paketo-buildpacks/builder:base", | ||
"gcr.io/paketo-buildpacks/builder:full-cf", | ||
"gcr.io/paketo-buildpacks/builder:tiny", | ||
builderName, | ||
"heroku/buildpacks:18", | ||
) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters