Skip to content

Commit

Permalink
Included arg for pack detect (image name)
Browse files Browse the repository at this point in the history
  • Loading branch information
rashadism committed May 7, 2024
1 parent 0157be0 commit 5b03e5b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 43 deletions.
16 changes: 5 additions & 11 deletions internal/build/lifecycle_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type LifecycleExecution struct {
mountPaths mountPaths
opts LifecycleOptions
tmpDir string
DetectOnly bool
// DetectOnly bool
}

func NewLifecycleExecution(logger logging.Logger, docker DockerClient, tmpDir string, opts LifecycleOptions) (*LifecycleExecution, error) {
Expand All @@ -67,7 +67,6 @@ func NewLifecycleExecution(logger logging.Logger, docker DockerClient, tmpDir st
os: osType,
mountPaths: mountPathsForOS(osType, opts.Workspace),
tmpDir: tmpDir,
DetectOnly: opts.DetectOnly,
}

if opts.Interactive {
Expand Down Expand Up @@ -580,17 +579,12 @@ func (l *LifecycleExecution) Analyze(ctx context.Context, buildCache, launchCach
var flags []string
var args []string

// TODO: a better way to handle the image name (no args are passed to detect)
if l.DetectOnly {
args = []string{"fakeimage"}
} else {
args = []string{l.opts.Image.String()}
}
args = []string{l.opts.Image.String()}

platformAPILessThan07 := l.platformAPI.LessThan("0.7")

cacheBindOp := NullOp()
if l.opts.ClearCache || l.DetectOnly {
if l.opts.ClearCache || l.opts.DetectOnly {
if platformAPILessThan07 || l.platformAPI.AtLeast("0.9") {
args = prependArg("-skip-layers", args)
}
Expand All @@ -607,7 +601,7 @@ func (l *LifecycleExecution) Analyze(ctx context.Context, buildCache, launchCach
}

launchCacheBindOp := NullOp()
if l.platformAPI.AtLeast("0.9") && !l.DetectOnly {
if l.platformAPI.AtLeast("0.9") && !l.opts.DetectOnly {
if !l.opts.Publish {
args = append([]string{"-launch-cache", l.mountPaths.launchCacheDir()}, args...)
launchCacheBindOp = WithBinds(fmt.Sprintf("%s:%s", launchCache.Name(), l.mountPaths.launchCacheDir()))
Expand All @@ -622,7 +616,7 @@ func (l *LifecycleExecution) Analyze(ctx context.Context, buildCache, launchCach
flags = append(flags, "-uid", strconv.Itoa(l.opts.UID))
}

if l.opts.PreviousImage != "" && !l.DetectOnly {
if l.opts.PreviousImage != "" || !l.opts.DetectOnly {
if l.opts.Image == nil {
return errors.New("image can't be nil")
}
Expand Down
2 changes: 0 additions & 2 deletions internal/build/lifecycle_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/buildpacks/pack/internal/container"
"github.com/buildpacks/pack/pkg/cache"
"github.com/buildpacks/pack/pkg/dist"
"github.com/buildpacks/pack/pkg/image"
"github.com/buildpacks/pack/pkg/logging"
)

Expand Down Expand Up @@ -105,7 +104,6 @@ type LifecycleOptions struct {
CreationTime *time.Time
Keychain authn.Keychain
DetectOnly bool
FetchOptions image.FetchOptions
}

func NewLifecycleExecutor(logger logging.Logger, docker DockerClient) *LifecycleExecutor {
Expand Down
10 changes: 6 additions & 4 deletions internal/commands/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ func Detect(logger logging.Logger, cfg config.Config, packClient PackClient) *co

cmd := &cobra.Command{
Use: "detect",
Args: cobra.ExactArgs(0),
Args: cobra.ExactArgs(1),
Short: "Pack Detect runs the analyze and detect phases of the Cloud Native Buildpacks lifecycle to determine a group of applicable buildpacks and a build plan.",
Example: "pack detect --path apps/test-app --builder cnbs/sample-builder:bionic",
Long: "Pack Detect uses Cloud Native Buildpacks to run the detect phase of buildpack groups against the source code.\n",
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
// if err := validateBuildFlags(&flags, cfg, inputImageName, logger); err != nil {
// return err
// }
inputImageName := client.ParseInputImageReference(args[0])
if err := validateBuildFlags(&flags, cfg, inputImageName, logger); err != nil {
return err
}

descriptor, actualDescriptorPath, err := parseProjectToml(flags.AppPath, flags.DescriptorPath, logger)
if err != nil {
Expand Down Expand Up @@ -90,6 +91,7 @@ func Detect(logger logging.Logger, cfg config.Config, packClient PackClient) *co
Builder: builder,
Registry: flags.Registry,
Env: env,
Image: inputImageName.Name(),
RunImage: flags.RunImage,
Publish: flags.Publish,
DockerHost: flags.DockerHost,
Expand Down
42 changes: 16 additions & 26 deletions pkg/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1694,32 +1694,22 @@ func readOnlyVolume(hostPath, targetPath string) string {

func (c *Client) ResolveLifecycleOptions(ctx context.Context, opts BuildOptions) (*build.LifecycleOptions, error) {
var pathsConfig layoutPathConfig
imageVars := struct {
imageRef name.Reference
imgRegistry string
imageName string
}{}

detectOnly := opts.DetectOnly
imageRef, err := c.parseReference(opts)
if err != nil {
return nil, errors.Wrapf(err, "invalid image name '%s'", opts.Image)
}
imgRegistry := imageRef.Context().RegistryStr()
imageName := imageRef.Name()

if !detectOnly {
imageRef, err := c.parseReference(opts)
if opts.Layout() {
pathsConfig, err = c.processLayoutPath(opts.LayoutConfig.InputImage, opts.LayoutConfig.PreviousInputImage)
if err != nil {
return nil, errors.Wrapf(err, "invalid image name '%s'", opts.Image)
}
imageVars.imgRegistry = imageRef.Context().RegistryStr()
imageVars.imageName = imageRef.Name()
imageVars.imageRef = imageRef

if opts.Layout() {
pathsConfig, err = c.processLayoutPath(opts.LayoutConfig.InputImage, opts.LayoutConfig.PreviousInputImage)
if err != nil {
if opts.LayoutConfig.PreviousInputImage != nil {
return nil, errors.Wrapf(err, "invalid layout paths image name '%s' or previous-image name '%s'", opts.LayoutConfig.InputImage.Name(),
opts.LayoutConfig.PreviousInputImage.Name())
}
return nil, errors.Wrapf(err, "invalid layout paths image name '%s'", opts.LayoutConfig.InputImage.Name())
if opts.LayoutConfig.PreviousInputImage != nil {
return nil, errors.Wrapf(err, "invalid layout paths image name '%s' or previous-image name '%s'", opts.LayoutConfig.InputImage.Name(),
opts.LayoutConfig.PreviousInputImage.Name())
}
return nil, errors.Wrapf(err, "invalid layout paths image name '%s'", opts.LayoutConfig.InputImage.Name())
}
}

Expand Down Expand Up @@ -1760,7 +1750,7 @@ func (c *Client) ResolveLifecycleOptions(ctx context.Context, opts BuildOptions)
PullPolicy: opts.PullPolicy,
Platform: fmt.Sprintf("%s/%s", builderOS, builderArch),
}
runImageName := c.resolveRunImage(opts.RunImage, imageVars.imgRegistry, builderRef.Context().RegistryStr(), bldr.DefaultRunImage(), opts.AdditionalMirrors, opts.Publish, fetchOptions)
runImageName := c.resolveRunImage(opts.RunImage, imgRegistry, builderRef.Context().RegistryStr(), bldr.DefaultRunImage(), opts.AdditionalMirrors, opts.Publish, fetchOptions)

if opts.Layout() {
targetRunImagePath, err := layout.ParseRefToPath(runImageName)
Expand Down Expand Up @@ -1932,7 +1922,7 @@ func (c *Client) ResolveLifecycleOptions(ctx context.Context, opts BuildOptions)

lifecycleOpts := &build.LifecycleOptions{
AppPath: appPath,
Image: imageVars.imageRef,
Image: imageRef,
Builder: ephemeralBuilder,
BuilderImage: builderRef.Name(),
LifecycleImage: ephemeralBuilder.Name(),
Expand All @@ -1959,13 +1949,13 @@ func (c *Client) ResolveLifecycleOptions(ctx context.Context, opts BuildOptions)
UID: opts.UserID,
PreviousImage: opts.PreviousImage,
Interactive: opts.Interactive,
Termui: termui.NewTermui(imageVars.imageName, ephemeralBuilder, runImageName),
Termui: termui.NewTermui(imageName, ephemeralBuilder, runImageName),
ReportDestinationDir: opts.ReportDestinationDir,
SBOMDestinationDir: opts.SBOMDestinationDir,
CreationTime: opts.CreationTime,
Layout: opts.Layout(),
Keychain: c.keychain,
DetectOnly: detectOnly,
DetectOnly: opts.DetectOnly,
}

switch {
Expand Down

0 comments on commit 5b03e5b

Please sign in to comment.