From 570f4bf144438d2ecd0839bf81abdb2eef5b8d99 Mon Sep 17 00:00:00 2001 From: Justin Patriquin Date: Tue, 9 Aug 2022 17:43:32 +0000 Subject: [PATCH] only zip the base dir and not all the dirs --- cmd/cape/cmd/deploy.go | 18 +++--------------- cmd/cape/cmd/test_test.go | 4 ++-- zip/zip.go | 16 ++++++++++++++-- zip/zip_test.go | 10 +++++++--- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/cmd/cape/cmd/deploy.go b/cmd/cape/cmd/deploy.go index 6e5085d8..0a0093a2 100644 --- a/cmd/cape/cmd/deploy.go +++ b/cmd/cape/cmd/deploy.go @@ -1,7 +1,6 @@ package cmd import ( - "archive/zip" "bytes" "crypto/rsa" "crypto/sha256" @@ -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) diff --git a/cmd/cape/cmd/test_test.go b/cmd/cape/cmd/test_test.go index 9f19ba2d..57c1d242 100644 --- a/cmd/cape/cmd/test_test.go +++ b/cmd/cape/cmd/test_test.go @@ -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) } @@ -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) } diff --git a/zip/zip.go b/zip/zip.go index 6db9d95f..8bde6639 100644 --- a/zip/zip.go +++ b/zip/zip.go @@ -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 @@ -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) } diff --git a/zip/zip_test.go b/zip/zip_test.go index 1d7f6d46..d3b22069 100644 --- a/zip/zip_test.go +++ b/zip/zip_test.go @@ -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) } @@ -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) } }