diff --git a/extra/completion.bash b/extra/completion.bash index 9d5ef04..f4902c5 100644 --- a/extra/completion.bash +++ b/extra/completion.bash @@ -30,7 +30,7 @@ _mutn_completions() { while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "password username url note" -- "$cur" ) ;; edit ) - while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "password username url note relocate" -- "$cur" ) + while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "password username url note rename" -- "$cur" ) ;; gen ) while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "update" -- "$cur" ) diff --git a/extra/completion.zsh b/extra/completion.zsh index b43789e..a1549ea 100644 --- a/extra/completion.zsh +++ b/extra/completion.zsh @@ -22,7 +22,7 @@ case ${words[-2]} in compadd {password,username,url,note} ;; edit ) - compadd {password,username,url,note,relocate} + compadd {password,username,url,note,rename} ;; gen ) compadd update diff --git a/main.go b/main.go index d3aff66..c1e522c 100644 --- a/main.go +++ b/main.go @@ -79,9 +79,10 @@ func main() { field = 'l' case "note", "-n": field = 'n' - case "rename", "-r": // TODO prompt for newLocation, offline.Rename(targetLocation, newLocation), exit after run + case "rename", "-r": + cli.RenameCli(targetLocation) default: - cli.HelpCopy() + cli.HelpEdit() } // TODO offline.EditEntry(targetLocation, field), exit after run case "add": diff --git a/src/cli/edit.go b/src/cli/edit.go new file mode 100644 index 0000000..7787d42 --- /dev/null +++ b/src/cli/edit.go @@ -0,0 +1,15 @@ +package cli + +import "github.com/rwinkhart/MUTN/src/offline" + +// RenameCli renames an entry at oldLocation to a new location (user input) +func RenameCli(oldLocation string) { + // ensure targetLocation exists + offline.TargetIsFile(oldLocation, true, 0) + + // prompt user for new location and rename + newLocation := offline.EntryRoot + offline.PathSeparator + input("New location:") + offline.Rename(oldLocation, newLocation) + + // exit is done from offline.Rename +} diff --git a/src/cli/printInfo.go b/src/cli/printInfo.go index b8ddc1a..62924de 100644 --- a/src/cli/printInfo.go +++ b/src/cli/printInfo.go @@ -37,7 +37,7 @@ This program comes with absolutely no warranty; type "mutn version" for details. note/-n Add a note entry folder/-f Add a new folder for entries edit: - rename/relocate/-r Rename or relocate an entry + rename/-r Rename or relocate an entry username/-u Change the username of an entry password/-p Change the password of an entry url/-l Change the url attached to an entry diff --git a/src/offline/edit.go b/src/offline/edit.go new file mode 100644 index 0000000..8b66979 --- /dev/null +++ b/src/offline/edit.go @@ -0,0 +1,25 @@ +package offline + +import ( + "fmt" + "os" +) + +// Rename renames oldLocation to newLocation +func Rename(oldLocation string, newLocation string) { + // ensure newLocation does not exist + _, isAccessible := TargetIsFile(newLocation, false, 0) + if isAccessible { + fmt.Println(AnsiError + "\"" + newLocation + "\" already exists" + AnsiReset) + os.Exit(1) + } + + // rename oldLocation to newLocation + err := os.Rename(oldLocation, newLocation) + if err != nil { + fmt.Println(AnsiError + "Failed to rename - does the target containing directory exists?" + AnsiReset) + } + + // TODO If in online mode, check if oldLocation is a directory and rename it on the server + os.Exit(0) +}