mirror of
https://github.com/rwinkhart/MUTN.git
synced 2026-09-05 16:47:20 -04:00
Implement initial entry editing support (no notes editing or gen update)
This commit is contained in:
@@ -68,23 +68,21 @@ func main() {
|
|||||||
}
|
}
|
||||||
offline.CopyArgument(targetLocation, field, args[0])
|
offline.CopyArgument(targetLocation, field, args[0])
|
||||||
case "edit":
|
case "edit":
|
||||||
var field rune
|
var field int // indicates which field to edit
|
||||||
field = field // TODO temporary to avoid unused variable error
|
|
||||||
switch args[3] {
|
switch args[3] {
|
||||||
case "password", "-p":
|
case "password", "-p":
|
||||||
field = 'p'
|
field = 0
|
||||||
case "username", "-u":
|
case "username", "-u":
|
||||||
field = 'u'
|
field = 1
|
||||||
case "url", "-l":
|
case "url", "-l":
|
||||||
field = 'l'
|
field = 2
|
||||||
case "note", "-n":
|
case "note", "-n": // TODO cli.EditNote(targetLocation), exit after run
|
||||||
field = 'n'
|
|
||||||
case "rename", "-r":
|
case "rename", "-r":
|
||||||
cli.RenameCli(targetLocation)
|
cli.RenameCli(targetLocation)
|
||||||
default:
|
default:
|
||||||
cli.HelpEdit()
|
cli.HelpEdit()
|
||||||
}
|
}
|
||||||
// TODO offline.EditEntry(targetLocation, field), exit after run
|
cli.EditEntry(targetLocation, true, field)
|
||||||
case "add":
|
case "add":
|
||||||
switch args[3] {
|
switch args[3] {
|
||||||
case "password", "-p":
|
case "password", "-p":
|
||||||
|
|||||||
+2
-10
@@ -40,16 +40,8 @@ func AddEntry(targetLocation string, hidePassword bool, entryType uint8) {
|
|||||||
unencryptedEntry = append([]string{"", "", ""}, note...)
|
unencryptedEntry = append([]string{"", "", ""}, note...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure entry data is not empty
|
// write and preview the new entry
|
||||||
var entryDataPresent bool
|
if offline.EntryIsNotEmpty(unencryptedEntry) {
|
||||||
for _, line := range unencryptedEntry {
|
|
||||||
if line != "" {
|
|
||||||
entryDataPresent = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if entryDataPresent {
|
|
||||||
offline.WriteEntry(targetLocation, unencryptedEntry)
|
offline.WriteEntry(targetLocation, unencryptedEntry)
|
||||||
fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset)
|
fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset)
|
||||||
EntryReader(unencryptedEntry, hidePassword)
|
EntryReader(unencryptedEntry, hidePassword)
|
||||||
|
|||||||
+39
-1
@@ -1,6 +1,10 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import "github.com/rwinkhart/MUTN/src/offline"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/rwinkhart/MUTN/src/offline"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
// RenameCli renames an entry at oldLocation to a new location (user input)
|
// RenameCli renames an entry at oldLocation to a new location (user input)
|
||||||
func RenameCli(oldLocation string) {
|
func RenameCli(oldLocation string) {
|
||||||
@@ -13,3 +17,37 @@ func RenameCli(oldLocation string) {
|
|||||||
|
|
||||||
// exit is done from offline.Rename
|
// exit is done from offline.Rename
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EditEntry edits a field of an entry at targetLocation (user input), does not allow for editing notes
|
||||||
|
func EditEntry(targetLocation string, hidePassword bool, field int) {
|
||||||
|
// ensure targetLocation exists
|
||||||
|
offline.TargetIsFile(targetLocation, true, 2)
|
||||||
|
|
||||||
|
// read old entry data
|
||||||
|
unencryptedEntry := offline.DecryptGPG(targetLocation)
|
||||||
|
|
||||||
|
// ensure slice is long enough for field
|
||||||
|
for len(unencryptedEntry) <= field {
|
||||||
|
unencryptedEntry = append(unencryptedEntry, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// edit the field
|
||||||
|
switch field {
|
||||||
|
case 0:
|
||||||
|
unencryptedEntry[field] = inputHidden("Password:")
|
||||||
|
case 1:
|
||||||
|
unencryptedEntry[field] = input("Username:")
|
||||||
|
case 2:
|
||||||
|
unencryptedEntry[field] = input("URL:")
|
||||||
|
}
|
||||||
|
|
||||||
|
// write and preview the modified entry
|
||||||
|
if offline.EntryIsNotEmpty(unencryptedEntry) {
|
||||||
|
offline.WriteEntry(targetLocation, unencryptedEntry)
|
||||||
|
fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset)
|
||||||
|
EntryReader(unencryptedEntry, hidePassword)
|
||||||
|
} else {
|
||||||
|
fmt.Println(offline.AnsiError + "No data supplied for entry" + offline.AnsiReset)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,14 +7,15 @@ import (
|
|||||||
"golang.org/x/crypto/ssh/terminal"
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// input prompts the user for input and returns the input as a string
|
// input prompts the user for input and returns the input as a string
|
||||||
func input(prompt string) string {
|
func input(prompt string) string {
|
||||||
fmt.Print("\n" + prompt + " ")
|
fmt.Print("\n" + prompt + " ")
|
||||||
var userInput string
|
reader := bufio.NewReader(os.Stdin)
|
||||||
fmt.Scanln(&userInput)
|
userInput, _ := reader.ReadString('\n')
|
||||||
return userInput
|
return strings.TrimRight(userInput, "\n ") // remove trailing newlines and spaces
|
||||||
}
|
}
|
||||||
|
|
||||||
// inputHidden prompts the user for input and returns the input as a string, hiding the input from the terminal
|
// inputHidden prompts the user for input and returns the input as a string, hiding the input from the terminal
|
||||||
|
|||||||
@@ -115,3 +115,12 @@ func StringGen(length int, complex bool, complexity float64) string {
|
|||||||
actualSpecialChars = 0
|
actualSpecialChars = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func EntryIsNotEmpty(entryData []string) bool {
|
||||||
|
for _, line := range entryData {
|
||||||
|
if line != "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user