From 4da0dd4b8b5846849172930739ff982b3f858bc6 Mon Sep 17 00:00:00 2001 From: Integralist Date: Fri, 29 Sep 2023 18:13:25 +0100 Subject: [PATCH 1/3] fix(compute/pack): use package name --- pkg/commands/compute/pack.go | 44 +++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/pkg/commands/compute/pack.go b/pkg/commands/compute/pack.go index 3d551257d..0dd8e0008 100644 --- a/pkg/commands/compute/pack.go +++ b/pkg/commands/compute/pack.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" + "github.com/kennygrant/sanitize" "github.com/mholt/archiver/v3" "github.com/fastly/cli/pkg/cmd" @@ -41,8 +42,13 @@ func (c *PackCommand) Exec(_ io.Reader, out io.Writer) (err error) { return err } + filename := sanitize.BaseName(c.manifest.File.Name) + if filename == "" { + filename = "package" + } + defer func(errLog fsterr.LogInterface) { - _ = os.RemoveAll("pkg/package") + _ = os.RemoveAll(fmt.Sprintf("pkg/%s", filename)) if err != nil { errLog.Add(err) } @@ -52,7 +58,7 @@ func (c *PackCommand) Exec(_ io.Reader, out io.Writer) (err error) { return err } - bin := "pkg/package/bin/main.wasm" + bin := fmt.Sprintf("pkg/%s/bin/main.wasm", filename) bindir := filepath.Dir(bin) err = filesystem.MakeDirectoryIfNotExists(bindir) @@ -94,7 +100,39 @@ func (c *PackCommand) Exec(_ io.Reader, out io.Writer) (err error) { Remediation: "Run `fastly compute pack --path ` to copy your wasm binary to the required location", } } - return nil + + src = manifest.Filename + dst = fmt.Sprintf("pkg/%s/%s", filename, manifest.Filename) + if err := filesystem.CopyFile(src, dst); err != nil { + c.Globals.ErrLog.AddWithContext(err, map[string]any{ + "Manifest (destination)": dst, + "Manifest (source)": src, + }) + return fmt.Errorf("error copying manifest to '%s': %w", dst, err) + } + + tar := archiver.NewTarGz() + tar.OverwriteExisting = true + { + dir := fmt.Sprintf("pkg/%s", filename) + src := []string{dir} + dst := fmt.Sprintf("%s.tar.gz", dir) + if err = tar.Archive(src, dst); err != nil { + c.Globals.ErrLog.AddWithContext(err, map[string]any{ + "Path (absolute)": src, + "Wasm destination (absolute)": dst, + }) + return fmt.Errorf("error copying wasm binary to '%s': %w", dst, err) + } + + if !filesystem.FileExists(bin) { + return fsterr.RemediationError{ + Inner: fmt.Errorf("no wasm binary found"), + Remediation: "Run `fastly compute pack --path ` to copy your wasm binary to the required location", + } + } + return nil + } }) if err != nil { return err From bf38be9c67e6b5859012bb09e46d116ccca80b8d Mon Sep 17 00:00:00 2001 From: Integralist Date: Sun, 1 Oct 2023 11:36:13 +0100 Subject: [PATCH 2/3] test(compute/package): update expected package name --- pkg/commands/compute/pack.go | 2 +- pkg/commands/compute/pack_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/commands/compute/pack.go b/pkg/commands/compute/pack.go index 0dd8e0008..342cac918 100644 --- a/pkg/commands/compute/pack.go +++ b/pkg/commands/compute/pack.go @@ -154,7 +154,7 @@ func (c *PackCommand) Exec(_ io.Reader, out io.Writer) (err error) { return err } - return spinner.Process("Creating package.tar.gz file", func(_ *text.SpinnerWrapper) error { + return spinner.Process(fmt.Sprintf("Creating %s.tar.gz file", filename), func(_ *text.SpinnerWrapper) error { tar := archiver.NewTarGz() tar.OverwriteExisting = true { diff --git a/pkg/commands/compute/pack_test.go b/pkg/commands/compute/pack_test.go index 905cd691e..0d3240bf6 100644 --- a/pkg/commands/compute/pack_test.go +++ b/pkg/commands/compute/pack_test.go @@ -32,10 +32,10 @@ func TestPack(t *testing.T) { wantOutput: []string{ "Copying wasm binary", "Copying manifest", - "Creating package.tar.gz file", + "Creating mypackagename.tar.gz file", }, expectedFiles: [][]string{ - {"pkg", "package.tar.gz"}, + {"pkg", "mypackagename.tar.gz"}, }, }, { From b4ea655c41a3cb80f602583785e3fe0b2ed31a96 Mon Sep 17 00:00:00 2001 From: Integralist Date: Wed, 15 Nov 2023 13:26:54 +0000 Subject: [PATCH 3/3] fix(compute/pack): reference manifest from global data --- pkg/commands/compute/pack.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/compute/pack.go b/pkg/commands/compute/pack.go index 342cac918..e03c599df 100644 --- a/pkg/commands/compute/pack.go +++ b/pkg/commands/compute/pack.go @@ -42,7 +42,7 @@ func (c *PackCommand) Exec(_ io.Reader, out io.Writer) (err error) { return err } - filename := sanitize.BaseName(c.manifest.File.Name) + filename := sanitize.BaseName(c.Globals.Manifest.File.Name) if filename == "" { filename = "package" }