Skip to content

Commit 0cae940

Browse files
breaking(compute/pack): use package name from manifest (#1025)
* fix(compute/pack): use package name * test(compute/package): update expected package name * fix(compute/pack): reference manifest from global data * changelog --------- Co-authored-by: Anthony Gomez <anthony.gomez@fastly.com>
1 parent cf55d6a commit 0cae940

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
WAF, which is no longer supported.
2020
[#1419](https://github.com/fastly/cli/pull/1419)
2121
- breaking(language.rust): Switch Rust builds to wasm32-wasip1 instead of wasm32-wasi [#1382](https://github.com/fastly/cli/pull/1382)
22-
23-
- breaking(language.assemblyscript): Remove support for AssemblyScript [#1001](https://github.com/fastly/cli/pull/1001/files)
22+
- breaking(language.assemblyscript): Remove support for AssemblyScript [#1001](https://github.com/fastly/cli/pull/1001)
23+
- breaking(compute/pack): use package name from manifest [#1025](https://github.com/fastly/cli/pull/1025)
2424

2525
### Enhancements:
2626

pkg/commands/compute/pack.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"path/filepath"
88

9+
"github.com/kennygrant/sanitize"
910
"github.com/mholt/archiver/v3"
1011

1112
"github.com/fastly/cli/pkg/argparser"
@@ -41,8 +42,13 @@ func (c *PackCommand) Exec(_ io.Reader, out io.Writer) (err error) {
4142
return err
4243
}
4344

45+
filename := sanitize.BaseName(c.Globals.Manifest.File.Name)
46+
if filename == "" {
47+
filename = "package"
48+
}
49+
4450
defer func(errLog fsterr.LogInterface) {
45-
_ = os.RemoveAll("pkg/package")
51+
_ = os.RemoveAll(fmt.Sprintf("pkg/%s", filename))
4652
if err != nil {
4753
errLog.Add(err)
4854
}
@@ -52,7 +58,7 @@ func (c *PackCommand) Exec(_ io.Reader, out io.Writer) (err error) {
5258
return err
5359
}
5460

55-
bin := "pkg/package/bin/main.wasm"
61+
bin := fmt.Sprintf("pkg/%s/bin/main.wasm", filename)
5662
bindir := filepath.Dir(bin)
5763

5864
err = filesystem.MakeDirectoryIfNotExists(bindir)
@@ -94,7 +100,39 @@ func (c *PackCommand) Exec(_ io.Reader, out io.Writer) (err error) {
94100
Remediation: "Run `fastly compute pack --path </path/to/wasm/binary>` to copy your wasm binary to the required location",
95101
}
96102
}
97-
return nil
103+
104+
src = manifest.Filename
105+
dst = fmt.Sprintf("pkg/%s/%s", filename, manifest.Filename)
106+
if err := filesystem.CopyFile(src, dst); err != nil {
107+
c.Globals.ErrLog.AddWithContext(err, map[string]any{
108+
"Manifest (destination)": dst,
109+
"Manifest (source)": src,
110+
})
111+
return fmt.Errorf("error copying manifest to '%s': %w", dst, err)
112+
}
113+
114+
tar := archiver.NewTarGz()
115+
tar.OverwriteExisting = true
116+
{
117+
dir := fmt.Sprintf("pkg/%s", filename)
118+
src := []string{dir}
119+
dst := fmt.Sprintf("%s.tar.gz", dir)
120+
if err = tar.Archive(src, dst); err != nil {
121+
c.Globals.ErrLog.AddWithContext(err, map[string]any{
122+
"Path (absolute)": src,
123+
"Wasm destination (absolute)": dst,
124+
})
125+
return fmt.Errorf("error copying wasm binary to '%s': %w", dst, err)
126+
}
127+
128+
if !filesystem.FileExists(bin) {
129+
return fsterr.RemediationError{
130+
Inner: fmt.Errorf("no wasm binary found"),
131+
Remediation: "Run `fastly compute pack --path </path/to/wasm/binary>` to copy your wasm binary to the required location",
132+
}
133+
}
134+
return nil
135+
}
98136
})
99137
if err != nil {
100138
return err
@@ -116,7 +154,7 @@ func (c *PackCommand) Exec(_ io.Reader, out io.Writer) (err error) {
116154
return err
117155
}
118156

119-
return spinner.Process("Creating package.tar.gz file", func(_ *text.SpinnerWrapper) error {
157+
return spinner.Process(fmt.Sprintf("Creating %s.tar.gz file", filename), func(_ *text.SpinnerWrapper) error {
120158
tar := archiver.NewTarGz()
121159
tar.OverwriteExisting = true
122160
{

pkg/commands/compute/pack_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ func TestPack(t *testing.T) {
3232
wantOutput: []string{
3333
"Copying wasm binary",
3434
"Copying manifest",
35-
"Creating package.tar.gz file",
35+
"Creating mypackagename.tar.gz file",
3636
},
3737
expectedFiles: [][]string{
38-
{"pkg", "package.tar.gz"},
38+
{"pkg", "mypackagename.tar.gz"},
3939
},
4040
},
4141
{

0 commit comments

Comments
 (0)