diff options
author | Martin Polden <mpolden@mpolden.no> | 2023-11-20 14:01:53 +0100 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2023-11-20 14:02:39 +0100 |
commit | 8d953760ece01d539f3612dec62ae21e31406d38 (patch) | |
tree | e5dc62bf9d7e1b2d9e80defca678b3c88fe2024b | |
parent | e8c0a04b67b632ea3f98327d8f39cd0293ad8581 (diff) |
Ensure that vespa prod init modifies source files
-rw-r--r-- | client/go/internal/cli/cmd/cert.go | 2 | ||||
-rw-r--r-- | client/go/internal/cli/cmd/config.go | 4 | ||||
-rw-r--r-- | client/go/internal/cli/cmd/deploy.go | 4 | ||||
-rw-r--r-- | client/go/internal/cli/cmd/prod.go | 4 | ||||
-rw-r--r-- | client/go/internal/cli/cmd/root.go | 4 | ||||
-rw-r--r-- | client/go/internal/vespa/application.go | 18 | ||||
-rw-r--r-- | client/go/internal/vespa/deploy_test.go | 17 |
7 files changed, 34 insertions, 19 deletions
diff --git a/client/go/internal/cli/cmd/cert.go b/client/go/internal/cli/cmd/cert.go index 1cc50b1faea..9668e78bd1c 100644 --- a/client/go/internal/cli/cmd/cert.go +++ b/client/go/internal/cli/cmd/cert.go @@ -153,7 +153,7 @@ func doCertAdd(cli *CLI, overwriteCertificate bool, args []string) error { if err != nil { return err } - pkg, err := cli.applicationPackageFrom(args, false) + pkg, err := cli.applicationPackageFrom(args, vespa.PackageOptions{}) if err != nil { return err } diff --git a/client/go/internal/cli/cmd/config.go b/client/go/internal/cli/cmd/config.go index cfadc6d32c5..a656279549a 100644 --- a/client/go/internal/cli/cmd/config.go +++ b/client/go/internal/cli/cmd/config.go @@ -152,7 +152,7 @@ $ vespa config set --local wait 600 config := cli.config if localArg { // Need an application package in working directory to allow local configuration - if _, err := cli.applicationPackageFrom(nil, false); err != nil { + if _, err := cli.applicationPackageFrom(nil, vespa.PackageOptions{}); err != nil { return fmt.Errorf("failed to write local configuration: %w", err) } config = cli.config.local @@ -188,7 +188,7 @@ $ vespa config unset --local application RunE: func(cmd *cobra.Command, args []string) error { config := cli.config if localArg { - if _, err := cli.applicationPackageFrom(nil, false); err != nil { + if _, err := cli.applicationPackageFrom(nil, vespa.PackageOptions{}); err != nil { return fmt.Errorf("failed to write local configuration: %w", err) } config = cli.config.local diff --git a/client/go/internal/cli/cmd/deploy.go b/client/go/internal/cli/cmd/deploy.go index aee26975901..8806a21c9fc 100644 --- a/client/go/internal/cli/cmd/deploy.go +++ b/client/go/internal/cli/cmd/deploy.go @@ -51,7 +51,7 @@ $ vespa deploy -t cloud -z perf.aws-us-east-1c`, DisableAutoGenTag: true, SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - pkg, err := cli.applicationPackageFrom(args, true) + pkg, err := cli.applicationPackageFrom(args, vespa.PackageOptions{Compiled: true}) if err != nil { return err } @@ -113,7 +113,7 @@ func newPrepareCmd(cli *CLI) *cobra.Command { DisableAutoGenTag: true, SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { - pkg, err := cli.applicationPackageFrom(args, true) + pkg, err := cli.applicationPackageFrom(args, vespa.PackageOptions{Compiled: true}) if err != nil { return fmt.Errorf("could not find application package: %w", err) } diff --git a/client/go/internal/cli/cmd/prod.go b/client/go/internal/cli/cmd/prod.go index 9e78b299e4b..74e4b4c4a1c 100644 --- a/client/go/internal/cli/cmd/prod.go +++ b/client/go/internal/cli/cmd/prod.go @@ -57,7 +57,7 @@ https://cloud.vespa.ai/en/reference/deployment`, if err != nil { return err } - pkg, err := cli.applicationPackageFrom(args, false) + pkg, err := cli.applicationPackageFrom(args, vespa.PackageOptions{SourceOnly: true}) if err != nil { return err } @@ -143,7 +143,7 @@ $ vespa prod deploy`, // TODO: Add support for hosted return fmt.Errorf("prod deploy does not support %s target", target.Type()) } - pkg, err := cli.applicationPackageFrom(args, true) + pkg, err := cli.applicationPackageFrom(args, vespa.PackageOptions{Compiled: true}) if err != nil { return err } diff --git a/client/go/internal/cli/cmd/root.go b/client/go/internal/cli/cmd/root.go index 068a7ed90b6..4d1a7cf6f89 100644 --- a/client/go/internal/cli/cmd/root.go +++ b/client/go/internal/cli/cmd/root.go @@ -586,7 +586,7 @@ func isTerminal(w io.Writer) bool { // applicationPackageFrom returns an application loaded from args. If args is empty, the application package is loaded // from the working directory. If requirePackaging is true, the application package is required to be packaged with mvn // package. -func (c *CLI) applicationPackageFrom(args []string, requirePackaging bool) (vespa.ApplicationPackage, error) { +func (c *CLI) applicationPackageFrom(args []string, options vespa.PackageOptions) (vespa.ApplicationPackage, error) { path := "." if len(args) == 1 { path = args[0] @@ -603,5 +603,5 @@ func (c *CLI) applicationPackageFrom(args []string, requirePackaging bool) (vesp } else if len(args) > 1 { return vespa.ApplicationPackage{}, fmt.Errorf("expected 0 or 1 arguments, got %d", len(args)) } - return vespa.FindApplicationPackage(path, requirePackaging) + return vespa.FindApplicationPackage(path, options) } diff --git a/client/go/internal/vespa/application.go b/client/go/internal/vespa/application.go index cb43578af32..6d28b24100f 100644 --- a/client/go/internal/vespa/application.go +++ b/client/go/internal/vespa/application.go @@ -234,6 +234,14 @@ func copyFile(src *zip.File, dst string) error { return err } +type PackageOptions struct { + // If true, a Maven-based Vespa application package is required to be compiled + Compiled bool + + // If true, only consider the source directores of the application package + SourceOnly bool +} + // FindApplicationPackage finds the path to an application package from the zip file or directory zipOrDir. If // requirePackaging is true, the application package is required to be packaged with mvn package. // @@ -242,8 +250,8 @@ func copyFile(src *zip.File, dst string) error { // 2. target/application // 3. src/main/application // 4. Given path, if it contains services.xml -func FindApplicationPackage(zipOrDir string, requirePackaging bool) (ApplicationPackage, error) { - pkg, err := findApplicationPackage(zipOrDir, requirePackaging) +func FindApplicationPackage(zipOrDir string, options PackageOptions) (ApplicationPackage, error) { + pkg, err := findApplicationPackage(zipOrDir, options) if err != nil { return ApplicationPackage{}, err } @@ -253,20 +261,20 @@ func FindApplicationPackage(zipOrDir string, requirePackaging bool) (Application return pkg, nil } -func findApplicationPackage(zipOrDir string, requirePackaging bool) (ApplicationPackage, error) { +func findApplicationPackage(zipOrDir string, options PackageOptions) (ApplicationPackage, error) { if isZip(zipOrDir) { return ApplicationPackage{Path: zipOrDir}, nil } // Pre-packaged application. We prefer the uncompressed application because this allows us to add // security/clients.pem to the package on-demand hasPOM := util.PathExists(filepath.Join(zipOrDir, "pom.xml")) - if hasPOM { + if hasPOM && !options.SourceOnly { path := filepath.Join(zipOrDir, "target", "application") if util.PathExists(path) { testPath := existingPath(filepath.Join(zipOrDir, "target", "application-test")) return ApplicationPackage{Path: path, TestPath: testPath}, nil } - if requirePackaging { + if options.Compiled { return ApplicationPackage{}, fmt.Errorf("found pom.xml, but %s does not exist: run 'mvn package' first", path) } } diff --git a/client/go/internal/vespa/deploy_test.go b/client/go/internal/vespa/deploy_test.go index d1dffe0f6d6..9dfdc47d8e6 100644 --- a/client/go/internal/vespa/deploy_test.go +++ b/client/go/internal/vespa/deploy_test.go @@ -146,9 +146,9 @@ func TestFindApplicationPackage(t *testing.T) { existingFiles: []string{filepath.Join(dir, "pom.xml"), filepath.Join(dir, "src/test/application/tests/foo.json")}, }) assertFindApplicationPackage(t, dir, pkgFixture{ - existingFile: filepath.Join(dir, "pom.xml"), - requirePackaging: true, - fail: true, + existingFile: filepath.Join(dir, "pom.xml"), + compiled: true, + fail: true, }) assertFindApplicationPackage(t, dir, pkgFixture{ expectedPath: filepath.Join(dir, "target", "application"), @@ -159,6 +159,12 @@ func TestFindApplicationPackage(t *testing.T) { expectedTestPath: filepath.Join(dir, "target", "application-test"), existingFiles: []string{filepath.Join(dir, "target", "application"), filepath.Join(dir, "target", "application-test")}, }) + assertFindApplicationPackage(t, dir, pkgFixture{ + expectedPath: filepath.Join(dir, "src", "main", "application"), + expectedTestPath: filepath.Join(dir, "src", "test", "application"), + existingFiles: []string{filepath.Join(dir, "target", "application"), filepath.Join(dir, "target", "application-test")}, + sourceOnly: true, + }) zip := filepath.Join(dir, "myapp.zip") assertFindApplicationPackage(t, zip, pkgFixture{ expectedPath: zip, @@ -195,7 +201,8 @@ type pkgFixture struct { expectedTestPath string existingFile string existingFiles []string - requirePackaging bool + compiled bool + sourceOnly bool fail bool } @@ -207,7 +214,7 @@ func assertFindApplicationPackage(t *testing.T, zipOrDir string, fixture pkgFixt for _, f := range fixture.existingFiles { writeFile(t, f) } - pkg, err := FindApplicationPackage(zipOrDir, fixture.requirePackaging) + pkg, err := FindApplicationPackage(zipOrDir, PackageOptions{Compiled: fixture.compiled, SourceOnly: fixture.sourceOnly}) assert.Equal(t, err != nil, fixture.fail, "Expected error for "+zipOrDir) assert.Equal(t, fixture.expectedPath, pkg.Path) assert.Equal(t, fixture.expectedTestPath, pkg.TestPath) |