Skip to content

Commit

Permalink
Add flag to keep original files
Browse files Browse the repository at this point in the history
  • Loading branch information
cynix committed Oct 21, 2024
1 parent 1c2e951 commit 73b73f6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
with:
go-version: stable

- uses: goreleaser/goreleaser-action@v5
- uses: goreleaser/goreleaser-action@v6
with:
distribution: goreleaser
version: latest
Expand Down
2 changes: 2 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 2

project_name: mage

builds:
Expand Down
22 changes: 15 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 73b73f6

Please sign in to comment.