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 {
0 commit comments