Add overwrite confirmation check if the output file exists

develop
trivernis 5 years ago
parent 5b82b7c06d
commit ee3e75640c

@ -1,6 +1,7 @@
package main package main
import ( import (
"bufio"
"crypto/aes" "crypto/aes"
"crypto/cipher" "crypto/cipher"
"crypto/rand" "crypto/rand"
@ -13,6 +14,7 @@ import (
"os" "os"
"syscall" "syscall"
"math" "math"
"strings"
"golang.org/x/crypto/scrypt" "golang.org/x/crypto/scrypt"
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
@ -45,7 +47,7 @@ func main() {
decryptFlags.StringVar(&imageFile, "image", "image.png", "The path of the png file.") decryptFlags.StringVar(&imageFile, "image", "image.png", "The path of the png file.")
encryptFlags.StringVar(&inputFile, "in", "input.txt","The file with the input data.") encryptFlags.StringVar(&inputFile, "in", "input.txt","The file with the input data.")
encryptFlags.StringVar(&outputFile, "out", "out.png", "The output filename for the image.") encryptFlags.StringVar(&outputFile, "out", "out.png", "The output filename for the image.")
decryptFlags.StringVar(&outputFile, "out", "out.txt", "The output file for the decrypted data.") decryptFlags.StringVar(&outputFile, "out", "out.png", "The output file for the decrypted data.")
flag.Parse() flag.Parse()
switch os.Args[1] { switch os.Args[1] {
case "encrypt": case "encrypt":
@ -55,6 +57,13 @@ func main() {
check(err) check(err)
defer f.Close() defer f.Close()
check(err) check(err)
if _, err := os.Stat(outputFile); err == nil {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("The output file %s exists and will be overwritten. Continue? [Y/n] ", outputFile)
if ans, _ := reader.ReadString('\n'); strings.ToLower(ans) != "y\n" {
log.Fatal("Aborting...")
}
}
fout, err := os.Create(outputFile) fout, err := os.Create(outputFile)
check(err) check(err)
defer fout.Close() defer fout.Close()

Loading…
Cancel
Save