-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement RFC 212 to provide a more general mechanism to provide project scaffolding for new buildpacks. Signed-off-by: Aidan Delaney <[email protected]>
- Loading branch information
1 parent
de786a2
commit 6e320e4
Showing
10 changed files
with
198 additions
and
0 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
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,70 @@ | ||
package commands | ||
|
||
import ( | ||
"context" | ||
|
||
scafall "github.com/buildpacks/scafall/pkg" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/buildpacks/pack/internal/style" | ||
"github.com/buildpacks/pack/pkg/client" | ||
"github.com/buildpacks/pack/pkg/logging" | ||
) | ||
|
||
const ( | ||
CNBTemplates = "https://github.com/buildpacks/templates" | ||
) | ||
|
||
type BuildpackCreateFlags struct { | ||
Arguments map[string]string | ||
Template string | ||
SubPath string | ||
} | ||
|
||
type BuildpackCreateCreator interface { | ||
CreateBuildpack(ctx context.Context, options client.CreateBuildpackOptions) error | ||
} | ||
|
||
func BuildpackCreate(logger logging.Logger, creator BuildpackCreateCreator) *cobra.Command { | ||
flags := BuildpackCreateFlags{} | ||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: "Creates basic scaffolding of a buildpack.", | ||
Args: cobra.MatchAll(cobra.ExactArgs(0)), | ||
Example: "pack buildpack create", | ||
Long: "buildpack new generates the basic scaffolding of a buildpack repository.", | ||
RunE: logError(logger, func(cmd *cobra.Command, args []string) error { | ||
if err := creator.CreateBuildpack(cmd.Context(), client.CreateBuildpackOptions{ | ||
Template: flags.Template, | ||
SubPath: flags.SubPath, | ||
Arguments: flags.Arguments, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
logger.Infof("Successfully scaffolded %s", style.Symbol("template")) | ||
return nil | ||
}), | ||
} | ||
|
||
cmd.Flags().StringVarP(&flags.Template, "template", "t", CNBTemplates, "URL of the buildpack template git repository") | ||
cmd.Flags().StringVar(&flags.SubPath, "sub-path", "", "directory within template git repository used to generate the buildpack") | ||
cmd.Flags().StringToStringVarP(&flags.Arguments, "arg", "a", nil, "arguments to the buildpack template") | ||
|
||
cmd.SetHelpFunc(func(*cobra.Command, []string) { | ||
s, err := scafall.NewScafall(flags.Template, scafall.WithSubPath(flags.SubPath)) | ||
if err != nil { | ||
logger.Errorf("unable to get help for template: %s", err) | ||
} else { | ||
info, args, err := s.TemplateArguments() | ||
if err != nil { | ||
logger.Errorf("unable to get template arguments for template: %s", err) | ||
} | ||
logger.Info(info) | ||
for _, arg := range args { | ||
logger.Infof("\t%s", arg) | ||
} | ||
} | ||
}) | ||
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,70 @@ | ||
package commands_test | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/heroku/color" | ||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/buildpacks/pack/internal/commands" | ||
"github.com/buildpacks/pack/internal/commands/testmocks" | ||
"github.com/buildpacks/pack/pkg/client" | ||
"github.com/buildpacks/pack/pkg/logging" | ||
h "github.com/buildpacks/pack/testhelpers" | ||
) | ||
|
||
func TestBuildpackCreateCommand(t *testing.T) { | ||
color.Disable(true) | ||
defer color.Disable(false) | ||
spec.Run(t, "BuildpackCreateCommand", testBuildpackCreateCommand, spec.Parallel(), spec.Report(report.Terminal{})) | ||
} | ||
|
||
func testBuildpackCreateCommand(t *testing.T, when spec.G, it spec.S) { | ||
var ( | ||
command *cobra.Command | ||
logger *logging.LogWithWriters | ||
outBuf bytes.Buffer | ||
mockController *gomock.Controller | ||
mockClient *testmocks.MockPackClient | ||
tmpDir string | ||
) | ||
|
||
it.Before(func() { | ||
tmpDir = t.TempDir() | ||
logger = logging.NewLogWithWriters(&outBuf, &outBuf) | ||
mockController = gomock.NewController(t) | ||
mockClient = testmocks.NewMockPackClient(mockController) | ||
|
||
command = commands.BuildpackCreate(logger, mockClient) | ||
}) | ||
|
||
it.After(func() { | ||
os.RemoveAll(tmpDir) | ||
}) | ||
|
||
when("BuildpackCreate#Execute", func() { | ||
it("uses the args to generate artifacts", func() { | ||
mockClient.EXPECT().CreateBuildpack(gomock.Any(), client.CreateBuildpackOptions{ | ||
Template: "testdata", | ||
SubPath: "create", | ||
Arguments: map[string]string{ | ||
"Test": "Quack", | ||
}, | ||
}).Return(nil).MaxTimes(1) | ||
|
||
command.SetArgs([]string{ | ||
"--template", "testdata", | ||
"--sub-path", "create", | ||
"--arg", "Test=Quack", | ||
}) | ||
|
||
err := command.Execute() | ||
h.AssertNil(t, err) | ||
}) | ||
}) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,34 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
|
||
scafall "github.com/buildpacks/scafall/pkg" | ||
) | ||
|
||
type CreateBuildpackOptions struct { | ||
// URL of git repository containing the project template | ||
Template string | ||
|
||
// Subdirectory within the repository containing the project template | ||
SubPath string | ||
|
||
// arguments to provide to the project template | ||
Arguments map[string]string | ||
} | ||
|
||
func (c *Client) CreateBuildpack(ctx context.Context, opts CreateBuildpackOptions) error { | ||
ops := []scafall.Option{scafall.WithSubPath(opts.SubPath)} | ||
if opts.Arguments != nil { | ||
ops = append(ops, scafall.WithArguments(opts.Arguments)) | ||
} | ||
s, err := scafall.NewScafall(opts.Template, ops...) | ||
if err != nil { | ||
return err | ||
} | ||
err = s.Scaffold() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
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