From 73b73f6364da8ba990f7c8d05ab7f9d29d42a591 Mon Sep 17 00:00:00 2001 From: cynix Date: Mon, 21 Oct 2024 21:05:00 +1100 Subject: [PATCH] Add flag to keep original files --- .github/workflows/release.yml | 2 +- .goreleaser.yaml | 2 ++ main.go | 22 +++++++++++++++------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5919131..a14ddfd 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -20,7 +20,7 @@ jobs: with: go-version: stable - - uses: goreleaser/goreleaser-action@v5 + - uses: goreleaser/goreleaser-action@v6 with: distribution: goreleaser version: latest diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 38d2bbc..2986e88 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,3 +1,5 @@ +version: 2 + project_name: mage builds: diff --git a/main.go b/main.go index 9e29323..fb7659b 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ func main() { func run() int { version := flag.Bool("version", false, "show version") test := flag.Bool("test", false, "test decryption") + keep := flag.Bool("keep", false, "keep original files") flag.Parse() if *version { @@ -50,8 +51,13 @@ func run() int { } } + if *test && !decrypt { + fmt.Fprintln(os.Stderr, "Cannot test encryption") + return 1 + } + for { - files = doAll(files, decrypt, *test) + files = doAll(files, decrypt, *test, *keep) if len(files) == 0 { return 0 @@ -61,7 +67,7 @@ func run() int { } } -func doAll(files []string, decrypt, test bool) []string { +func doAll(files []string, decrypt, test, keep bool) []string { var failed []string pass, err := readPassphrase(decrypt) @@ -180,14 +186,16 @@ func doAll(files []string, decrypt, test bool) []string { fmt.Fprintf(os.Stderr, "Encrypted %s\n", f) - if _, err = i.Seek(0, 0); err != nil { - fmt.Fprintf(os.Stderr, "Failed to erase %s: %v\n", f, err) - } else if _, err = io.CopyN(i, ZeroReader, s.Size()); err != nil { - fmt.Fprintf(os.Stderr, "Failed to erase %s: %v\n", f, err) + if !keep { + if _, err = i.Seek(0, 0); err != nil { + fmt.Fprintf(os.Stderr, "Failed to erase %s: %v\n", f, err) + } else if _, err = io.CopyN(i, ZeroReader, s.Size()); err != nil { + fmt.Fprintf(os.Stderr, "Failed to erase %s: %v\n", f, err) + } } } - if !test { + if !test && !keep { if err := os.Remove(f); err != nil { fmt.Fprintf(os.Stderr, "Failed to delete %s: %v\n", f, err) }