From 46cef6e85a7e2d815fae022375963d64cb2ef7a9 Mon Sep 17 00:00:00 2001 From: Max Brauer Date: Wed, 19 Feb 2025 13:49:12 +0100 Subject: [PATCH] Export cleans up file in case of errors Signed-off-by: Max Brauer --- .github/workflows/e2e.yaml | 7 +++++++ cmd/crane/cmd/export.go | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index dcb345242..159bbdb4d 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -92,3 +92,10 @@ jobs: ./app/crane export - - < ubuntu.tar > filesystem.tar ls -la *.tar + - name: crane export leaves filesystem clean in case of error + shell: bash + run: | + set -euxo pipefail + + ./app/crane export this-image-does-not-exist export.tar + [[ -f export.tar ]] && echo "did not expect export.tar to exist" && exit 1 diff --git a/cmd/crane/cmd/export.go b/cmd/crane/cmd/export.go index 497eb8e09..889218f08 100644 --- a/cmd/crane/cmd/export.go +++ b/cmd/crane/cmd/export.go @@ -40,7 +40,7 @@ func NewCmdExport(options *[]crane.Option) *cobra.Command { # Read image from stdin crane export - ubuntu.tar`, Args: cobra.RangeArgs(1, 2), - RunE: func(_ *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, args []string) (err error) { src, dst := args[0], "-" if len(args) > 1 { dst = args[1] @@ -50,7 +50,12 @@ func NewCmdExport(options *[]crane.Option) *cobra.Command { if err != nil { return fmt.Errorf("failed to open %s: %w", dst, err) } - defer f.Close() + defer func() { + f.Close() + if err != nil { + os.Remove(f.Name()) + } + }() var img v1.Image if src == "-" {