Skip to content

Commit

Permalink
Better handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vwxyzjn committed Oct 24, 2018
1 parent 76386d8 commit 67266e3
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions cmd/portwarden/portwarden.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ func main() {
if len(passphrase) == 0 {
return errors.New(ErrNoPhassPhraseProvided)
}
encryptBackup(filename, passphrase)
err := encryptBackup(filename, passphrase)
if err != nil {
fmt.Println("encryption failed: " + err.Error())
return err
}
fmt.Println("encrypted export successful")
return nil
},
},
Expand All @@ -87,7 +92,12 @@ func main() {
if len(filename) == 0 {
return errors.New(ErrNoFilenameProvided)
}
decryptBackup(filename, passphrase)
err := decryptBackup(filename, passphrase)
if err != nil {
fmt.Println("encryption failed: " + err.Error())
return err
}
fmt.Println("decryption successful")
return nil
},
},
Expand All @@ -103,68 +113,63 @@ func main() {

}

func encryptBackup(fileName, passphrase string) {
func encryptBackup(fileName, passphrase string) error {
if !strings.HasSuffix(fileName, ".portwarden") {
fileName += ".portwarden"
}

pwes := []portwarden.PortWardenElement{}
sessionKey, err := BWGetSessionKey()
if err != nil {
fmt.Println("encryption failed: " + err.Error())
return
return err
}

// save formmated json to "main.json"
rawByte := BWListItemsRawBytes(sessionKey)
if err := json.Unmarshal(rawByte, &pwes); err != nil {
fmt.Println("encryption failed: " + err.Error())
return
return err
}
err = BWGetAllAttachments(BackupFolderName, sessionKey, pwes)
if err != nil {
fmt.Println("encryption failed: " + err.Error())
return
return err
}
formattedByte := pretty.Pretty(rawByte)
if err := ioutil.WriteFile(BackupFolderName+"main.json", formattedByte, 0644); err != nil {
fmt.Println("encryption failed: " + err.Error())
return
return err
}

var b bytes.Buffer
writer := bufio.NewWriter(&b)
err = archiver.Zip.Write(writer, []string{BackupFolderName})
if err != nil {
fmt.Println("encryption failed: " + err.Error())
return
return err
}

// derive a key from the master password
err = portwarden.EncryptFile(fileName, b.Bytes(), passphrase)
if err != nil {
fmt.Println("encryption failed: " + err.Error())
return
return err
}

// cleanup: delete temporary files
err = os.RemoveAll(BackupFolderName)
if err != nil {
fmt.Println("encryption failed: " + err.Error())
return
return err
}
return nil
}

func decryptBackup(fileName, passphrase string) {
func decryptBackup(fileName, passphrase string) error {
tb, err := portwarden.DecryptFile(fileName, passphrase)
if err != nil {
fmt.Println("decryption failed: " + err.Error())
return
return err
}
if err := ioutil.WriteFile(fileName+".decrypted"+".zip", tb, 0644); err != nil {
fmt.Println("decryption failed: " + err.Error())
return
return err
}
return nil
}

func extractSessionKey(stdout string) (string, error) {
Expand Down

0 comments on commit 67266e3

Please sign in to comment.