lockbox

password manager
Log | Files | Refs | README | LICENSE

commit aeaeeb91893e79d13241bd38c7b470f4063fe95a
parent d17e9b1c731999d3c67f2ed8227a60460977eefa
Author: Sean Enck <sean@ttypty.com>
Date:   Fri, 16 Sep 2022 18:15:12 -0400

support rm globs

Diffstat:
Mcmd/main.go | 36+++++++++++++++++++++++++++++-------
Minternal/store/filesystem.go | 31++++++++++++++++++++-----------
2 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/cmd/main.go b/cmd/main.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/enckse/lockbox/internal/cli" "github.com/enckse/lockbox/internal/dump" @@ -126,16 +127,37 @@ func main() { } case "rm": s := store.NewFileSystemStore() - entry := getEntry(s, args, 2) - if !store.PathExists(entry) { - die("does not exists", errors.New("can not delete unknown entry")) + value := args[2] + var deletes []string + confirmText := "entry" + if strings.Contains(value, "*") { + globs, err := s.Globs(value) + if err != nil { + die("rm glob failed", err) + } + if len(globs) > 1 { + confirmText = "entries" + } + deletes = append(deletes, globs...) + } else { + deletes = []string{getEntry(s, args, 2)} } - if confirm("remove entry") { - if err := os.Remove(entry); err != nil { - die("unable to remove entry", err) + if len(deletes) == 0 { + die("nothing to delete", errors.New("no files to remove")) + } + if confirm(fmt.Sprintf("remove %s", confirmText)) { + for _, entry := range deletes { + if !store.PathExists(entry) { + die("does not exists", errors.New("can not delete unknown entry")) + } + } + for _, entry := range deletes { + if err := os.Remove(entry); err != nil { + die("unable to remove entry", err) + } } hooks.Run(hooks.Remove, hooks.PostStep) - if err := s.GitRemove(entry); err != nil { + if err := s.GitRemove(deletes); err != nil { die("failed to git remove", err) } } diff --git a/internal/store/filesystem.go b/internal/store/filesystem.go @@ -38,6 +38,11 @@ func NewFileSystemStore() FileSystem { return FileSystem{path: os.Getenv(inputs.StoreEnv)} } +// Globs will return any globs from the input path from within the store. +func (s FileSystem) Globs(inputPath string) ([]string, error) { + return filepath.Glob(filepath.Join(s.path, inputPath)) +} + // List will get all lockbox files in a store. func (s FileSystem) List(options ViewOptions) ([]string, error) { var results []string @@ -112,15 +117,15 @@ func PathExists(path string) bool { // GitCommit is for adding/changing entities func (s FileSystem) GitCommit(entry string) error { - return s.gitAction("add", entry) + return s.gitAction("add", []string{entry}) } // GitRemove is for removing entities -func (s FileSystem) GitRemove(entry string) error { - return s.gitAction("rm", entry) +func (s FileSystem) GitRemove(entries []string) error { + return s.gitAction("rm", entries) } -func (s FileSystem) gitAction(action, entry string) error { +func (s FileSystem) gitAction(action string, entries []string) error { ok, err := inputs.IsGitEnabled() if err != nil { return err @@ -131,14 +136,18 @@ func (s FileSystem) gitAction(action, entry string) error { if !PathExists(filepath.Join(s.path, ".git")) { return nil } - useEntry, err := filepath.Rel(s.path, entry) - if err != nil { - return err - } - if err := s.gitRun(action, useEntry); err != nil { - return err + var message []string + for _, entry := range entries { + useEntry, err := filepath.Rel(s.path, entry) + if err != nil { + return err + } + if err := s.gitRun(action, useEntry); err != nil { + return err + } + message = append(message, fmt.Sprintf("lb %s: %s", action, useEntry)) } - return s.gitRun("commit", "-m", fmt.Sprintf("lb %s: %s", action, useEntry)) + return s.gitRun("commit", "-m", strings.Join(message, "\n")) } func (s FileSystem) gitRun(args ...string) error {