-
Notifications
You must be signed in to change notification settings - Fork 294
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 #330 from dgageot/improve-suggestions
Improve stack/builders suggestions
- Loading branch information
Showing
5 changed files
with
152 additions
and
106 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 |
---|---|---|
@@ -1,22 +1,97 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"sync" | ||
"text/tabwriter" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/buildpack/pack/logging" | ||
"github.com/buildpack/pack/style" | ||
) | ||
|
||
type suggestedBuilder struct { | ||
Name string | ||
Image string | ||
DefaultDescription string | ||
} | ||
|
||
var suggestedBuilders = []suggestedBuilder{ | ||
{ | ||
Name: "Cloud Foundry", | ||
Image: "cloudfoundry/cnb:bionic", | ||
DefaultDescription: "Small base image with Java & Node.js", | ||
}, | ||
{ | ||
Name: "Cloud Foundry", | ||
Image: "cloudfoundry/cnb:cflinuxfs3", | ||
DefaultDescription: "Larger base image with Java, Node.js & Python", | ||
}, | ||
{ | ||
Name: "Heroku", | ||
Image: "heroku/buildpacks:18", | ||
DefaultDescription: "heroku-18 base image with buildpacks for Ruby, Java, Node.js, Python, Golang, & PHP", | ||
}, | ||
} | ||
|
||
func SuggestBuilders(logger logging.Logger, client PackClient) *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 { | ||
Run: func(*cobra.Command, []string) { | ||
suggestBuilders(logger, client) | ||
return nil | ||
}), | ||
}, | ||
} | ||
|
||
AddHelpFlag(cmd, "suggest-builders") | ||
return cmd | ||
} | ||
|
||
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.Logger, client PackClient) { | ||
sort.Slice(suggestedBuilders, func(i, j int) bool { return suggestedBuilders[i].Image < suggestedBuilders[j].Image }) | ||
|
||
logger.Info("Suggested builders:") | ||
|
||
// Fetch descriptions concurrently. | ||
descriptions := make([]string, len(suggestedBuilders)) | ||
|
||
var wg sync.WaitGroup | ||
for i, builder := range suggestedBuilders { | ||
wg.Add(1) | ||
|
||
go func(i int, builder suggestedBuilder) { | ||
descriptions[i] = getBuilderDescription(builder, client) | ||
wg.Done() | ||
}(i, builder) | ||
} | ||
wg.Wait() | ||
|
||
tw := tabwriter.NewWriter(logger.Writer(), 10, 10, 5, ' ', tabwriter.TabIndent) | ||
for i, builder := range suggestedBuilders { | ||
fmt.Fprintf(tw, "\t%s:\t%s\t%s\t\n", builder.Name, style.Symbol(builder.Image), descriptions[i]) | ||
} | ||
fmt.Fprintln(tw) | ||
|
||
logging.Tip(logger, "Learn more about a specific builder with:\n") | ||
logger.Info("\tpack inspect-builder [builder image]") | ||
} | ||
|
||
func getBuilderDescription(builder suggestedBuilder, client PackClient) string { | ||
info, err := client.InspectBuilder(builder.Image, false) | ||
if err == nil && info.Description != "" { | ||
return info.Description | ||
} | ||
|
||
return builder.DefaultDescription | ||
} |
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 |
---|---|---|
@@ -1,22 +1,80 @@ | ||
package commands | ||
|
||
import ( | ||
"html/template" | ||
"sort" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/buildpack/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 Cloud Foundry stack based on Ubuntu 18.04", | ||
Maintainer: "Cloud Foundry", | ||
BuildImage: "cloudfoundry/build:base-cnb", | ||
RunImage: "cloudfoundry/run:base-cnb", | ||
}, | ||
{ | ||
ID: "org.cloudfoundry.stacks.cflinuxfs3", | ||
Description: "A large Cloud Foundry stack based on Ubuntu 18.04", | ||
Maintainer: "Cloud Foundry", | ||
BuildImage: "cloudfoundry/build:full-cnb", | ||
RunImage: "cloudfoundry/run:full-cnb", | ||
}, | ||
{ | ||
ID: "org.cloudfoundry.stacks.tiny", | ||
Description: "A tiny Cloud Foundry stack based on Ubuntu 18.04, similar to distroless", | ||
Maintainer: "Cloud Foundry", | ||
BuildImage: "cloudfoundry/build:tiny-cnb", | ||
RunImage: "cloudfoundry/run:tiny-cnb", | ||
}, | ||
} | ||
|
||
func SuggestStacks(logger logging.Logger) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "suggest-stacks", | ||
Short: "Display list of recommended stacks", | ||
Args: cobra.NoArgs, | ||
RunE: logError(logger, func(cmd *cobra.Command, args []string) error { | ||
Run: func(*cobra.Command, []string) { | ||
suggestStacks(logger) | ||
return nil | ||
}), | ||
}, | ||
} | ||
|
||
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 }} | ||
`)) | ||
tmpl.Execute(log.Writer(), suggestedStacks) | ||
} |
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