diff --git a/cmd/porter/bundle.go b/cmd/porter/bundle.go index df01d4097..f2cbf0e65 100644 --- a/cmd/porter/bundle.go +++ b/cmd/porter/bundle.go @@ -80,9 +80,9 @@ The docker driver builds the bundle image using the local Docker host. To use a f.StringVar(&opts.Name, "name", "", "Override the bundle name") f.StringVar(&opts.Version, "version", "", "Override the bundle version") f.StringVarP(&opts.File, "file", "f", "", - "Path to the Porter manifest. Defaults to `porter.yaml` in the current directory.") + "Path to the Porter manifest. The path is relative to the build context directory. Defaults to porter.yaml in the current directory.") f.StringVarP(&opts.Dir, "dir", "d", "", - "Path to the build context directory where all bundle assets are located.") + "Path to the build context directory where all bundle assets are located. Defaults to the current directory.") f.StringVar(&opts.Driver, "driver", porter.BuildDriverDefault, fmt.Sprintf("Driver for building the invocation image. Allowed values are: %s", strings.Join(porter.BuildDriverAllowedValues, ", "))) f.MarkHidden("driver") // Hide the driver flag since there aren't any choices to make right now diff --git a/pkg/manifest/manifest.go b/pkg/manifest/manifest.go index 8321aa45e..732246041 100644 --- a/pkg/manifest/manifest.go +++ b/pkg/manifest/manifest.go @@ -17,6 +17,7 @@ import ( "get.porter.sh/porter/pkg/config" "get.porter.sh/porter/pkg/portercontext" "get.porter.sh/porter/pkg/schema" + "get.porter.sh/porter/pkg/tracing" "get.porter.sh/porter/pkg/yaml" "github.com/Masterminds/semver/v3" "github.com/cbroglie/mustache" @@ -1052,6 +1053,9 @@ func scanManifestTemplating(data []byte) (templateScanResult, error) { // LoadManifestFrom reads and validates the manifest at the specified location, // and returns a populated Manifest structure. func LoadManifestFrom(ctx context.Context, config *config.Config, file string) (*Manifest, error) { + ctx, log := tracing.StartSpan(ctx) + defer log.EndSpan() + m, err := ReadManifest(config.Context, file) if err != nil { return nil, err diff --git a/pkg/porter/build.go b/pkg/porter/build.go index e29754b4a..75979b41b 100644 --- a/pkg/porter/build.go +++ b/pkg/porter/build.go @@ -14,6 +14,7 @@ import ( "get.porter.sh/porter/pkg/mixin" "get.porter.sh/porter/pkg/printer" "get.porter.sh/porter/pkg/storage" + "get.porter.sh/porter/pkg/tracing" "github.com/Masterminds/semver/v3" "github.com/opencontainers/go-digest" "github.com/pkg/errors" @@ -92,6 +93,9 @@ func (o *BuildOptions) parseCustomInputs() error { } func (p *Porter) Build(ctx context.Context, opts BuildOptions) error { + ctx, log := tracing.StartSpan(ctx) + defer log.EndSpan() + opts.Apply(p.Context) if p.Debug { @@ -120,7 +124,7 @@ func (p *Porter) Build(ctx context.Context, opts BuildOptions) error { m.ManifestPath = opts.File if !opts.NoLint { - if err := p.preLint(ctx); err != nil { + if err := p.preLint(ctx, opts.File); err != nil { return err } } @@ -148,10 +152,11 @@ func (p *Porter) Build(ctx context.Context, opts BuildOptions) error { return errors.Wrap(builder.BuildInvocationImage(ctx, m, opts.BuildImageOptions), "unable to build CNAB invocation image") } -func (p *Porter) preLint(ctx context.Context) error { +func (p *Porter) preLint(ctx context.Context, file string) error { lintOpts := LintOptions{ contextOptions: NewContextOptions(p.Context), PrintOptions: printer.PrintOptions{}, + File: file, } lintOpts.RawFormat = string(printer.FormatPlaintext) err := lintOpts.Validate(p.Context) diff --git a/pkg/porter/cnab.go b/pkg/porter/cnab.go index bd41e264b..5cf2bfec5 100644 --- a/pkg/porter/cnab.go +++ b/pkg/porter/cnab.go @@ -46,19 +46,10 @@ type bundleFileOptions struct { func (o *bundleFileOptions) Validate(cxt *portercontext.Context) error { var err error - err = o.validateBundleFiles(cxt) - if err != nil { - return err - } - if o.ReferenceSet { return nil } - if o.File != "" { - o.File = cxt.FileSystem.Abs(o.File) - } - // Resolve the proper build context directory if o.Dir != "" { _, err = cxt.FileSystem.IsDir(o.Dir) @@ -66,6 +57,22 @@ func (o *bundleFileOptions) Validate(cxt *portercontext.Context) error { return errors.Wrapf(err, "%q is not a valid directory", o.Dir) } o.Dir = cxt.FileSystem.Abs(o.Dir) + } else { + // default to current working directory + o.Dir = cxt.Getwd() + } + + if o.File != "" { + if !filepath.IsAbs(o.File) { + o.File = cxt.FileSystem.Abs(filepath.Join(o.Dir, o.File)) + } else { + o.File = cxt.FileSystem.Abs(o.File) + } + } + + err = o.validateBundleFiles(cxt) + if err != nil { + return err } err = o.defaultBundleFiles(cxt) @@ -164,27 +171,21 @@ func (o *bundleFileOptions) defaultBundleFiles(cxt *portercontext.Context) error } else if o.CNABFile != "" { // --cnab-file // Nothing to default } else { - manifestExists, err := cxt.FileSystem.Exists(config.Name) - if err != nil { - return errors.Wrap(err, "could not check if porter manifest exists in current directory") + defaultPath := filepath.Join(o.Dir, config.Name) + manifestExists, err := cxt.FileSystem.Exists(defaultPath) + if err != nil || !manifestExists { + return errors.Wrapf(err, "could not find a porter manifest at %s", defaultPath) } - if manifestExists { - o.File = config.Name - o.defaultCNABFile() - } + o.File = defaultPath + o.defaultCNABFile() } return nil } func (o *bundleFileOptions) defaultCNABFile() { - // Place the bundle.json in o.Dir if set; otherwise place in current directory - if o.Dir != "" { - o.CNABFile = filepath.Join(o.Dir, build.LOCAL_BUNDLE) - } else { - o.CNABFile = build.LOCAL_BUNDLE - } + o.CNABFile = filepath.Join(o.Dir, build.LOCAL_BUNDLE) } func (o *bundleFileOptions) validateBundleFiles(cxt *portercontext.Context) error { diff --git a/pkg/porter/cnab_test.go b/pkg/porter/cnab_test.go index db03c7011..83d8361c5 100644 --- a/pkg/porter/cnab_test.go +++ b/pkg/porter/cnab_test.go @@ -2,6 +2,7 @@ package porter import ( "context" + "path/filepath" "testing" "get.porter.sh/porter/pkg" @@ -260,8 +261,8 @@ func Test_bundleFileOptions(t *testing.T) { name: "no opts", opts: bundleFileOptions{}, setup: func(ctx *portercontext.Context, opts bundleFileOptions) error { return nil }, - wantFile: config.Name, - wantCNABFile: build.LOCAL_BUNDLE, + wantFile: "/" + config.Name, + wantCNABFile: "/" + build.LOCAL_BUNDLE, wantError: "", }, { name: "reference set", @@ -289,16 +290,20 @@ func Test_bundleFileOptions(t *testing.T) { setup: func(ctx *portercontext.Context, opts bundleFileOptions) error { return nil }, wantFile: "", wantCNABFile: "", - wantError: "unable to access --file alternate/porter.yaml: open /alternate/porter.yaml: file does not exist", + wantError: "unable to access --file /alternate/porter.yaml: open /alternate/porter.yaml: file does not exist", }, { name: "valid dir", opts: bundleFileOptions{ Dir: "path/to/bundle", }, setup: func(ctx *portercontext.Context, opts bundleFileOptions) error { + err := ctx.FileSystem.MkdirAll(filepath.Join(opts.Dir, config.Name), pkg.FileModeDirectory) + if err != nil { + return err + } return ctx.FileSystem.MkdirAll(opts.Dir, pkg.FileModeDirectory) }, - wantFile: config.Name, + wantFile: "/path/to/bundle/porter.yaml", wantCNABFile: "/path/to/bundle/.cnab/bundle.json", wantError: "", }, { @@ -310,7 +315,7 @@ func Test_bundleFileOptions(t *testing.T) { return ctx.FileSystem.MkdirAll(opts.File, pkg.FileModeDirectory) }, wantFile: "/alternate/porter.yaml", - wantCNABFile: build.LOCAL_BUNDLE, + wantCNABFile: "/" + build.LOCAL_BUNDLE, wantError: "", }, { name: "valid dir and file", @@ -319,13 +324,13 @@ func Test_bundleFileOptions(t *testing.T) { File: "alternate/porter.yaml", }, setup: func(ctx *portercontext.Context, opts bundleFileOptions) error { - err := ctx.FileSystem.MkdirAll(opts.File, pkg.FileModeDirectory) + err := ctx.FileSystem.MkdirAll(filepath.Join(opts.Dir, opts.File), pkg.FileModeDirectory) if err != nil { return err } return ctx.FileSystem.MkdirAll(opts.Dir, pkg.FileModeDirectory) }, - wantFile: "/alternate/porter.yaml", + wantFile: "/path/to/bundle/alternate/porter.yaml", wantCNABFile: "/path/to/bundle/.cnab/bundle.json", wantError: "", }}