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 {

View File

@@ -66,20 +66,26 @@ func Execute() {
},
}
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
// options
rootCmd.PersistentFlags().BoolVarP(&ShowVersion, "version", "v", false, "Print program version")
rootCmd.PersistentFlags().BoolVarP(&conf.Debug, "debug", "d", false, "Enable debugging")
rootCmd.PersistentFlags().StringVarP(&conf.Dbfile, "dbfile", "f", filepath.Join(os.Getenv("HOME"), ".config", "anydb", "default.db"), "DB file to use")
rootCmd.PersistentFlags().StringVarP(&conf.Dbfile, "dbfile", "f",
filepath.Join(home, ".config", "anydb", "default.db"), "DB file to use")
rootCmd.AddCommand(Set(&conf))
rootCmd.AddCommand(List(&conf))
// rootCmd.AddCommand(Set(&conf))
// rootCmd.AddCommand(Del(&conf))
// rootCmd.AddCommand(List(&conf))
// rootCmd.AddCommand(Find(&conf))
// rootCmd.AddCommand(Help(&conf))
// rootCmd.AddCommand(Man(&conf))
err := rootCmd.Execute()
err = rootCmd.Execute()
if err != nil {
os.Exit(1)
}