mirror of
https://github.com/rwinkhart/MUTN.git
synced 2026-09-05 16:47:20 -04:00
Re-write argument parsing, implement skeleton of copy functionality
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/rwinkhart/MUTN/src/cli"
|
"github.com/rwinkhart/MUTN/src/cli"
|
||||||
"github.com/rwinkhart/MUTN/src/offline"
|
"github.com/rwinkhart/MUTN/src/offline"
|
||||||
"os"
|
"os"
|
||||||
@@ -13,43 +12,116 @@ func main() {
|
|||||||
args := os.Args[1:]
|
args := os.Args[1:]
|
||||||
argsCount := len(args)
|
argsCount := len(args)
|
||||||
|
|
||||||
|
// if no arguments are supplied...
|
||||||
if argsCount == 0 {
|
if argsCount == 0 {
|
||||||
|
// list all entries
|
||||||
cli.EntryListGen()
|
cli.EntryListGen()
|
||||||
|
} else {
|
||||||
|
|
||||||
} else if argsCount == 1 {
|
// if the first argument is an entry...
|
||||||
|
|
||||||
// initial entry reader shortcut
|
|
||||||
if strings.HasPrefix(args[0], "/") {
|
if strings.HasPrefix(args[0], "/") {
|
||||||
|
|
||||||
|
// store location of target entry
|
||||||
targetLocation := offline.EntryRoot + offline.PathSeparator + args[0][1:]
|
targetLocation := offline.EntryRoot + offline.PathSeparator + args[0][1:]
|
||||||
if isFile, _ := offline.TargetIsFile(targetLocation, true); isFile {
|
|
||||||
cli.EntryReader(offline.DecryptGPG(targetLocation))
|
// entry reader shortcut (if no other arguments are supplied)
|
||||||
} else {
|
if argsCount == 1 {
|
||||||
fmt.Println(offline.AnsiError + "Failed to read \"" + targetLocation + "\" - it is a directory" + offline.AnsiReset)
|
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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -192,4 +192,6 @@ func EntryListGen() {
|
|||||||
|
|
||||||
// print trailing new lines for proper spacing after entry list is complete
|
// print trailing new lines for proper spacing after entry list is complete
|
||||||
fmt.Print("\n\n")
|
fmt.Print("\n\n")
|
||||||
|
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,11 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/rwinkhart/MUTN/src/offline"
|
"github.com/rwinkhart/MUTN/src/offline"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EntryReader prints the decrypted contents of a libmutton entry in a human-readable format
|
// EntryReader prints the decrypted contents of a libmutton entry in a human-readable format
|
||||||
|
// TODO implement password hiding
|
||||||
func EntryReader(decryptedEntry []string) {
|
func EntryReader(decryptedEntry []string) {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
|
||||||
@@ -54,4 +56,13 @@ func EntryReader(decryptedEntry []string) {
|
|||||||
if notesFlag {
|
if notesFlag {
|
||||||
fmt.Println()
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/rwinkhart/MUTN/src/offline"
|
"github.com/rwinkhart/MUTN/src/offline"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// global constants used only in this file
|
// 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 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 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")
|
` + 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() {
|
func HelpAdd() {
|
||||||
@@ -62,6 +64,7 @@ func HelpAdd() {
|
|||||||
password/-p Add a password entry
|
password/-p Add a password entry
|
||||||
note/-n Add a note entry
|
note/-n Add a note entry
|
||||||
folder/-f Add a new folder for entries` + "\n\n")
|
folder/-f Add a new folder for entries` + "\n\n")
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func HelpEdit() {
|
func HelpEdit() {
|
||||||
@@ -74,6 +77,7 @@ func HelpEdit() {
|
|||||||
password/-p Change the password of an entry
|
password/-p Change the password of an entry
|
||||||
url/-l Change the url attached to an entry
|
url/-l Change the url attached to an entry
|
||||||
note/-n Change the note attached to an entry` + "\n\n")
|
note/-n Change the note attached to an entry` + "\n\n")
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func HelpCopy() {
|
func HelpCopy() {
|
||||||
@@ -85,6 +89,7 @@ func HelpCopy() {
|
|||||||
password/-p Copy the password in an entry to your clipboard
|
password/-p Copy the password in an entry to your clipboard
|
||||||
url/-l Copy the url 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")
|
note/-n Copy the first note line in an entry to your clipboard` + "\n\n")
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func HelpGen() {
|
func HelpGen() {
|
||||||
@@ -95,6 +100,7 @@ func HelpGen() {
|
|||||||
update/-u Generate a password for an existing entry
|
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")
|
` + ansiBold + "Tip:" + offline.AnsiReset + " If no options are provided, a new password entry is generated\n\n")
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Version() {
|
func Version() {
|
||||||
@@ -141,4 +147,5 @@ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|||||||
"\n For more information, see:\n\n" +
|
"\n For more information, see:\n\n" +
|
||||||
" https://github.com/rwinkhart/MUTN\n" +
|
" https://github.com/rwinkhart/MUTN\n" +
|
||||||
" https://github.com/rwinkhart/libmutton\n\n")
|
" https://github.com/rwinkhart/libmutton\n\n")
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user