commit a2ff63245c08a95ca04e6b71904905bad2c429dd
parent b6bc86aff04da14873dcd7f3b86225f2198bb923
Author: Sean Enck <sean@ttypty.com>
Date: Tue, 5 Oct 2021 18:14:10 -0400
support show globbing
Diffstat:
3 files changed, 46 insertions(+), 23 deletions(-)
diff --git a/cmd/lb-totp/main.go b/cmd/lb-totp/main.go
@@ -14,11 +14,6 @@ import (
"voidedtech.com/stock"
)
-const (
- beginRed = "\033[1;31m"
- endRed = "\033[0m"
-)
-
func getEnv() string {
return filepath.Join(internal.GetStore(), os.Getenv("LOCKBOX_TOTP"))
}
@@ -97,8 +92,8 @@ func display(token string, clip bool) error {
startColor := ""
endColor := ""
if left < 10 {
- startColor = beginRed
- endColor = endRed
+ startColor = internal.TermBeginRed
+ endColor = internal.TermEndRed
}
if !clip {
outputs = append(outputs, fmt.Sprintf("%s\n %s", tok, code))
diff --git a/cmd/lb/main.go b/cmd/lb/main.go
@@ -171,24 +171,48 @@ func main() {
os.Remove(entry)
}
case "show", "-c", "clip":
- entry := getEntry(store, args, 2)
- if !stock.PathExists(entry) {
- stock.Die("invalid entry", internal.NewLockboxError("entry not found"))
- }
- l, err := internal.NewLockbox("", "", entry)
- if err != nil {
- stock.Die("unable to make lockbox model instance", err)
- }
- decrypt, err := l.Decrypt()
- if err != nil {
- stock.Die("unable to decrypt", err)
+ inEntry := getEntry(store, args, 2)
+ isShow := command == "show"
+ entries := []string{inEntry}
+ isGlob := false
+ if strings.Contains(inEntry, "*") {
+ if !isShow {
+ stock.Die("cannot glob to clipboard", internal.NewLockboxError("bad glob request"))
+ }
+ isGlob = true
+ matches, err := filepath.Glob(inEntry)
+ if err != nil {
+ stock.Die("bad glob", err)
+ }
+ entries = matches
}
- value := strings.TrimSpace(string(decrypt))
- if command == "show" {
- fmt.Println(value)
- return
+ for _, entry := range entries {
+ if !stock.PathExists(entry) {
+ stock.Die("invalid entry", internal.NewLockboxError("entry not found"))
+ }
+ l, err := internal.NewLockbox("", "", entry)
+ if err != nil {
+ stock.Die("unable to make lockbox model instance", err)
+ }
+ decrypt, err := l.Decrypt()
+ if err != nil {
+ stock.Die("unable to decrypt", err)
+ }
+ value := strings.TrimSpace(string(decrypt))
+ if isShow {
+ if isGlob {
+ fileName := strings.ReplaceAll(entry, store, "")
+ if fileName[0] == '/' {
+ fileName = fileName[1:]
+ }
+ fileName = strings.ReplaceAll(fileName, internal.Extension, "")
+ fmt.Printf("%s%s:%s\n", internal.TermBeginRed, fileName, internal.TermEndRed)
+ }
+ fmt.Println(value)
+ continue
+ }
+ internal.CopyToClipboard(value)
}
- internal.CopyToClipboard(value)
case "clear":
idx := 0
val, err := stdin(false)
diff --git a/internal/utils.go b/internal/utils.go
@@ -13,6 +13,10 @@ import (
const (
// Extension is the lockbox file extension.
Extension = ".lb"
+ // TermBeginRed will turn terminal text red.
+ TermBeginRed = "\033[1;31m"
+ // TermEndRed will end red terminal text.
+ TermEndRed = "\033[0m"
)
// GetStore gets the lockbox directory.