Skip to content

Commit

Permalink
Handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Biptaste committed Jun 8, 2022
1 parent 50669e3 commit 34e3acd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
17 changes: 11 additions & 6 deletions actions/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,19 @@ func RestoreActionHandler(ctx domain.ExecutionContext, file string, restoreConfi
fmt.Printf("\n\n")

dpath, dfile := path.Split(file)
fmt.Println("%s --- %s", dpath, dfile)
encryptedExtension := dfile[len(dfile)-4:]
isEncrypted := encryptedExtension == ".enc" && key != nil || *key != ""
isEncrypted := false
encryptedFile := file
// decrypt in an hidden file
decryptedFile := dpath + "." + dfile[:len(dfile)-4]
fmt.Println(encryptedFile, decryptedFile, *key)
decryptedFile := ""
if len(dfile) > 4 {
encryptedExtension := dfile[len(dfile)-4:]
if encryptedExtension != ".enc" && key != nil && *key != "" {
fmt.Printf(" %s This is not a .enc file, skip deciphering\n", color.RedString("✗"))
}
isEncrypted = encryptedExtension == ".enc" && key != nil && *key != ""
decryptedFile = dpath + "." + dfile[:len(dfile)-4]
}

// decrypt in an hidden file
if isEncrypted {
err := decrypt(encryptedFile, decryptedFile, key)
if err != nil {
Expand Down
20 changes: 18 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,11 @@ func main() {
backupDB := cmd.BoolOpt("db", false, "Indicates if DB will be backup")

outputFilename := cmd.StringOpt("o output", "", "Set the filename of the tar.gz")
key := cmd.StringOpt("k", "", "the encryption password (32 characters for a key of 256 bits)")
key := cmd.StringOpt("k", "", "the encryption password (length must be 16, 24 or 32 characters for a key of 128, 192 or 256 bits)")

cmd.Action = func() {
checkKeyLength(key)

if *quiet == false {
backupFiles = nil
backupDB = nil
Expand All @@ -318,11 +320,13 @@ func main() {
restoreConfigFiles := cmd.BoolOpt("config-files", false, "Indicates if config files will be restored")
restoreFiles := cmd.BoolOpt("files", false, "Indicates if files will be restored")
restoreDB := cmd.BoolOpt("db", false, "Indicates if DB will be restored")
key := cmd.StringOpt("k", "", "the encryption password (32 characters for a key of 256 bits)")
key := cmd.StringOpt("k", "", "the encryption password (length must be 16, 24 or 32 characters for a key of 128, 192 or 256 bits)")

file := cmd.StringArg("FILE", "", "A pliz backup file (tar.gz)")

cmd.Action = func() {
checkKeyLength(key)

if *quiet == false {
restoreConfigFiles = nil
restoreFiles = nil
Expand Down Expand Up @@ -424,3 +428,15 @@ func parseAndCheckConfig() {
return
}
}

func checkKeyLength(key *string) {
if key == nil {
return
}

length := len(*key)
if length != 16 && length != 24 && length != 32 {
fmt.Printf(" %s The key length must be 16, 24 or 32 \n", color.RedString("✗"))
cli.Exit(1)
}
}

0 comments on commit 34e3acd

Please sign in to comment.