2024-12-16 19:11:36 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
2024-12-18 18:44:23 +01:00
|
|
|
"bytes"
|
2024-12-16 19:11:36 +01:00
|
|
|
"errors"
|
2024-12-18 18:44:23 +01:00
|
|
|
"fmt"
|
2024-12-17 14:23:56 +01:00
|
|
|
"os"
|
2024-12-18 18:44:23 +01:00
|
|
|
"os/exec"
|
2024-12-18 14:06:21 +01:00
|
|
|
"strings"
|
2024-12-16 19:11:36 +01:00
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
"github.com/tlinden/anydb/app"
|
|
|
|
|
"github.com/tlinden/anydb/cfg"
|
2024-12-17 14:23:56 +01:00
|
|
|
"github.com/tlinden/anydb/output"
|
2024-12-16 19:11:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Set(conf *cfg.Config) *cobra.Command {
|
|
|
|
|
var (
|
|
|
|
|
attr app.DbAttr
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var cmd = &cobra.Command{
|
|
|
|
|
Use: "set <key> [<value> | -r <file>] [-t <tag>]",
|
|
|
|
|
Short: "Insert key/value pair",
|
|
|
|
|
Long: `Insert key/value pair`,
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
if len(args) == 0 {
|
2024-12-18 14:06:21 +01:00
|
|
|
return errors.New("no key/value pair specified")
|
2024-12-16 19:11:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// errors at this stage do not cause the usage to be shown
|
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
|
attr.Args = args
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 14:06:21 +01:00
|
|
|
// turn comma list into slice, if needed
|
|
|
|
|
if len(attr.Tags) == 1 && strings.Contains(attr.Tags[0], ",") {
|
|
|
|
|
attr.Tags = strings.Split(attr.Tags[0], ",")
|
2024-12-16 19:11:36 +01:00
|
|
|
}
|
|
|
|
|
|
2024-12-18 14:06:21 +01:00
|
|
|
return conf.DB.Set(&attr)
|
2024-12-16 19:11:36 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd.PersistentFlags().StringVarP(&attr.File, "file", "r", "", "Filename or - for STDIN")
|
|
|
|
|
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
|
|
|
|
|
|
2024-12-18 14:06:21 +01:00
|
|
|
cmd.Aliases = append(cmd.Aliases, "add")
|
|
|
|
|
cmd.Aliases = append(cmd.Aliases, "s")
|
|
|
|
|
cmd.Aliases = append(cmd.Aliases, "+")
|
|
|
|
|
|
2024-12-16 19:11:36 +01:00
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Get(conf *cfg.Config) *cobra.Command {
|
2024-12-18 14:06:21 +01:00
|
|
|
var (
|
|
|
|
|
attr app.DbAttr
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var cmd = &cobra.Command{
|
|
|
|
|
Use: "get <key> [-o <file>]",
|
|
|
|
|
Short: "Retrieve value for a key",
|
|
|
|
|
Long: `Retrieve value for a key`,
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return errors.New("no key specified")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// errors at this stage do not cause the usage to be shown
|
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
|
attr.Key = args[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry, err := conf.DB.Get(&attr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output.Print(os.Stdout, conf, entry)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd.PersistentFlags().StringVarP(&conf.Mode, "output", "o", "", "output to file")
|
|
|
|
|
|
|
|
|
|
cmd.Aliases = append(cmd.Aliases, "show")
|
|
|
|
|
cmd.Aliases = append(cmd.Aliases, "g")
|
|
|
|
|
cmd.Aliases = append(cmd.Aliases, ".")
|
|
|
|
|
|
|
|
|
|
return cmd
|
2024-12-16 19:11:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Del(conf *cfg.Config) *cobra.Command {
|
2024-12-18 14:06:21 +01:00
|
|
|
var (
|
|
|
|
|
attr app.DbAttr
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var cmd = &cobra.Command{
|
|
|
|
|
Use: "del <key>",
|
|
|
|
|
Short: "Delete key",
|
|
|
|
|
Long: `Delete key and value matching key`,
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
|
return errors.New("No key specified")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// errors at this stage do not cause the usage to be shown
|
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
|
attr.Key = args[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conf.DB.Del(&attr)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd.Aliases = append(cmd.Aliases, "d")
|
|
|
|
|
cmd.Aliases = append(cmd.Aliases, "rm")
|
|
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Export(conf *cfg.Config) *cobra.Command {
|
|
|
|
|
var (
|
|
|
|
|
attr app.DbAttr
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var cmd = &cobra.Command{
|
2024-12-18 18:44:23 +01:00
|
|
|
Use: "export [-o <json filename>]",
|
2024-12-18 14:06:21 +01:00
|
|
|
Short: "Export database to json",
|
|
|
|
|
Long: `Export database to json`,
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
// errors at this stage do not cause the usage to be shown
|
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
|
|
|
|
|
|
conf.Mode = "json"
|
|
|
|
|
|
|
|
|
|
entries, err := conf.DB.List(&attr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output.WriteFile(&attr, conf, entries)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 18:44:23 +01:00
|
|
|
cmd.PersistentFlags().StringVarP(&attr.File, "output", "o", "", "output to file")
|
|
|
|
|
|
2024-12-18 14:06:21 +01:00
|
|
|
cmd.Aliases = append(cmd.Aliases, "dump")
|
|
|
|
|
cmd.Aliases = append(cmd.Aliases, "backup")
|
|
|
|
|
|
|
|
|
|
return cmd
|
2024-12-16 19:11:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func List(conf *cfg.Config) *cobra.Command {
|
2024-12-17 14:23:56 +01:00
|
|
|
var (
|
|
|
|
|
attr app.DbAttr
|
2024-12-18 14:06:21 +01:00
|
|
|
wide bool
|
2024-12-17 14:23:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 14:06:21 +01:00
|
|
|
// 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"
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-17 14:23:56 +01:00
|
|
|
entries, err := conf.DB.List(&attr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 14:06:21 +01:00
|
|
|
return output.List(os.Stdout, conf, entries)
|
2024-12-17 14:23:56 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 14:06:21 +01:00
|
|
|
cmd.PersistentFlags().StringVarP(&conf.Mode, "output-mode", "o", "", "output format (table|wide|json), wide is a verbose table. (default 'table')")
|
|
|
|
|
cmd.PersistentFlags().BoolVarP(&wide, "wide-output", "l", false, "output mode: wide")
|
|
|
|
|
cmd.PersistentFlags().BoolVarP(&conf.NoHeaders, "no-headers", "n", false, "omit headers in tables")
|
2024-12-17 14:23:56 +01:00
|
|
|
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
|
|
|
|
|
|
2024-12-18 14:06:21 +01:00
|
|
|
cmd.Aliases = append(cmd.Aliases, "/")
|
|
|
|
|
cmd.Aliases = append(cmd.Aliases, "ls")
|
|
|
|
|
|
2024-12-17 14:23:56 +01:00
|
|
|
return cmd
|
2024-12-16 19:11:36 +01:00
|
|
|
}
|
|
|
|
|
|
2024-12-18 14:06:21 +01:00
|
|
|
func Import(conf *cfg.Config) *cobra.Command {
|
|
|
|
|
var (
|
|
|
|
|
attr app.DbAttr
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var cmd = &cobra.Command{
|
|
|
|
|
Use: "import [<json file>]",
|
|
|
|
|
Short: "Import database dump",
|
|
|
|
|
Long: `Import database dump`,
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
// errors at this stage do not cause the usage to be shown
|
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
|
|
|
|
|
|
return conf.DB.Import(&attr)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd.PersistentFlags().StringVarP(&attr.File, "file", "r", "", "Filename or - for STDIN")
|
|
|
|
|
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
|
|
|
|
|
|
|
|
|
|
cmd.Aliases = append(cmd.Aliases, "add")
|
|
|
|
|
cmd.Aliases = append(cmd.Aliases, "s")
|
|
|
|
|
cmd.Aliases = append(cmd.Aliases, "+")
|
|
|
|
|
|
|
|
|
|
return cmd
|
2024-12-16 19:11:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Help(conf *cfg.Config) *cobra.Command {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Man(conf *cfg.Config) *cobra.Command {
|
2024-12-18 18:44:23 +01:00
|
|
|
var cmd = &cobra.Command{
|
|
|
|
|
Use: "man",
|
|
|
|
|
Short: "show manual page",
|
|
|
|
|
Long: `show manual page`,
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
// errors at this stage do not cause the usage to be shown
|
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
|
|
|
|
|
|
man := exec.Command("less", "-")
|
|
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
|
|
|
|
|
b.WriteString(manpage)
|
|
|
|
|
|
|
|
|
|
man.Stdout = os.Stdout
|
|
|
|
|
man.Stdin = &b
|
|
|
|
|
man.Stderr = os.Stderr
|
|
|
|
|
|
|
|
|
|
err := man.Run()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to execute 'less': %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cmd
|
2024-12-16 19:11:36 +01:00
|
|
|
}
|