aboutsummaryrefslogtreecommitdiff
path: root/internal/utils
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-07-12 16:41:00 +0530
committerishaan <contact@ishaan.cc>2026-07-12 16:41:00 +0530
commit050db499d18c61d90a68096027ac0d5aaa842f57 (patch)
tree2d0cd6db07407c2d44c8ea7ec899fb3088766d01 /internal/utils
parent7b5328536a82755c2724f2075c31035d709588df (diff)
add init/new/edit subcommands, config, mlock/cleared sensitive info
Diffstat (limited to 'internal/utils')
-rw-r--r--internal/utils/cli.go34
-rw-r--r--internal/utils/utils.go35
2 files changed, 68 insertions, 1 deletions
diff --git a/internal/utils/cli.go b/internal/utils/cli.go
new file mode 100644
index 0000000..773b529
--- /dev/null
+++ b/internal/utils/cli.go
@@ -0,0 +1,34 @@
+package utils
+
+import (
+ "bufio"
+ "fmt"
+ "os"
+ "strings"
+)
+
+func AskUserYesNo(prompt string, defaultVal bool) (bool, error) {
+ suffix := "[y/N]"
+ if defaultVal {
+ suffix = "[Y/n]"
+ }
+
+ scanner := bufio.NewScanner(os.Stdin)
+ fmt.Printf("%s %s ", prompt, suffix)
+ scanner.Scan()
+ if err := scanner.Err(); err != nil {
+ return false, fmt.Errorf("failed to read confirmation: %w", err)
+ }
+
+ input := strings.TrimSpace(scanner.Text())
+ if input == "" {
+ return defaultVal, nil
+ }
+
+ switch strings.ToUpper(input) {
+ case "Y", "YE", "YES":
+ return true, nil
+ default:
+ return false, nil
+ }
+}
diff --git a/internal/utils/utils.go b/internal/utils/utils.go
index 5e6af2d..621b5a0 100644
--- a/internal/utils/utils.go
+++ b/internal/utils/utils.go
@@ -4,6 +4,9 @@ import (
"bytes"
"encoding/gob"
"fmt"
+ "os"
+ "os/signal"
+ "syscall"
"github.com/charmbracelet/x/term"
)
@@ -11,6 +14,10 @@ import (
type FileHeaders struct {
Salt [16]byte
Nonce [12]byte
+
+ // TODO: `Parts (int)` for the number of encrypted parts the file is divided into.
+ // TODO: `Version (string)` for the version of .eno file.
+ // TODO: `EncryptMethod (string)` for the mode of encryption (gpg or passphrase). Maybe even stdout command.
}
func GetFileHeaders(b []byte) (FileHeaders, error) {
@@ -33,8 +40,34 @@ func FileHeadersBytes(headers FileHeaders) ([]byte, error) {
return buf.Bytes(), nil
}
+func TryMlock(b []byte) {
+ if len(b) == 0 {
+ return
+ }
+ if err := syscall.Mlock(b); err != nil {
+ fmt.Fprintf(os.Stderr, "mlock warning: %v\n", err)
+ }
+}
+
func TakeSecureUserInput() ([]byte, error) {
- sensInfo, err := term.ReadPassword(0)
+ fd := os.Stdin.Fd()
+
+ oldState, err := term.GetState(fd)
+ if err != nil {
+ return nil, fmt.Errorf("failed to get terminal state: %w", err)
+ }
+
+ sigCh := make(chan os.Signal, 1)
+ signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
+ defer signal.Stop(sigCh)
+
+ go func() {
+ <-sigCh
+ term.Restore(fd, oldState)
+ os.Exit(130)
+ }()
+
+ sensInfo, err := term.ReadPassword(fd)
fmt.Println()
if err != nil {
return nil, err