This commit is contained in:
2024-12-16 19:11:36 +01:00
commit d1d2328fcd
12 changed files with 550 additions and 0 deletions

9
cmd/anydb.go Normal file
View File

@@ -0,0 +1,9 @@
package cmd
var manpage = `
anydb
FIXME
`
var usage = `
`

68
cmd/maincommands.go Normal file
View File

@@ -0,0 +1,68 @@
package cmd
import (
"errors"
"github.com/spf13/cobra"
"github.com/tlinden/anydb/app"
"github.com/tlinden/anydb/cfg"
)
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 {
return errors.New("No key/value pair specified")
}
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
if len(args) > 0 {
attr.Args = args
}
if err := conf.DB.Set(&attr); err != nil {
return err
}
return conf.DB.Close()
},
}
cmd.PersistentFlags().StringVarP(&attr.File, "file", "r", "", "Filename or - for STDIN")
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
return cmd
}
func Get(conf *cfg.Config) *cobra.Command {
return nil
}
func Del(conf *cfg.Config) *cobra.Command {
return nil
}
func List(conf *cfg.Config) *cobra.Command {
return nil
}
func Find(conf *cfg.Config) *cobra.Command {
return nil
}
func Help(conf *cfg.Config) *cobra.Command {
return nil
}
func Man(conf *cfg.Config) *cobra.Command {
return nil
}

86
cmd/root.go Normal file
View File

@@ -0,0 +1,86 @@
package cmd
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/tlinden/anydb/app"
"github.com/tlinden/anydb/cfg"
)
func completion(cmd *cobra.Command, mode string) error {
switch mode {
case "bash":
return cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
return cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
return cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
default:
return errors.New("Invalid shell parameter! Valid ones: bash|zsh|fish|powershell")
}
}
func Execute() {
var (
conf cfg.Config
ShowVersion bool
ShowCompletion string
)
var rootCmd = &cobra.Command{
Use: "anydb <command> [options]",
Short: "anydb",
Long: `A personal key value store`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
db, err := app.New(conf.Dbfile, conf.Debug)
if err != nil {
return err
}
conf.DB = db
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if ShowVersion {
fmt.Println(cfg.Version)
return nil
}
if len(ShowCompletion) > 0 {
return completion(cmd, ShowCompletion)
}
if len(args) == 0 {
return errors.New("No command specified!")
}
return nil
},
}
// 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.AddCommand(Set(&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()
if err != nil {
os.Exit(1)
}
}