Skip to content

Commit

Permalink
Merge pull request #123 from capeprivacy/justin/only-zip-root
Browse files Browse the repository at this point in the history
only zip the base dir and not all the dirs
  • Loading branch information
justin1121 authored Aug 9, 2022
2 parents a789ad2 + 570f4bf commit eaade20
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
18 changes: 3 additions & 15 deletions cmd/cape/cmd/deploy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"archive/zip"
"bytes"
"crypto/rsa"
"crypto/sha256"
Expand Down Expand Up @@ -162,23 +161,12 @@ func Deploy(url string, functionInput string, functionName string, insecure bool

reader = f
} else {
buf := new(bytes.Buffer)
zipRoot := filepath.Base(functionInput)
w := zip.NewWriter(buf)

err = filepath.Walk(functionInput, czip.Walker(w, zipRoot))
if err != nil {
return "", nil, fmt.Errorf("zipping directory failed: %w", err)
}

// Explicitly close now so that the bytes are flushed and
// available in buf.Bytes() below.
err = w.Close()
buf, err := czip.Create(functionInput)
if err != nil {
return "", nil, fmt.Errorf("zipping directory failed: %w", err)
return "", nil, err
}

reader = buf
reader = bytes.NewBuffer(buf)
}

id, hash, err := doDeploy(url, functionName, reader, insecure, pcrSlice)
Expand Down
4 changes: 2 additions & 2 deletions cmd/cape/cmd/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestBadFunction(t *testing.T) {
t.Fatal(errors.New("received no error when we should have"))
}

if got, want := stderr.String(), "Error: zipping directory failed: lstat testdata/notafunction: no such file or directory\n"; got != want {
if got, want := stderr.String(), "Error: zipping directory failed: lstat notafunction: no such file or directory\n"; got != want {
t.Errorf("didn't get expected stderr, got %s, wanted %s", got, want)
}

Expand Down Expand Up @@ -163,7 +163,7 @@ func TestSuccess(t *testing.T) {
t.Fatalf("didn't get expected stdout, got %s, wanted %s", got, want)
}

want, err := czip.Create("testdata/my_fn")
want, err := czip.Create("./testdata/my_fn")
if err != nil {
t.Fatal(err)
}
Expand Down
16 changes: 14 additions & 2 deletions zip/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"path/filepath"
)

func Walker(w *zip.Writer, zipRoot string) filepath.WalkFunc {
func walker(w *zip.Writer) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand Down Expand Up @@ -43,7 +43,19 @@ func Create(input string) ([]byte, error) {
zipRoot := filepath.Base(input)
w := zip.NewWriter(buf)

if err := filepath.Walk(input, Walker(w, zipRoot)); err != nil {
savedDir, err := os.Getwd()
if err != nil {
return nil, err
}

// change dir to just above zip root incase we're many nested directories in
err = os.Chdir(filepath.Join(input, ".."))
if err != nil {
return nil, err
}
defer os.Chdir(savedDir) // nolint: errcheck

if err := filepath.Walk(zipRoot, walker(w)); err != nil {
return nil, fmt.Errorf("zipping directory failed: %w", err)
}

Expand Down
10 changes: 7 additions & 3 deletions zip/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ func TestWalker(t *testing.T) {

zipRoot := filepath.Base(dir)

err = filepath.Walk(dir, Walker(w, zipRoot))
err = os.Chdir(filepath.Join(dir, ".."))
if err != nil {
t.Fatal(err)
}

err = filepath.Walk(zipRoot, walker(w))
if err != nil {
t.Fatal(err)
}
Expand All @@ -39,9 +44,8 @@ func TestWalker(t *testing.T) {
t.Fatalf("there must be one file in zip but there are %d", len(r.File))
}

expectedFileName := "001/file.py"
// we make this in a temp dir so the which will have a wonky prefix
if !strings.HasSuffix(r.File[0].Name, "001/file.py") {
if expectedFileName := "file.py"; !strings.HasSuffix(r.File[0].Name, "file.py") {
t.Fatalf("expected %s got %s", expectedFileName, r.File[0].Name)
}
}

0 comments on commit eaade20

Please sign in to comment.