Re-write argument parsing, implement skeleton of copy functionality

This commit is contained in:
2024-03-01 21:09:56 -05:00
parent 0afcdbb29b
commit 51313eb307
5 changed files with 145 additions and 32 deletions
+104 -32
View File
@@ -1,7 +1,6 @@
package main
import (
"fmt"
"github.com/rwinkhart/MUTN/src/cli"
"github.com/rwinkhart/MUTN/src/offline"
"os"
@@ -13,43 +12,116 @@ func main() {
args := os.Args[1:]
argsCount := len(args)
// if no arguments are supplied...
if argsCount == 0 {
// list all entries
cli.EntryListGen()
} else {
} else if argsCount == 1 {
// initial entry reader shortcut
// if the first argument is an entry...
if strings.HasPrefix(args[0], "/") {
// store location of target entry
targetLocation := offline.EntryRoot + offline.PathSeparator + args[0][1:]
if isFile, _ := offline.TargetIsFile(targetLocation, true); isFile {
cli.EntryReader(offline.DecryptGPG(targetLocation))
} else {
fmt.Println(offline.AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + offline.AnsiReset)
// entry reader shortcut (if no other arguments are supplied)
if argsCount == 1 {
cli.EntryReaderShortcut(targetLocation)
// perform other operations on the entry (if other arguments are supplied)
} else if argsCount == 2 {
switch args[1] {
case "shear": // TODO offline.Shear(targetLocation), exit after run
case "gen": // TODO offline.Gen(targetLocation), exit after run
case "copy":
cli.HelpCopy()
case "edit":
cli.HelpEdit()
case "add":
cli.HelpAdd()
default:
cli.HelpMain()
}
} else if argsCount == 3 {
// declare entry field to perform target operation on
var field rune
switch args[1] {
case "copy":
switch args[2] {
case "password", "-p":
field = 'p'
case "username", "-u":
field = 'u'
case "url", "-l":
field = 'l'
case "note", "-n":
field = 'n'
default:
cli.HelpCopy()
}
offline.CopyField(targetLocation, field)
case "edit":
switch args[2] {
case "password", "-p":
field = 'p'
case "username", "-u":
field = 'u'
case "url", "-l":
field = 'l'
case "note", "-n":
field = 'n'
case "rename", "-r": // TODO prompt for newLocation, offline.Rename(targetLocation, newLocation), exit after run
default:
cli.HelpCopy()
}
offline.CopyField(targetLocation, field)
case "add":
switch args[2] {
case "password", "-p":
field = 'p'
case "note", "-n":
field = 'n'
case "folder", "-f": // TODO offline.AddFolder(targetLocation), exit after run
default:
cli.HelpAdd()
}
// TODO offline.AddEntry(targetLocation, field), exit after run
case "gen":
switch args[2] {
case "update", "-u": // TODO offline.GenUpdate(targetLocation), exit after run
default:
cli.HelpGen()
}
default:
cli.HelpMain()
}
}
// if the first argument is not an entry...
} else {
switch args[0] {
case "sync": // TODO online.Sync(), exit after run
case "init": // TODO offline.Init(), exit after run
case "tweak": // TODO offline.Tweak(), exit after run
case "add":
cli.HelpAdd()
case "edit":
cli.HelpEdit()
case "copy":
cli.HelpCopy()
case "gen":
cli.HelpGen()
case "version", "-v":
cli.Version()
default:
cli.HelpMain()
}
os.Exit(0)
}
switch args[0] {
case "help", "--help", "-h":
cli.HelpMain()
case "add":
cli.HelpAdd()
case "edit":
cli.HelpEdit()
case "copy":
cli.HelpCopy()
case "gen":
cli.HelpGen()
case "version", "-v":
cli.Version()
default:
cli.HelpMain()
}
} else if argsCount > 1 {
// load config (libmutton.ini)
offline.ReadConfig()
}
}
+2
View File
@@ -192,4 +192,6 @@ func EntryListGen() {
// print trailing new lines for proper spacing after entry list is complete
fmt.Print("\n\n")
os.Exit(0)
}
+11
View File
@@ -3,9 +3,11 @@ package cli
import (
"fmt"
"github.com/rwinkhart/MUTN/src/offline"
"os"
)
// EntryReader prints the decrypted contents of a libmutton entry in a human-readable format
// TODO implement password hiding
func EntryReader(decryptedEntry []string) {
fmt.Println()
@@ -54,4 +56,13 @@ func EntryReader(decryptedEntry []string) {
if notesFlag {
fmt.Println()
}
os.Exit(0)
}
func EntryReaderShortcut(targetLocation string) {
if isFile, _ := offline.TargetIsFile(targetLocation, true); isFile {
EntryReader(offline.DecryptGPG(targetLocation))
} else {
fmt.Println(offline.AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + offline.AnsiReset)
}
}
+7
View File
@@ -3,6 +3,7 @@ package cli
import (
"fmt"
"github.com/rwinkhart/MUTN/src/offline"
"os"
)
// global constants used only in this file
@@ -52,6 +53,7 @@ This program comes with absolutely no warranty; type "mutn version" for details.
` + ansiBold + "Tip 1:" + offline.AnsiReset + ` You can quickly read an entry with "mutn /<entry name>"
` + ansiBold + "Tip 2:" + offline.AnsiReset + ` Type "mutn" (no arguments/options) to view a list of saved entries
` + ansiBold + "Tip 3:" + offline.AnsiReset + " Provide \"add\", \"edit\", \"copy\", or \"gen\" as the only argument to receive more specific help\n\n")
os.Exit(0)
}
func HelpAdd() {
@@ -62,6 +64,7 @@ func HelpAdd() {
password/-p Add a password entry
note/-n Add a note entry
folder/-f Add a new folder for entries` + "\n\n")
os.Exit(0)
}
func HelpEdit() {
@@ -74,6 +77,7 @@ func HelpEdit() {
password/-p Change the password of an entry
url/-l Change the url attached to an entry
note/-n Change the note attached to an entry` + "\n\n")
os.Exit(0)
}
func HelpCopy() {
@@ -85,6 +89,7 @@ func HelpCopy() {
password/-p Copy the password in an entry to your clipboard
url/-l Copy the url in an entry to your clipboard
note/-n Copy the first note line in an entry to your clipboard` + "\n\n")
os.Exit(0)
}
func HelpGen() {
@@ -95,6 +100,7 @@ func HelpGen() {
update/-u Generate a password for an existing entry
` + ansiBold + "Tip:" + offline.AnsiReset + " If no options are provided, a new password entry is generated\n\n")
os.Exit(0)
}
func Version() {
@@ -141,4 +147,5 @@ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
"\n For more information, see:\n\n" +
" https://github.com/rwinkhart/MUTN\n" +
" https://github.com/rwinkhart/libmutton\n\n")
os.Exit(0)
}
+21
View File
@@ -0,0 +1,21 @@
package offline
import (
"fmt"
"os"
)
func CopyField(targetLocation string, field rune) {
if isFile, _ := TargetIsFile(targetLocation, true); isFile {
//decryptedEntry := DecryptGPG(targetLocation)
// TODO implement clipboard support (maybe use third-party library?)
fmt.Println("In the future, this will copy " + string(field))
} else {
fmt.Println(AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + AnsiReset)
}
os.Exit(0)
}
func clipClear() {
}