mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-28 04:46:42 -04:00
Simplify error handling syntax
This commit is contained in:
+2
-4
@@ -22,8 +22,7 @@ func Entry(vanityPath string, timestamp int64) error {
|
||||
return errors.New("unable to create age file for " + vanityPath + ": " + err.Error())
|
||||
}
|
||||
_ = f.Close() // error ignored; if the file could be created, it can probably be closed
|
||||
err = os.Chtimes(ageFilePath, time.Now(), time.Unix(timestamp, 0))
|
||||
if err != nil {
|
||||
if err = os.Chtimes(ageFilePath, time.Now(), time.Unix(timestamp, 0)); err != nil {
|
||||
return errors.New("unable to set timestamp on age file for " + vanityPath + ": " + err.Error())
|
||||
}
|
||||
return nil
|
||||
@@ -57,8 +56,7 @@ func AllPasswordEntries(forceReage bool) error {
|
||||
// calculate random UNIX timestamp from within the last 365 days
|
||||
offsetInt, _ := rand.Int(rand.Reader, big.NewInt(31557600))
|
||||
randomOffset := time.Duration(offsetInt.Int64()) * time.Second
|
||||
err = Entry(vanityPath, time.Now().Add(-randomOffset).Unix())
|
||||
if err != nil {
|
||||
if err = Entry(vanityPath, time.Now().Add(-randomOffset).Unix()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
+3
-6
@@ -36,15 +36,13 @@ func CopyShortcut(realPath string, field int) error {
|
||||
fmt.Println(back.AnsiWarning + "[Starting]" + back.AnsiReset + " TOTP clipboard refresher")
|
||||
errorChan := make(chan error)
|
||||
go TOTPCopier(decSlice[2], errorChan, nil) // "done" is not needed because the process runs until the program is killed
|
||||
err = <-errorChan
|
||||
if err != nil { // handle error from first copy
|
||||
if err = <-errorChan; err != nil { // handle error from first copy
|
||||
return errors.New("error encountered in TOTP refresh process: " + err.Error())
|
||||
}
|
||||
select {} // block indefinitely
|
||||
} else { // other
|
||||
// copy field to clipboard; launch clipboard clearing process
|
||||
err = CopyString(true, decSlice[field])
|
||||
if err != nil {
|
||||
if err = CopyString(true, decSlice[field]); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -60,6 +58,5 @@ func ClearArgument() error {
|
||||
if assignedContents == "" {
|
||||
os.Exit(0) // use os.Exit instead of core.Exit, as this function runs out of a background subprocess that is invisible to the user (will never appear in GUI/TUI environment)
|
||||
}
|
||||
err := ClearProcess(assignedContents)
|
||||
return err
|
||||
return ClearProcess(assignedContents)
|
||||
}
|
||||
|
||||
@@ -19,8 +19,7 @@ func ClearProcess(assignedContents string) error {
|
||||
}
|
||||
|
||||
clearClipboard := func() error {
|
||||
err := cmdClear.Run()
|
||||
if err != nil {
|
||||
if err := cmdClear.Run(); err != nil {
|
||||
return errors.New("unable to clear clipboard")
|
||||
}
|
||||
back.Exit(0)
|
||||
@@ -29,8 +28,7 @@ func ClearProcess(assignedContents string) error {
|
||||
|
||||
// if assignedContents is empty, clear the clipboard immediately and unconditionally
|
||||
if assignedContents == "" {
|
||||
err := clearClipboard()
|
||||
if err != nil {
|
||||
if err := clearClipboard(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
+1
-2
@@ -13,8 +13,7 @@ import (
|
||||
func CopyString(clearClipboardAutomatically bool, copySubject string) error {
|
||||
cmd := exec.Command("pbcopy")
|
||||
_ = back.WriteToStdin(cmd, copySubject)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
if err := cmd.Run(); err != nil {
|
||||
return errors.New("unable to copy to clipboard: " + err.Error())
|
||||
}
|
||||
if clearClipboardAutomatically {
|
||||
|
||||
+1
-2
@@ -13,8 +13,7 @@ import (
|
||||
func CopyString(clearClipboardAutomatically bool, copySubject string) error {
|
||||
cmd := exec.Command("termux-clipboard-set")
|
||||
_ = back.WriteToStdin(cmd, copySubject)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
if err := cmd.Run(); err != nil {
|
||||
return errors.New("unable to copy to clipboard: " + err.Error())
|
||||
}
|
||||
if clearClipboardAutomatically {
|
||||
|
||||
+1
-2
@@ -25,8 +25,7 @@ func CopyString(clearClipboardAutomatically bool, copySubject string) error {
|
||||
}
|
||||
|
||||
_ = back.WriteToStdin(cmdCopy, copySubject)
|
||||
err = cmdCopy.Run()
|
||||
if err != nil {
|
||||
if err = cmdCopy.Run(); err != nil {
|
||||
return errors.New("unable to copy to clipboard: " + err.Error())
|
||||
}
|
||||
if clearClipboardAutomatically {
|
||||
|
||||
+1
-2
@@ -12,8 +12,7 @@ import (
|
||||
// CopyString copies a string to the clipboard.
|
||||
func CopyString(clearClipboardAutomatically bool, copySubject string) error {
|
||||
cmd := exec.Command("powershell.exe", "-c", fmt.Sprintf("echo '%s' | Set-Clipboard", strings.ReplaceAll(copySubject, "'", "''")))
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
if err := cmd.Run(); err != nil {
|
||||
return errors.New("unable to copy to clipboard: " + err.Error())
|
||||
}
|
||||
if clearClipboardAutomatically {
|
||||
|
||||
+1
-2
@@ -17,8 +17,7 @@ func TOTPCopier(secret string, errorChan chan<- error, done <-chan bool) {
|
||||
if err != nil {
|
||||
errorChan <- err
|
||||
}
|
||||
err = CopyString(false, token)
|
||||
if err != nil {
|
||||
if err = CopyString(false, token); err != nil {
|
||||
errorChan <- err
|
||||
}
|
||||
|
||||
|
||||
+2
-4
@@ -30,8 +30,7 @@ func Load() (*CfgT, error) {
|
||||
return nil, errors.New("unable to load libmuttoncfg.json: " + err.Error())
|
||||
}
|
||||
var cfg CfgT
|
||||
err = json.Unmarshal(cfgBytes, &cfg)
|
||||
if err != nil {
|
||||
if err = json.Unmarshal(cfgBytes, &cfg); err != nil {
|
||||
return nil, errors.New("unable to unmarshal libmuttoncfg.json: " + err.Error())
|
||||
}
|
||||
return &cfg, nil
|
||||
@@ -75,8 +74,7 @@ start:
|
||||
if err != nil {
|
||||
return errors.New("unable to marshal new/updated cfg: " + err.Error())
|
||||
}
|
||||
err = os.WriteFile(global.CfgPath, cfgBytes, 0600)
|
||||
if err != nil {
|
||||
if err = os.WriteFile(global.CfgPath, cfgBytes, 0600); err != nil {
|
||||
return errors.New("unable to write new/updated cfg to libmuttoncfg.json: " + err.Error())
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -101,8 +101,7 @@ func LibmuttonInit(inputCB func(prompt string) string, rcwPassword []byte, appen
|
||||
|
||||
// RCWSanityCheckGen generates the RCW sanity check file for libmutton.
|
||||
func RCWSanityCheckGen(password []byte) error {
|
||||
err := wrappers.GenSanityCheck(global.CfgDir+global.PathSeparator+"sanity.rcw", password)
|
||||
if err != nil {
|
||||
if err := wrappers.GenSanityCheck(global.CfgDir+global.PathSeparator+"sanity.rcw", password); err != nil {
|
||||
return errors.New("unable to generate sanity check file: " + err.Error())
|
||||
}
|
||||
return nil
|
||||
|
||||
+8
-16
@@ -28,13 +28,11 @@ func WriteEntry(realPath string, decSlice []string, passwordIsNew bool) error {
|
||||
if decSlice != nil {
|
||||
if passwordIsNew { // update age data when password changes
|
||||
if decSlice[0] != "" { // if the password change was NOT a removal, update the age file
|
||||
err = age.Entry(global.GetVanityPath(realPath), time.Now().Unix())
|
||||
if err != nil {
|
||||
if err = age.Entry(global.GetVanityPath(realPath), time.Now().Unix()); err != nil {
|
||||
return errors.New("unable to update age data: " + err.Error())
|
||||
}
|
||||
} else { // if the password change was a removal, remove the associated age file
|
||||
err = syncclient.ShearRemote(global.GetVanityPath(realPath), true)
|
||||
if err != nil {
|
||||
if err = syncclient.ShearRemote(global.GetVanityPath(realPath), true); err != nil {
|
||||
return errors.New("unable to remove age data: " + err.Error())
|
||||
}
|
||||
}
|
||||
@@ -60,8 +58,7 @@ func EntryRefresh(oldRCWPassword, newRCWPassword []byte, removeOldDir bool) erro
|
||||
return errors.New("unable to refresh entries: \"" + global.EntryRoot + "-old\" already exists")
|
||||
}
|
||||
}
|
||||
err := os.RemoveAll(global.EntryRoot + dirEnd)
|
||||
if err != nil {
|
||||
if err := os.RemoveAll(global.EntryRoot + dirEnd); err != nil {
|
||||
return errors.New("unable to remove \"" + global.EntryRoot + dirEnd + "\": " + err.Error())
|
||||
}
|
||||
}
|
||||
@@ -73,8 +70,7 @@ func EntryRefresh(oldRCWPassword, newRCWPassword []byte, removeOldDir bool) erro
|
||||
}
|
||||
for _, folder := range folders {
|
||||
fullPath := global.EntryRoot + "-new" + strings.ReplaceAll(folder, "/", global.PathSeparator)
|
||||
err := os.MkdirAll(fullPath, 0700)
|
||||
if err != nil {
|
||||
if err := os.MkdirAll(fullPath, 0700); err != nil {
|
||||
return errors.New("unable to create temporary directory \"" + fullPath + "\": " + err.Error())
|
||||
}
|
||||
}
|
||||
@@ -118,25 +114,21 @@ func EntryRefresh(oldRCWPassword, newRCWPassword []byte, removeOldDir bool) erro
|
||||
encBytes = wrappers.Encrypt([]byte(strings.Join(decryptedEntry, "\n")), newRCWPassword)
|
||||
|
||||
// write the entry to the new directory
|
||||
err = os.WriteFile(global.EntryRoot+"-new"+strings.ReplaceAll(vanityPath, "/", global.PathSeparator), encBytes, 0600)
|
||||
if err != nil {
|
||||
if err = os.WriteFile(global.EntryRoot+"-new"+strings.ReplaceAll(vanityPath, "/", global.PathSeparator), encBytes, 0600); err != nil {
|
||||
return errors.New("unable to write to file: " + err.Error())
|
||||
}
|
||||
|
||||
// generate new sanity check file
|
||||
err = RCWSanityCheckGen(newRCWPassword)
|
||||
if err != nil {
|
||||
if err = RCWSanityCheckGen(newRCWPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// swap the new directory with the old one
|
||||
err = os.Rename(global.EntryRoot, global.EntryRoot+"-old")
|
||||
if err != nil {
|
||||
if err = os.Rename(global.EntryRoot, global.EntryRoot+"-old"); err != nil {
|
||||
return errors.New("unable to rename old directory: " + err.Error())
|
||||
}
|
||||
err = os.Rename(global.EntryRoot+"-new", global.EntryRoot)
|
||||
if err != nil {
|
||||
if err = os.Rename(global.EntryRoot+"-new", global.EntryRoot); err != nil {
|
||||
return errors.New("unable to rename new directory: " + err.Error())
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -71,8 +71,7 @@ func launchRCWDProcess() []byte {
|
||||
if RetryPassword {
|
||||
for {
|
||||
password = global.GetPassword("RCW Password:")
|
||||
err := wrappers.RunSanityCheck(global.CfgDir+global.PathSeparator+"sanity.rcw", password)
|
||||
if err == nil {
|
||||
if err := wrappers.RunSanityCheck(global.CfgDir+global.PathSeparator+"sanity.rcw", password); err == nil {
|
||||
break
|
||||
}
|
||||
fmt.Println(back.AnsiError + "Incorrect password" + back.AnsiReset)
|
||||
|
||||
+4
-8
@@ -11,8 +11,7 @@ import (
|
||||
// Returns: oldDeviceID (from before the directory reset; will be FSMisc if there is no pre-existing ID).
|
||||
func DirInit(preserveOldCfgDir bool) (string, error) {
|
||||
// create EntryRoot
|
||||
err := os.MkdirAll(EntryRoot, 0700)
|
||||
if err != nil {
|
||||
if err := os.MkdirAll(EntryRoot, 0700); err != nil {
|
||||
return "", errors.New("unable to create \"" + EntryRoot + "\": " + err.Error())
|
||||
}
|
||||
|
||||
@@ -26,22 +25,19 @@ func DirInit(preserveOldCfgDir bool) (string, error) {
|
||||
if !preserveOldCfgDir {
|
||||
isAccessible, _ := back.TargetIsFile(CfgDir, false) // error is ignored because dir/file status is irrelevant
|
||||
if isAccessible {
|
||||
err = os.RemoveAll(CfgDir)
|
||||
if err != nil {
|
||||
if err = os.RemoveAll(CfgDir); err != nil {
|
||||
return "", errors.New("unable to remove existing config directory: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// create config directory w/devices subdirectory
|
||||
err = os.MkdirAll(CfgDir+PathSeparator+"devices", 0700)
|
||||
if err != nil {
|
||||
if err = os.MkdirAll(CfgDir+PathSeparator+"devices", 0700); err != nil {
|
||||
return "", errors.New("unable to create \"" + CfgDir + "\": " + err.Error())
|
||||
}
|
||||
|
||||
// create password age directory
|
||||
err = os.MkdirAll(AgeDir, 0700)
|
||||
if err != nil {
|
||||
if err = os.MkdirAll(AgeDir, 0700); err != nil {
|
||||
return "", errors.New("unable to create \"" + AgeDir + "\": " + err.Error())
|
||||
}
|
||||
|
||||
|
||||
+4
-8
@@ -61,8 +61,7 @@ func main() {
|
||||
// stdin[0] is evaluated after fallthrough
|
||||
// stdin[1] is expected to be the OLD vanityPath with FSPath representing path separators - Always pass in UNIX format
|
||||
// stdin[2] is expected to be the NEW vanityPath with FSPath representing path separators - Always pass in UNIX format
|
||||
err := synccommon.RenameLocal(strings.ReplaceAll(stdin[1], global.FSPath, "/"), strings.ReplaceAll(stdin[2], global.FSPath, "/"))
|
||||
if err != nil {
|
||||
if err := synccommon.RenameLocal(strings.ReplaceAll(stdin[1], global.FSPath, "/"), strings.ReplaceAll(stdin[2], global.FSPath, "/")); err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
@@ -88,8 +87,7 @@ func main() {
|
||||
case "addfolder":
|
||||
// add a new folder to the server
|
||||
// stdin[0] is expected to be the vanityPath with FSPath representing path separators - Always pass in UNIX format
|
||||
err := synccommon.AddFolderLocal(strings.ReplaceAll(stdin[0], global.FSPath, "/"))
|
||||
if err != nil {
|
||||
if err := synccommon.AddFolderLocal(strings.ReplaceAll(stdin[0], global.FSPath, "/")); err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
@@ -105,8 +103,7 @@ func main() {
|
||||
_ = f.Close()
|
||||
if stdin[1] != global.FSMisc { // FSMisc is used to indicate that no device ID is being replaced
|
||||
// remove the old device ID file
|
||||
err = os.RemoveAll(global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + stdin[1])
|
||||
if err != nil {
|
||||
if err = os.RemoveAll(global.CfgDir + global.PathSeparator + "devices" + global.PathSeparator + stdin[1]); err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
@@ -120,8 +117,7 @@ func main() {
|
||||
for _, deletion := range deletionsList {
|
||||
affectedIDVanityPath := strings.Split(deletion.Name(), global.FSSpace)
|
||||
if affectedIDVanityPath[0] == stdin[1] {
|
||||
err = os.Rename(deletionsDirRoot+deletion.Name(), deletionsDirRoot+stdin[0]+global.FSSpace+affectedIDVanityPath[1]+global.FSSpace+affectedIDVanityPath[2])
|
||||
if err != nil {
|
||||
if err = os.Rename(deletionsDirRoot+deletion.Name(), deletionsDirRoot+stdin[0]+global.FSSpace+affectedIDVanityPath[1]+global.FSSpace+affectedIDVanityPath[2]); err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
+9
-18
@@ -119,8 +119,7 @@ func getRemoteDataFromClient(sshClient *ssh.Client) (synccommon.EntriesMap, []sy
|
||||
}
|
||||
|
||||
var fetchResp synccommon.FetchResp
|
||||
err = json.Unmarshal(output, &fetchResp)
|
||||
if err != nil {
|
||||
if err = json.Unmarshal(output, &fetchResp); err != nil {
|
||||
fmt.Println(string(output))
|
||||
return nil, nil, 0, 0, errors.New("unable to unmarshal server fetch response: " + err.Error())
|
||||
}
|
||||
@@ -157,8 +156,7 @@ func sftpSync(sshClient *ssh.Client, sshEntryRoot string, sshIsWindows bool, dow
|
||||
fmt.Println("Downloading " + back.AnsiGreen + vanityPath + back.AnsiReset)
|
||||
|
||||
// store path to remote entry
|
||||
var remoteFileRealPath string
|
||||
remoteFileRealPath = getRealPathSFTP(vanityPath, sshEntryRoot, sshIsWindows)
|
||||
remoteFileRealPath := getRealPathSFTP(vanityPath, sshEntryRoot, sshIsWindows)
|
||||
|
||||
// save modification time of remote file
|
||||
var fileInfo os.FileInfo
|
||||
@@ -176,8 +174,7 @@ func sftpSync(sshClient *ssh.Client, sshEntryRoot string, sshIsWindows bool, dow
|
||||
}
|
||||
|
||||
// store path to local file
|
||||
var localFileRealPath string
|
||||
localFileRealPath = global.GetRealPath(vanityPath)
|
||||
localFileRealPath := global.GetRealPath(vanityPath)
|
||||
|
||||
// create local file
|
||||
var localFile *os.File
|
||||
@@ -197,8 +194,7 @@ func sftpSync(sshClient *ssh.Client, sshEntryRoot string, sshIsWindows bool, dow
|
||||
_ = localFile.Close()
|
||||
|
||||
// set the modification time of the local file to match the value saved from the remote file (from before the download)
|
||||
err = os.Chtimes(localFileRealPath, time.Now(), modTime)
|
||||
if err != nil {
|
||||
if err = os.Chtimes(localFileRealPath, time.Now(), modTime); err != nil {
|
||||
return errors.New("unable to set local file modification time: " + err.Error())
|
||||
}
|
||||
}
|
||||
@@ -264,14 +260,12 @@ func sftpSync(sshClient *ssh.Client, sshEntryRoot string, sshIsWindows bool, dow
|
||||
_ = remoteFile.Close()
|
||||
|
||||
// set permissions on remote file
|
||||
err = sftpClient.Chmod(remoteFileRealPath, 0600)
|
||||
if err != nil {
|
||||
if err = sftpClient.Chmod(remoteFileRealPath, 0600); err != nil {
|
||||
return errors.New("unable to set permissions on remote file: " + err.Error())
|
||||
}
|
||||
|
||||
// set the modification time of the remote file to match the value saved from the local file (from before the upload)
|
||||
err = sftpClient.Chtimes(remoteFileRealPath, time.Now(), modTime)
|
||||
if err != nil {
|
||||
if err = sftpClient.Chtimes(remoteFileRealPath, time.Now(), modTime); err != nil {
|
||||
return errors.New("unable to set remote file modification time: " + err.Error())
|
||||
}
|
||||
}
|
||||
@@ -328,8 +322,7 @@ func syncLists(sshClient *ssh.Client, sshEntryRoot string, sshIsWindows bool, ti
|
||||
// call sftpSync with the download and upload lists
|
||||
if timeSyncedErr == nil && (max(len(downloadList), len(uploadList)) > 0) { // only call sftpSync if there are entries to download or upload
|
||||
fmt.Println() // add a gap between list-add messages and the actual sync messages from sftpSync
|
||||
err := sftpSync(sshClient, sshEntryRoot, sshIsWindows, downloadList, uploadList)
|
||||
if err != nil {
|
||||
if err := sftpSync(sshClient, sshEntryRoot, sshIsWindows, downloadList, uploadList); err != nil {
|
||||
return [3][]string{nil, nil, nil}, errors.New("unable to sync entries: " + err.Error())
|
||||
}
|
||||
fmt.Println("Client is synchronized with server")
|
||||
@@ -346,8 +339,7 @@ func deletionSync(deletions []synccommon.Deletion) error {
|
||||
entryDeleted = true // set a flag to indicate that at least one entry has been deleted (used to determine whether to print a gap between deletion and other messages)
|
||||
fmt.Println(synccommon.AnsiDelete+deletion.VanityPath+back.AnsiReset, "has been sheared, removing locally (if it exists)")
|
||||
}
|
||||
err := os.RemoveAll(global.GetRealPath(deletion.VanityPath))
|
||||
if err != nil {
|
||||
if err := os.RemoveAll(global.GetRealPath(deletion.VanityPath)); err != nil {
|
||||
if !deletion.IsAgeFile {
|
||||
return errors.New("unable to shear " + deletion.VanityPath + " locally: " + err.Error())
|
||||
}
|
||||
@@ -382,8 +374,7 @@ func RunJob() ([3][]string, error) {
|
||||
}
|
||||
|
||||
// sync deletions
|
||||
err = deletionSync(deletions)
|
||||
if err != nil {
|
||||
if err = deletionSync(deletions); err != nil {
|
||||
return [3][]string{nil, nil, nil}, errors.New("unable to sync deletions: " + err.Error())
|
||||
}
|
||||
|
||||
|
||||
+9
-14
@@ -58,8 +58,7 @@ func ShearRemote(vanityPath string, onlyShearAgeFile bool) error {
|
||||
}
|
||||
|
||||
// close the SSH client
|
||||
err = sshClient.Close()
|
||||
if err != nil {
|
||||
if err = sshClient.Close(); err != nil {
|
||||
return errors.New("unable to close SSH client: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -74,8 +73,8 @@ end:
|
||||
// It can safely be called in offline mode, as well, so this is the intended
|
||||
// interface for renaming (RenameLocal should only be used directly by the server binary).
|
||||
func RenameRemote(oldVanityPath, newVanityPath string) error {
|
||||
err := synccommon.RenameLocal(oldVanityPath, newVanityPath) // move the target on the local system
|
||||
if err != nil {
|
||||
// move the target on the local system
|
||||
if err := synccommon.RenameLocal(oldVanityPath, newVanityPath); err != nil {
|
||||
return errors.New("unable to rename target locally: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -110,8 +109,7 @@ func RenameRemote(oldVanityPath, newVanityPath string) error {
|
||||
}
|
||||
|
||||
// close the SSH client
|
||||
err = sshClient.Close()
|
||||
if err != nil {
|
||||
if err = sshClient.Close(); err != nil {
|
||||
return errors.New("unable to close SSH client: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -126,8 +124,8 @@ end:
|
||||
// intended interface for adding folders (AddFolderLocal should only be
|
||||
// used directly by the server binary).
|
||||
func AddFolderRemote(vanityPath string) error {
|
||||
err := synccommon.AddFolderLocal(vanityPath) // add the folder on the local system
|
||||
if err != nil {
|
||||
// add the folder on the local system
|
||||
if err := synccommon.AddFolderLocal(vanityPath); err != nil {
|
||||
return errors.New("unable to add folder locally: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -151,8 +149,7 @@ func AddFolderRemote(vanityPath string) error {
|
||||
}
|
||||
|
||||
// close the SSH client
|
||||
err = sshClient.Close()
|
||||
if err != nil {
|
||||
if err = sshClient.Close(); err != nil {
|
||||
return errors.New("unable to close SSH client: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -209,8 +206,7 @@ func GenDeviceID(oldDeviceID, prefix string) (string, bool, error) {
|
||||
return "", false, errors.New("unable to register device ID with server: " + err.Error())
|
||||
}
|
||||
var registerResp synccommon.RegisterResp
|
||||
err = json.Unmarshal(output, ®isterResp)
|
||||
if err != nil {
|
||||
if err = json.Unmarshal(output, ®isterResp); err != nil {
|
||||
cleanupOnFail()
|
||||
return "", false, errors.New("unable to unmarshal server register response: " + err.Error())
|
||||
}
|
||||
@@ -221,8 +217,7 @@ func GenDeviceID(oldDeviceID, prefix string) (string, bool, error) {
|
||||
_ = sshClient.Close() // ignore error; non-critical/unlikely/not much could be done about it
|
||||
|
||||
// remove old device ID file (locally; may not exist)
|
||||
err = os.RemoveAll(oldDeviceIDPath)
|
||||
if err != nil {
|
||||
if err = os.RemoveAll(oldDeviceIDPath); err != nil {
|
||||
cleanupOnFail()
|
||||
return "", false, errors.New("unable to remove old device ID file (locally): " + err.Error())
|
||||
}
|
||||
|
||||
+8
-16
@@ -123,13 +123,11 @@ func ShearLocal(vanityPath, clientDeviceID string, onlyShearAgeFile bool) (strin
|
||||
}
|
||||
}
|
||||
if !onlyShearAgeFile {
|
||||
err = os.RemoveAll(realPath)
|
||||
if err != nil {
|
||||
if err = os.RemoveAll(realPath); err != nil {
|
||||
return "", false, errors.New("unable to remove local entry (" + vanityPath + "): " + err.Error())
|
||||
}
|
||||
}
|
||||
err = ShearAgeFileLocal(vanityPath)
|
||||
if err != nil {
|
||||
if err = ShearAgeFileLocal(vanityPath); err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
@@ -144,8 +142,7 @@ func ShearLocal(vanityPath, clientDeviceID string, onlyShearAgeFile bool) (strin
|
||||
// ShearAgeFileLocal removes the age file for a vanity path.
|
||||
// This function should only be used directly by the server binary.
|
||||
func ShearAgeFileLocal(vanityPath string) error {
|
||||
err := os.RemoveAll(global.AgeDir + global.PathSeparator + strings.ReplaceAll(vanityPath, "/", global.FSPath))
|
||||
if err != nil {
|
||||
if err := os.RemoveAll(global.AgeDir + global.PathSeparator + strings.ReplaceAll(vanityPath, "/", global.FSPath)); err != nil {
|
||||
return errors.New("unable to remove age file for " + vanityPath + ": " + err.Error())
|
||||
}
|
||||
return nil
|
||||
@@ -167,21 +164,17 @@ func RenameLocal(oldVanityPath, newVanityPath string) error {
|
||||
}
|
||||
|
||||
// rename oldLocation to newLocation
|
||||
err := os.Rename(oldRealPath, newRealPath)
|
||||
if err != nil {
|
||||
if err := os.Rename(oldRealPath, newRealPath); err != nil {
|
||||
return errors.New("unable to rename: " + err.Error())
|
||||
}
|
||||
|
||||
// do the same for the age file (if one exists) - also back up timestamp first
|
||||
var fileInfo os.FileInfo
|
||||
fileInfo, err = os.Stat(oldRealAgePath)
|
||||
fileInfo, err := os.Stat(oldRealAgePath)
|
||||
if err == nil { // assume age file does not exist if os.Stat errors
|
||||
err = os.Rename(oldRealAgePath, newRealAgePath)
|
||||
if err != nil {
|
||||
if err = os.Rename(oldRealAgePath, newRealAgePath); err != nil {
|
||||
return errors.New("unable to rename: " + err.Error())
|
||||
}
|
||||
err = os.Chtimes(newRealAgePath, time.Now(), fileInfo.ModTime())
|
||||
if err != nil {
|
||||
if err = os.Chtimes(newRealAgePath, time.Now(), fileInfo.ModTime()); err != nil {
|
||||
return errors.New("unable to set timestamp on age file for " + newVanityPath + ": " + err.Error())
|
||||
}
|
||||
}
|
||||
@@ -195,8 +188,7 @@ func RenameLocal(oldVanityPath, newVanityPath string) error {
|
||||
func AddFolderLocal(vanityPath string) error {
|
||||
// create the target locally
|
||||
realPath := global.GetRealPath(vanityPath)
|
||||
err := os.Mkdir(realPath, 0700)
|
||||
if err != nil {
|
||||
if err := os.Mkdir(realPath, 0700); err != nil {
|
||||
if os.IsExist(err) {
|
||||
fmt.Println(back.AnsiBlue + "Directory already exists - libmutton will still ensure it exists on the server")
|
||||
} else {
|
||||
|
||||
@@ -16,6 +16,10 @@ import (
|
||||
func GetRemoteDataFromServer(clientDeviceID string) {
|
||||
// collect info
|
||||
entryMap, err := synccommon.GetAllEntryData()
|
||||
if err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
deletionsList, err := os.ReadDir(global.CfgDir + global.PathSeparator + "deletions")
|
||||
if err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
@@ -38,8 +42,7 @@ func GetRemoteDataFromServer(clientDeviceID string) {
|
||||
fetchResp.Deletions = append(fetchResp.Deletions, synccommon.Deletion{VanityPath: strings.ReplaceAll(affectedIDVanityPath[2], global.FSPath, "/"), IsAgeFile: isAgeFile})
|
||||
|
||||
// assume successful client deletion and remove deletions file (if assumption is somehow false, worst case scenario is that the client will re-upload the deleted entry)
|
||||
err = os.RemoveAll(global.CfgDir + global.PathSeparator + "deletions" + global.PathSeparator + deletion.Name()) // error ignored; function not run from a user-facing argument and thus the error would not be visible
|
||||
if err != nil {
|
||||
if err = os.RemoveAll(global.CfgDir + global.PathSeparator + "deletions" + global.PathSeparator + deletion.Name()); err != nil {
|
||||
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user