restructured data storage, values now have their own sub bucket

This commit is contained in:
2024-12-29 18:29:43 +01:00
parent c144e99b41
commit a4b6a3cfdf
12 changed files with 330 additions and 151 deletions

View File

@@ -20,7 +20,6 @@ import (
"errors"
"os"
"strings"
"unicode/utf8"
"github.com/spf13/cobra"
"github.com/tlinden/anydb/app"
@@ -124,12 +123,7 @@ func Get(conf *cfg.Config) *cobra.Command {
return err
}
if utf8.ValidString(string(clear)) {
entry.Value = string(clear)
} else {
entry.Bin = clear
}
entry.Value = clear
entry.Encrypted = false
}
@@ -188,7 +182,7 @@ func List(conf *cfg.Config) *cobra.Command {
)
var cmd = &cobra.Command{
Use: "list [<filter-regex>] [-t <tag>] [-m <mode>] [-n -N] [-T <tpl>] [-i]",
Use: "list [<filter-regex>] [-m <mode>] [-n -N] [-T <tpl>] [-i]",
Short: "List database contents",
Long: `List database contents`,
RunE: func(cmd *cobra.Command, args []string) error {
@@ -221,6 +215,58 @@ func List(conf *cfg.Config) *cobra.Command {
},
}
cmd.PersistentFlags().StringVarP(&conf.Mode, "mode", "m", "", "output format (table|wide|json|template), wide is a verbose table. (default 'table')")
cmd.PersistentFlags().StringVarP(&conf.Template, "template", "T", "", "go template for '-m template'")
cmd.PersistentFlags().BoolVarP(&wide, "wide-output", "l", false, "output mode: wide")
cmd.PersistentFlags().BoolVarP(&conf.NoHeaders, "no-headers", "n", false, "omit headers in tables")
cmd.PersistentFlags().BoolVarP(&conf.NoHumanize, "no-human", "N", false, "do not translate to human readable values")
cmd.PersistentFlags().BoolVarP(&conf.CaseInsensitive, "case-insensitive", "i", false, "filter case insensitive")
cmd.Aliases = append(cmd.Aliases, "ls")
return cmd
}
func Find(conf *cfg.Config) *cobra.Command {
var (
attr app.DbAttr
wide bool
)
var cmd = &cobra.Command{
Use: "find <filter-regex> | -t <tag> [-m <mode>] [-n -N] [-T <tpl>] [-i]",
Short: "Find database contents",
Long: `Find database contents`,
RunE: func(cmd *cobra.Command, args []string) error {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
if len(args) > 0 {
if conf.CaseInsensitive {
attr.Args = []string{"(?i)" + args[0]}
} else {
attr.Args = args
}
}
// turn comma list into slice, if needed
if len(attr.Tags) == 1 && strings.Contains(attr.Tags[0], ",") {
attr.Tags = strings.Split(attr.Tags[0], ",")
}
if wide {
conf.Mode = "wide"
}
entries, err := conf.DB.Find(&attr)
if err != nil {
return err
}
return output.List(os.Stdout, conf, entries)
},
}
cmd.PersistentFlags().StringVarP(&conf.Mode, "mode", "m", "", "output format (table|wide|json|template), wide is a verbose table. (default 'table')")
cmd.PersistentFlags().StringVarP(&conf.Template, "template", "T", "", "go template for '-m template'")
cmd.PersistentFlags().BoolVarP(&wide, "wide-output", "l", false, "output mode: wide")
@@ -230,7 +276,8 @@ func List(conf *cfg.Config) *cobra.Command {
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
cmd.Aliases = append(cmd.Aliases, "/")
cmd.Aliases = append(cmd.Aliases, "ls")
cmd.Aliases = append(cmd.Aliases, "f")
cmd.Aliases = append(cmd.Aliases, "search")
return cmd
}

View File

@@ -23,7 +23,6 @@ import (
"io"
"os"
"os/exec"
"unicode/utf8"
"github.com/spf13/cobra"
"github.com/tlinden/anydb/app"
@@ -199,7 +198,7 @@ func Edit(conf *cfg.Config) *cobra.Command {
return err
}
if len(entry.Value) == 0 && len(entry.Bin) > 0 {
if len(entry.Value) == 0 && entry.Binary {
return errors.New("key contains binary uneditable content")
}
@@ -216,12 +215,7 @@ func Edit(conf *cfg.Config) *cobra.Command {
return err
}
if utf8.ValidString(string(clear)) {
entry.Value = string(clear)
} else {
entry.Bin = clear
}
entry.Value = clear
entry.Encrypted = false
}
@@ -231,7 +225,7 @@ func Edit(conf *cfg.Config) *cobra.Command {
// save file to a temp file, call the editor with it, read
// it back in and compare the content with the original
// one
newcontent, err := editContent(editor, entry.Value)
newcontent, err := editContent(editor, string(entry.Value))
if err != nil {
return err
}
@@ -241,7 +235,7 @@ func Edit(conf *cfg.Config) *cobra.Command {
Key: attr.Key,
Tags: attr.Tags,
Encrypted: attr.Encrypted,
Val: newcontent,
Val: []byte(newcontent),
}
// encrypt if needed

View File

@@ -122,6 +122,7 @@ func Execute() {
// CRUD
rootCmd.AddCommand(Set(&conf))
rootCmd.AddCommand(List(&conf))
rootCmd.AddCommand(Find(&conf))
rootCmd.AddCommand(Get(&conf))
rootCmd.AddCommand(Del(&conf))