add list command, fix set command

This commit is contained in:
2024-12-17 14:23:56 +01:00
parent d1d2328fcd
commit 9e6bbd5419
9 changed files with 381 additions and 31 deletions

View File

@@ -2,10 +2,12 @@ package cmd
import (
"errors"
"os"
"github.com/spf13/cobra"
"github.com/tlinden/anydb/app"
"github.com/tlinden/anydb/cfg"
"github.com/tlinden/anydb/output"
)
func Set(conf *cfg.Config) *cobra.Command {
@@ -52,7 +54,37 @@ func Del(conf *cfg.Config) *cobra.Command {
}
func List(conf *cfg.Config) *cobra.Command {
return nil
var (
attr app.DbAttr
)
var cmd = &cobra.Command{
Use: "list [-t <tag>] [-o <mode>] [<filter-regex>]",
Short: "List database contents",
Long: `List 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 {
attr.Args = args
}
entries, err := conf.DB.List(&attr)
if err != nil {
return err
}
output.List(os.Stdout, conf, entries)
return conf.DB.Close()
},
}
cmd.PersistentFlags().StringVarP(&conf.Mode, "output-mode", "o", "", "output mode: wide, yaml, json, table")
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
return cmd
}
func Find(conf *cfg.Config) *cobra.Command {