Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a single constructor for lifecycle inputs #1009

Merged
merged 7 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions cmd/lifecycle/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func (a *analyzeCmd) DefineFlags() {
cli.FlagAnalyzedPath(&a.AnalyzedPath)
cli.FlagCacheImage(&a.CacheImageRef)
cli.FlagGID(&a.GID)
cli.FlagLayersDir(&a.LayersDir)
cli.FlagPreviousImage(&a.PreviousImageRef)
cli.FlagRunImage(&a.RunImageRef)
cli.FlagStackPath(&a.StackPath)
Expand All @@ -54,7 +53,6 @@ func (a *analyzeCmd) DefineFlags() {
cli.FlagCacheImage(&a.CacheImageRef)
cli.FlagGID(&a.GID)
cli.FlagGroupPath(&a.GroupPath)
cli.FlagLayersDir(&a.LayersDir)
cli.FlagSkipLayers(&a.SkipLayers)
cli.FlagUID(&a.UID)
cli.FlagUseDaemon(&a.UseDaemon)
Expand All @@ -68,7 +66,7 @@ func (a *analyzeCmd) Args(nargs int, args []string) error {
return cmd.FailErrCode(err, cmd.CodeForInvalidArgs, "parse arguments")
}
a.LifecycleInputs.OutputImageRef = args[0]
if err := platform.ResolveInputs(platform.Analyze, &a.LifecycleInputs, cmd.DefaultLogger); err != nil {
if err := platform.ResolveInputs(platform.Analyze, a.LifecycleInputs, cmd.DefaultLogger); err != nil {
return cmd.FailErrCode(err, cmd.CodeForInvalidArgs, "resolve inputs")
}
if a.UseLayout {
Expand Down
4 changes: 1 addition & 3 deletions cmd/lifecycle/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ func (b *buildCmd) DefineFlags() {
cli.FlagBuildConfigDir(&b.BuildConfigDir)
cli.FlagBuildpacksDir(&b.BuildpacksDir)
cli.FlagGroupPath(&b.GroupPath)
cli.FlagLayersDir(&b.LayersDir)
cli.FlagPlanPath(&b.PlanPath)
cli.FlagPlatformDir(&b.PlatformDir)
default:
cli.FlagAppDir(&b.AppDir)
cli.FlagBuildpacksDir(&b.BuildpacksDir)
cli.FlagGroupPath(&b.GroupPath)
cli.FlagLayersDir(&b.LayersDir)
cli.FlagPlanPath(&b.PlanPath)
cli.FlagPlatformDir(&b.PlatformDir)
}
Expand All @@ -45,7 +43,7 @@ func (b *buildCmd) Args(nargs int, _ []string) error {
if nargs != 0 {
return cmd.FailErrCode(errors.New("received unexpected arguments"), cmd.CodeForInvalidArgs, "parse arguments")
}
if err := platform.ResolveInputs(platform.Build, &b.LifecycleInputs, cmd.DefaultLogger); err != nil {
if err := platform.ResolveInputs(platform.Build, b.LifecycleInputs, cmd.DefaultLogger); err != nil {
return cmd.FailErrCode(err, cmd.CodeForInvalidArgs, "resolve inputs")
}
return nil
Expand Down
3 changes: 1 addition & 2 deletions cmd/lifecycle/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (c *createCmd) DefineFlags() {
cli.FlagGID(&c.GID)
cli.FlagLaunchCacheDir(&c.LaunchCacheDir)
cli.FlagLauncherPath(&c.LauncherPath)
cli.FlagLayersDir(&c.LayersDir)
cli.FlagOrderPath(&c.OrderPath)
cli.FlagPlatformDir(&c.PlatformDir)
cli.FlagPreviousImage(&c.PreviousImageRef)
Expand All @@ -66,7 +65,7 @@ func (c *createCmd) Args(nargs int, args []string) error {
return cmd.FailErrCode(fmt.Errorf("received %d arguments, but expected 1", nargs), cmd.CodeForInvalidArgs, "parse arguments")
}
c.OutputImageRef = args[0]
if err := platform.ResolveInputs(platform.Create, &c.LifecycleInputs, cmd.DefaultLogger); err != nil {
if err := platform.ResolveInputs(platform.Create, c.LifecycleInputs, cmd.DefaultLogger); err != nil {
return cmd.FailErrCode(err, cmd.CodeForInvalidArgs, "resolve inputs")
}
if c.UseLayout {
Expand Down
4 changes: 1 addition & 3 deletions cmd/lifecycle/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ func (d *detectCmd) DefineFlags() {
cli.FlagExtensionsDir(&d.ExtensionsDir)
cli.FlagGeneratedDir(&d.GeneratedDir)
cli.FlagGroupPath(&d.GroupPath)
cli.FlagLayersDir(&d.LayersDir)
cli.FlagOrderPath(&d.OrderPath)
cli.FlagPlanPath(&d.PlanPath)
cli.FlagPlatformDir(&d.PlatformDir)
default:
cli.FlagAppDir(&d.AppDir)
cli.FlagBuildpacksDir(&d.BuildpacksDir)
cli.FlagGroupPath(&d.GroupPath)
cli.FlagLayersDir(&d.LayersDir)
cli.FlagOrderPath(&d.OrderPath)
cli.FlagPlanPath(&d.PlanPath)
cli.FlagPlatformDir(&d.PlatformDir)
Expand All @@ -47,7 +45,7 @@ func (d *detectCmd) Args(nargs int, _ []string) error {
if nargs != 0 {
return cmd.FailErrCode(errors.New("received unexpected arguments"), cmd.CodeForInvalidArgs, "parse arguments")
}
if err := platform.ResolveInputs(platform.Detect, &d.LifecycleInputs, cmd.DefaultLogger); err != nil {
if err := platform.ResolveInputs(platform.Detect, d.LifecycleInputs, cmd.DefaultLogger); err != nil {
return cmd.FailErrCode(err, cmd.CodeForInvalidArgs, "resolve inputs")
}
return nil
Expand Down
3 changes: 1 addition & 2 deletions cmd/lifecycle/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func (e *exportCmd) DefineFlags() {
cli.FlagGroupPath(&e.GroupPath)
cli.FlagLaunchCacheDir(&e.LaunchCacheDir)
cli.FlagLauncherPath(&e.LauncherPath)
cli.FlagLayersDir(&e.LayersDir)
cli.FlagProcessType(&e.DefaultProcessType)
cli.FlagProjectMetadataPath(&e.ProjectMetadataPath)
cli.FlagReportPath(&e.ReportPath)
Expand All @@ -79,7 +78,7 @@ func (e *exportCmd) Args(nargs int, args []string) error {
}
e.OutputImageRef = args[0]
e.AdditionalTags = args[1:]
if err := platform.ResolveInputs(platform.Export, &e.LifecycleInputs, cmd.DefaultLogger); err != nil {
if err := platform.ResolveInputs(platform.Export, e.LifecycleInputs, cmd.DefaultLogger); err != nil {
return cmd.FailErrCode(err, cmd.CodeForInvalidArgs, "resolve inputs")
}
// read analyzed metadata for use in later stages
Expand Down
3 changes: 1 addition & 2 deletions cmd/lifecycle/extender.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func (e *extendCmd) DefineFlags() {
cli.FlagGeneratedDir(&e.GeneratedDir)
cli.FlagGroupPath(&e.GroupPath)
cli.FlagKanikoCacheTTL(&e.KanikoCacheTTL)
cli.FlagLayersDir(&e.LayersDir)
cli.FlagPlanPath(&e.PlanPath)
cli.FlagPlatformDir(&e.PlatformDir)
cli.FlagUID(&e.UID)
Expand All @@ -36,7 +35,7 @@ func (e *extendCmd) Args(nargs int, _ []string) error {
if nargs != 0 {
return cmd.FailErrCode(errors.New("received unexpected arguments"), cmd.CodeForInvalidArgs, "parse arguments")
}
if err := platform.ResolveInputs(platform.Extend, &e.LifecycleInputs, cmd.DefaultLogger); err != nil {
if err := platform.ResolveInputs(platform.Extend, e.LifecycleInputs, cmd.DefaultLogger); err != nil {
return cmd.FailErrCode(err, cmd.CodeForInvalidArgs, "resolve inputs")
}
return nil
Expand Down
58 changes: 38 additions & 20 deletions cmd/lifecycle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,41 @@ import (
)

func main() {
switch strings.TrimSuffix(filepath.Base(os.Args[0]), filepath.Ext(os.Args[0])) {
phase := strings.TrimSuffix(filepath.Base(os.Args[0]), filepath.Ext(os.Args[0]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw I'm a fan of explicitly naming this string phase instead of leaving it anonymous 👏

if phase == "rebaser" {
cli.Run(&rebaseCmd{Platform: platform.NewPlatformFor(platformAPIWithExitOnError(), "<layers>")}, false)
}
var layersDir string
switch phase {
case "detector":
cli.Run(&detectCmd{Platform: platform.NewPlatformFor(platform.Detect, platformAPIWithExitOnError())}, false)
cli.FlagLayersDir(&layersDir)
cli.Run(&detectCmd{Platform: platform.NewPlatformFor(platformAPIWithExitOnError(), layersDir)}, false)
case "analyzer":
cli.Run(&analyzeCmd{Platform: platform.NewPlatformFor(platform.Analyze, platformAPIWithExitOnError())}, false)
cli.FlagLayersDir(&layersDir)
cli.Run(&analyzeCmd{Platform: platform.NewPlatformFor(platformAPIWithExitOnError(), layersDir)}, false)
case "restorer":
cli.Run(&restoreCmd{Platform: platform.NewPlatformFor(platform.Restore, platformAPIWithExitOnError())}, false)
cli.FlagLayersDir(&layersDir)
cli.Run(&restoreCmd{Platform: platform.NewPlatformFor(platformAPIWithExitOnError(), layersDir)}, false)
case "builder":
cli.Run(&buildCmd{Platform: platform.NewPlatformFor(platform.Build, platformAPIWithExitOnError())}, false)
cli.FlagLayersDir(&layersDir)
cli.Run(&buildCmd{Platform: platform.NewPlatformFor(platformAPIWithExitOnError(), layersDir)}, false)
case "exporter":
cli.Run(&exportCmd{Platform: platform.NewPlatformFor(platform.Export, platformAPIWithExitOnError())}, false)
case "rebaser":
cli.Run(&rebaseCmd{Platform: platform.NewPlatformFor(platform.Rebase, platformAPIWithExitOnError())}, false)
cli.FlagLayersDir(&layersDir)
cli.Run(&exportCmd{Platform: platform.NewPlatformFor(platformAPIWithExitOnError(), layersDir)}, false)
case "creator":
cli.Run(&createCmd{Platform: platform.NewPlatformFor(platform.Create, platformAPIWithExitOnError())}, false)
cli.FlagLayersDir(&layersDir)
cli.Run(&createCmd{Platform: platform.NewPlatformFor(platformAPIWithExitOnError(), layersDir)}, false)
case "extender":
cli.Run(&extendCmd{Platform: platform.NewPlatformFor(platform.Extend, platformAPIWithExitOnError())}, false)
cli.FlagLayersDir(&layersDir)
cli.Run(&extendCmd{Platform: platform.NewPlatformFor(platformAPIWithExitOnError(), layersDir)}, false)
default:
if len(os.Args) < 2 {
cmd.Exit(cmd.FailCode(cmd.CodeForInvalidArgs, "parse arguments"))
}
if os.Args[1] == "-version" {
cmd.ExitWithVersion()
}
subcommand(platformAPIWithExitOnError())
subcommand(platformAPIWithExitOnError(), layersDir)
}
}

Expand All @@ -54,25 +64,33 @@ func platformAPIWithExitOnError() string {
return platformAPI
}

func subcommand(platformAPI string) {
func subcommand(platformAPI, layersDir string) {
phase := filepath.Base(os.Args[1])
switch phase {
case "detect":
cli.Run(&detectCmd{Platform: platform.NewPlatformFor(platform.Detect, platformAPI)}, true)
cli.FlagLayersDir(&layersDir)
cli.Run(&detectCmd{Platform: platform.NewPlatformFor(platformAPI, layersDir)}, true)
case "analyze":
cli.Run(&analyzeCmd{Platform: platform.NewPlatformFor(platform.Analyze, platformAPI)}, true)
cli.FlagLayersDir(&layersDir)
cli.Run(&analyzeCmd{Platform: platform.NewPlatformFor(platformAPI, layersDir)}, true)
case "restore":
cli.Run(&restoreCmd{Platform: platform.NewPlatformFor(platform.Restore, platformAPI)}, true)
cli.FlagLayersDir(&layersDir)
cli.Run(&restoreCmd{Platform: platform.NewPlatformFor(platformAPI, layersDir)}, true)
case "build":
cli.Run(&buildCmd{Platform: platform.NewPlatformFor(platform.Build, platformAPI)}, true)
cli.FlagLayersDir(&layersDir)
cli.Run(&buildCmd{Platform: platform.NewPlatformFor(platformAPI, layersDir)}, true)
case "export":
cli.Run(&exportCmd{Platform: platform.NewPlatformFor(platform.Export, platformAPI)}, true)
cli.FlagLayersDir(&layersDir)
cli.Run(&exportCmd{Platform: platform.NewPlatformFor(platformAPI, layersDir)}, true)
case "rebase":
cli.Run(&rebaseCmd{Platform: platform.NewPlatformFor(platform.Rebase, platformAPI)}, true)
cli.FlagLayersDir(&layersDir)
cli.Run(&rebaseCmd{Platform: platform.NewPlatformFor(platformAPI, "<layers>")}, true)
case "create":
cli.Run(&createCmd{Platform: platform.NewPlatformFor(platform.Create, platformAPI)}, true)
cli.FlagLayersDir(&layersDir)
cli.Run(&createCmd{Platform: platform.NewPlatformFor(platformAPI, layersDir)}, true)
case "extend":
cli.Run(&extendCmd{Platform: platform.NewPlatformFor(platform.Extend, platformAPI)}, true)
cli.FlagLayersDir(&layersDir)
cli.Run(&extendCmd{Platform: platform.NewPlatformFor(platformAPI, layersDir)}, true)
default:
cmd.Exit(cmd.FailCode(cmd.CodeForInvalidArgs, "unknown phase:", phase))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/lifecycle/rebaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (r *rebaseCmd) Args(nargs int, args []string) error {
}
r.OutputImageRef = args[0]
r.AdditionalTags = args[1:]
if err := platform.ResolveInputs(platform.Rebase, &r.LifecycleInputs, cmd.DefaultLogger); err != nil {
if err := platform.ResolveInputs(platform.Rebase, r.LifecycleInputs, cmd.DefaultLogger); err != nil {
return cmd.FailErrCode(err, cmd.CodeForInvalidArgs, "resolve inputs")
}
if err := r.setAppImage(); err != nil {
Expand Down
5 changes: 1 addition & 4 deletions cmd/lifecycle/restorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func (r *restoreCmd) DefineFlags() {
cli.FlagCacheDir(&r.CacheDir)
cli.FlagCacheImage(&r.CacheImageRef)
cli.FlagGroupPath(&r.GroupPath)
cli.FlagLayersDir(&r.LayersDir)
cli.FlagUID(&r.UID)
cli.FlagGID(&r.GID)
cli.FlagAnalyzedPath(&r.AnalyzedPath)
Expand All @@ -49,7 +48,6 @@ func (r *restoreCmd) DefineFlags() {
cli.FlagCacheDir(&r.CacheDir)
cli.FlagCacheImage(&r.CacheImageRef)
cli.FlagGroupPath(&r.GroupPath)
cli.FlagLayersDir(&r.LayersDir)
cli.FlagUID(&r.UID)
cli.FlagGID(&r.GID)
cli.FlagAnalyzedPath(&r.AnalyzedPath)
Expand All @@ -58,7 +56,6 @@ func (r *restoreCmd) DefineFlags() {
cli.FlagCacheDir(&r.CacheDir)
cli.FlagCacheImage(&r.CacheImageRef)
cli.FlagGroupPath(&r.GroupPath)
cli.FlagLayersDir(&r.LayersDir)
cli.FlagUID(&r.UID)
cli.FlagGID(&r.GID)
}
Expand All @@ -69,7 +66,7 @@ func (r *restoreCmd) Args(nargs int, _ []string) error {
if nargs > 0 {
return cmd.FailErrCode(errors.New("received unexpected Args"), cmd.CodeForInvalidArgs, "parse arguments")
}
if err := platform.ResolveInputs(platform.Restore, &r.LifecycleInputs, cmd.DefaultLogger); err != nil {
if err := platform.ResolveInputs(platform.Restore, r.LifecycleInputs, cmd.DefaultLogger); err != nil {
return cmd.FailErrCode(err, cmd.CodeForInvalidArgs, "resolve inputs")
}
return nil
Expand Down
89 changes: 0 additions & 89 deletions platform/analyze_inputs.go

This file was deleted.

46 changes: 0 additions & 46 deletions platform/build_inputs.go

This file was deleted.

Loading