Skip to content
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Enhancements:

### Dependencies:
- refactor(deps): migrate from `mholt/archiver/v3` to `mholt/archives` v0.1.5 ([#1787](https://github.com/fastly/cli/pull/1787))
- build(deps): `golang.org/x/sys` from 0.43.0 to 0.44.0 ([#1785](https://github.com/fastly/cli/pull/1785))
- build(deps): `golang.org/x/term` from 0.42.0 to 0.43.0 ([#1785](https://github.com/fastly/cli/pull/1785))
- build(deps): `golang.org/x/crypto` from 0.50.0 to 0.51.0 ([#1785](https://github.com/fastly/cli/pull/1785))
Expand Down
18 changes: 13 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
github.com/fsnotify/fsnotify v1.10.1
github.com/google/go-cmp v0.7.0
github.com/mattn/go-isatty v0.0.22 // indirect
github.com/mholt/archiver/v3 v3.5.1
github.com/mitchellh/go-wordwrap v1.0.1
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c
github.com/nicksnyder/go-i18n v1.10.3 // indirect
Expand All @@ -39,23 +38,33 @@ require (
)

require (
github.com/STARRY-S/zip v0.2.3 // indirect
github.com/bodgit/plumbing v1.3.0 // indirect
github.com/bodgit/sevenzip v1.6.1 // indirect
github.com/bodgit/windows v1.0.1 // indirect
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
github.com/dnaeon/go-vcr v1.2.0 // indirect
github.com/go-jose/go-jose/v4 v4.1.4 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mikelolasagasti/xz v1.0.1 // indirect
github.com/minio/minlz v1.0.1 // indirect
github.com/nwaples/rardecode/v2 v2.2.0 // indirect
github.com/otiai10/mint v1.6.3 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/sorairolake/lzip-go v0.3.8 // indirect
github.com/spf13/afero v1.15.0 // indirect
github.com/stretchr/testify v1.11.1 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

require (
github.com/andybalholm/brotli v1.2.1 // indirect
github.com/coreos/go-oidc/v3 v3.18.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/dsnet/compress v0.0.2-0.20230904184137-39efe44ab707 // indirect
github.com/go-jose/go-jose/v3 v3.0.5 // indirect
github.com/golang/snappy v1.0.0 // indirect
github.com/google/go-querystring v1.2.0 // indirect
github.com/google/jsonapi v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand All @@ -65,12 +74,10 @@ require (
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-runewidth v0.0.23 // indirect
github.com/nwaples/rardecode v1.1.3 // indirect
github.com/peterhellberg/link v1.2.0 // indirect
github.com/pierrec/lz4/v4 v4.1.26 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/ulikunitz/xz v0.5.15 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
golang.org/x/net v0.53.0 // indirect
golang.org/x/oauth2 v0.36.0 // indirect
golang.org/x/sync v0.20.0 // indirect
Expand All @@ -83,5 +90,6 @@ require (
4d63.com/optional v0.2.0
github.com/creack/pty v1.1.24
github.com/fastly/go-fastly/v15 v15.0.1
github.com/mholt/archives v0.1.5
github.com/mitchellh/go-ps v1.0.0
)
247 changes: 230 additions & 17 deletions go.sum

Large diffs are not rendered by default.

47 changes: 42 additions & 5 deletions pkg/commands/compute/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compute

import (
"bufio"
"context"
"crypto/rand"
"encoding/json"
"errors"
Expand All @@ -17,7 +18,7 @@ import (
"time"

"github.com/kennygrant/sanitize"
"github.com/mholt/archiver/v3"
"github.com/mholt/archives"
"golang.org/x/text/cases"
textlang "golang.org/x/text/language"

Expand Down Expand Up @@ -785,11 +786,47 @@ func CreatePackageArchive(files []string, destination string) error {
}
}

tar := archiver.NewTarGz()
tar.OverwriteExisting = true //
tar.MkdirAll = true // make destination directory if it doesn't exist
return createTarGz(dir, destination)
}

// createTarGz creates a .tar.gz archive from a directory.
func createTarGz(sourceDir, destFile string) error {
ctx := context.Background()

return tar.Archive([]string{dir}, destination)
// Map files from disk
files, err := archives.FilesFromDisk(ctx, nil, map[string]string{
sourceDir: "",
})
if err != nil {
return fmt.Errorf("failed to map files from disk: %w", err)
}

// Ensure parent directory exists
destDir := filepath.Dir(destFile)
if err := os.MkdirAll(destDir, 0o755); err != nil {
return fmt.Errorf("failed to create destination directory: %w", err)
}

// Create output file
out, err := os.Create(destFile)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer out.Close()

// Create compressed archive format
format := archives.CompressedArchive{
Compression: archives.Gz{},
Archival: archives.Tar{},
}

// Create the archive
err = format.Archive(ctx, out, files)
if err != nil {
return fmt.Errorf("failed to create archive: %w", err)
}

return nil
}

// FileNameWithoutExtension returns a filename with its extension stripped.
Expand Down
38 changes: 29 additions & 9 deletions pkg/commands/compute/compute_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package compute_test

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

"github.com/mholt/archiver/v3"
"github.com/mholt/archives"

"github.com/fastly/kingpin"

Expand Down Expand Up @@ -229,17 +230,36 @@ func TestCreatePackageArchive(t *testing.T) {
testutil.AssertNoError(t, err)

var files, directories []string
if err := archiver.Walk(destination, func(f archiver.File) error {
if f.IsDir() {
directories = append(directories, f.Name())
} else {
files = append(files, f.Name())
}
return nil
}); err != nil {

// Walk the archive using archives API
input, err := os.Open(destination)
if err != nil {
t.Fatal(err)
}
defer input.Close()

format, stream, err := archives.Identify(context.Background(), destination, input)
if err != nil {
t.Fatal(err)
}

if ex, ok := format.(archives.Extractor); ok {
err = ex.Extract(context.Background(), stream, func(_ context.Context, f archives.FileInfo) error {
name := filepath.Base(f.NameInArchive)
if f.IsDir() {
directories = append(directories, name)
} else {
files = append(files, name)
}
return nil
})
if err != nil {
t.Fatal(err)
}
} else {
t.Fatal("format does not support extraction")
}

wantDirectories := []string{"cli", "src"}
testutil.AssertEqual(t, wantDirectories, directories)

Expand Down
5 changes: 3 additions & 2 deletions pkg/commands/compute/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"time"

"github.com/kennygrant/sanitize"
"github.com/mholt/archiver/v3"

"github.com/fastly/go-fastly/v15/fastly"

Expand All @@ -24,6 +23,7 @@ import (
"github.com/fastly/cli/pkg/commands/compute/setup"
"github.com/fastly/cli/pkg/debug"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/file"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/internal/beacon"
"github.com/fastly/cli/pkg/lookup"
Expand Down Expand Up @@ -425,7 +425,8 @@ func readManifestFromPackageArchive(data *manifest.Data, packageFlag, manifestFi
}
defer os.RemoveAll(dst)

if err = archiver.Unarchive(packageFlag, dst); err != nil {
// Extract archive using shared utility
if err = file.ExtractArchive(packageFlag, dst, nil); err != nil {
return fmt.Errorf("error extracting package '%s': %w", packageFlag, err)
}

Expand Down
22 changes: 9 additions & 13 deletions pkg/commands/compute/hashfiles.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package compute

import (
"archive/tar"
"bytes"
"crypto/sha512"
"errors"
Expand All @@ -12,7 +11,7 @@ import (
"sort"

"github.com/kennygrant/sanitize"
"github.com/mholt/archiver/v3"
"github.com/mholt/archives"

"github.com/fastly/cli/pkg/argparser"
fsterr "github.com/fastly/cli/pkg/errors"
Expand Down Expand Up @@ -179,18 +178,15 @@ func (c *HashFilesCommand) Build(in io.Reader, out io.Writer) error {
func getFilesHash(pkgPath string) (string, error) {
contents := make(map[string]*bytes.Buffer)

if err := packageFiles(pkgPath, func(f archiver.File) error {
// We want the full path here and not f.Name(), which is only the
// filename.
//
// This is safe to do - we already verified it in packageFiles().
header, ok := f.Header.(*tar.Header)
if !ok {
return errors.New("failed to convert file type into *tar.Header")
}
entry := header.Name
if err := packageFiles(pkgPath, func(f archives.FileInfo) error {
entry := f.NameInArchive
contents[entry] = &bytes.Buffer{}
if _, err := io.Copy(contents[entry], f); err != nil {
rc, err := f.Open()
if err != nil {
return fmt.Errorf("error opening %s: %w", entry, err)
}
defer rc.Close()
if _, err := io.Copy(contents[entry], rc); err != nil {
return fmt.Errorf("error reading %s: %w", entry, err)
}
return nil
Expand Down
29 changes: 10 additions & 19 deletions pkg/commands/compute/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path/filepath"

"github.com/kennygrant/sanitize"
"github.com/mholt/archiver/v3"

"github.com/fastly/cli/pkg/argparser"
fsterr "github.com/fastly/cli/pkg/errors"
Expand Down Expand Up @@ -111,15 +110,12 @@ func (c *PackCommand) Exec(_ io.Reader, out io.Writer) (err error) {
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 {
if err = createTarGz(dir, dst); err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Path (absolute)": src,
"Path (absolute)": dir,
"Wasm destination (absolute)": dst,
})
return fmt.Errorf("error copying wasm binary to '%s': %w", dst, err)
Expand Down Expand Up @@ -155,19 +151,14 @@ func (c *PackCommand) Exec(_ io.Reader, out io.Writer) (err error) {
}

return spinner.Process(fmt.Sprintf("Creating %s.tar.gz file", filename), func(_ *text.SpinnerWrapper) error {
tar := archiver.NewTarGz()
tar.OverwriteExisting = true
{
dir := "pkg/package"
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{
"Tar source": dir,
"Tar destination": dst,
})
return err
}
dir := "pkg/package"
dst := fmt.Sprintf("%s.tar.gz", dir)
if err = createTarGz(dir, dst); err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"Tar source": dir,
"Tar destination": dst,
})
return err
}
return nil
})
Expand Down
Loading
Loading