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

Use user specified directory for resolving file path #2142

Merged
merged 7 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 2 deletions cmd/porter/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions pkg/porter/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand Down
45 changes: 23 additions & 22 deletions pkg/porter/cnab.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,33 @@ 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)
if err != nil {
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)
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 12 additions & 7 deletions pkg/porter/cnab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package porter

import (
"context"
"path/filepath"
"testing"

"get.porter.sh/porter/pkg"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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: "",
}, {
Expand All @@ -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",
Expand All @@ -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: "",
}}
Expand Down