mirror of
https://github.com/rwinkhart/MUTN.git
synced 2026-09-04 08:07:16 -04:00
Implement initial entry adding (partial, missing notes support)
This commit is contained in:
@@ -46,7 +46,7 @@ func main() {
|
|||||||
cli.HelpMain()
|
cli.HelpMain()
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if argsCount == 4 {
|
} else if argsCount >= 4 {
|
||||||
|
|
||||||
// declare entry field to perform target operation on
|
// declare entry field to perform target operation on
|
||||||
|
|
||||||
@@ -84,19 +84,24 @@ func main() {
|
|||||||
}
|
}
|
||||||
// TODO offline.EditEntry(targetLocation, field), exit after run
|
// TODO offline.EditEntry(targetLocation, field), exit after run
|
||||||
case "add":
|
case "add":
|
||||||
var field bool // indicates whether to add a password or note
|
|
||||||
field = field // TODO temporary to avoid unused variable error
|
|
||||||
switch args[3] {
|
switch args[3] {
|
||||||
case "password", "-p":
|
case "password", "-p":
|
||||||
field = true
|
if argsCount == 4 {
|
||||||
case "note", "-n":
|
cli.AddPasswordEntry(targetLocation, true)
|
||||||
field = false
|
} else {
|
||||||
|
switch args[4] {
|
||||||
|
case "show", "-s":
|
||||||
|
cli.AddPasswordEntry(targetLocation, false)
|
||||||
|
default:
|
||||||
|
cli.AddPasswordEntry(targetLocation, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "note", "-n": // TODO cli.AddNoteEntry(targetLocation)
|
||||||
case "folder", "-f":
|
case "folder", "-f":
|
||||||
offline.AddFolder(targetLocation)
|
offline.AddFolder(targetLocation)
|
||||||
default:
|
default:
|
||||||
cli.HelpAdd()
|
cli.HelpAdd()
|
||||||
}
|
}
|
||||||
// TODO offline.AddEntry(targetLocation, field), exit after run
|
|
||||||
case "gen":
|
case "gen":
|
||||||
var field rune
|
var field rune
|
||||||
field = field // TODO temporary to avoid unused variable error
|
field = field // TODO temporary to avoid unused variable error
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/rwinkhart/MUTN/src/offline"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddPasswordEntry prompts the user for all information relevant to a password entry and writes the entry to an encrypted file at targetLocation
|
||||||
|
func AddPasswordEntry(targetLocation string, hidePassword bool) {
|
||||||
|
_, isAccessible := offline.TargetIsFile(targetLocation, false, 0)
|
||||||
|
if isAccessible {
|
||||||
|
fmt.Println(offline.AnsiError + "Target location already exists" + offline.AnsiReset)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
username := input("Username: ")
|
||||||
|
password := inputHidden("Password: ")
|
||||||
|
url := input("URL: ")
|
||||||
|
// TODO implement notes editor
|
||||||
|
notes := ""
|
||||||
|
|
||||||
|
unencryptedEntry := []string{password, username, url, notes}
|
||||||
|
offline.WriteEntry(targetLocation, unencryptedEntry)
|
||||||
|
fmt.Println(ansiBold + "\nEntry Preview:" + offline.AnsiReset)
|
||||||
|
EntryReader(unencryptedEntry, hidePassword)
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// input prompts the user for input and returns the input as a string
|
||||||
|
func input(prompt string) string {
|
||||||
|
fmt.Print("\n" + prompt)
|
||||||
|
var input string
|
||||||
|
fmt.Scanln(&input)
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
|
||||||
|
// inputHidden prompts the user for input and returns the input as a string, hiding the input from the terminal
|
||||||
|
func inputHidden(prompt string) string {
|
||||||
|
fmt.Print("\n" + prompt)
|
||||||
|
byteInput, _ := terminal.ReadPassword(int(os.Stdin.Fd()))
|
||||||
|
password := string(byteInput)
|
||||||
|
fmt.Println()
|
||||||
|
return password
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AddFolder creates a new directory at targetLocation
|
||||||
func AddFolder(targetLocation string) {
|
func AddFolder(targetLocation string) {
|
||||||
// create the directory specified by targetLocation
|
// create the directory specified by targetLocation
|
||||||
err := os.Mkdir(targetLocation, 0700)
|
err := os.Mkdir(targetLocation, 0700)
|
||||||
@@ -20,3 +21,13 @@ func AddFolder(targetLocation string) {
|
|||||||
// TODO If in online mode, create the directory on the server
|
// TODO If in online mode, create the directory on the server
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteEntry writes entryData to an encrypted file at targetLocation
|
||||||
|
func WriteEntry(targetLocation string, entryData []string) {
|
||||||
|
encryptedBytes := EncryptGPG(entryData)
|
||||||
|
err := os.WriteFile(targetLocation, encryptedBytes, 0600)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(AnsiError + "Failed to write to file: " + err.Error() + AnsiReset)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,3 +24,16 @@ func DecryptGPG(targetLocation string) []string {
|
|||||||
}
|
}
|
||||||
return outputSlice
|
return outputSlice
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EncryptGPG encrypts a slice of strings using GPG and returns the encrypted data as a byte slice
|
||||||
|
func EncryptGPG(input []string) []byte {
|
||||||
|
ReadConfig()
|
||||||
|
cmd := exec.Command("gpg", "-q", "-r", Config["gpgID"].(string), "-e")
|
||||||
|
writeToStdin(cmd, strings.Join(input, "\n"))
|
||||||
|
encryptedBytes, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(AnsiError + "Failed to encrypt data - ensure that your GPG key is valid and that you have a valid GPG ID set in libmutton.ini" + AnsiReset)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
return encryptedBytes
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user