2024-12-21 00:08:16 +01:00
|
|
|
/*
|
|
|
|
|
Copyright © 2024 Thomas von Dein
|
|
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
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-19 10:37:21 +01:00
|
|
|
"unicode/utf8"
|
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-20 18:59:59 +01:00
|
|
|
"github.com/tlinden/anydb/rest"
|
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-19 10:37:21 +01:00
|
|
|
// check if value given as file or via stdin and fill attr accordingly
|
|
|
|
|
if err := attr.ParseKV(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// encrypt?
|
|
|
|
|
if conf.Encrypt {
|
|
|
|
|
pass, err := app.AskForPassword()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = app.Encrypt(pass, &attr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 14:06:21 +01:00
|
|
|
return conf.DB.Set(&attr)
|
2024-12-16 19:11:36 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 10:37:21 +01:00
|
|
|
cmd.PersistentFlags().BoolVarP(&conf.Encrypt, "encrypt", "e", false, "encrypt value")
|
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{
|
2024-12-20 11:53:09 +01:00
|
|
|
Use: "get <key> [-o <file>] [-m <mode>] [-n -N] [-T <tpl>]",
|
2024-12-18 14:06:21 +01:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 10:37:21 +01:00
|
|
|
if entry.Encrypted {
|
|
|
|
|
pass, err := app.AskForPassword()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear, err := app.Decrypt(pass, entry.Value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if utf8.ValidString(string(clear)) {
|
|
|
|
|
entry.Value = string(clear)
|
|
|
|
|
} else {
|
|
|
|
|
entry.Bin = clear
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry.Encrypted = false
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 09:04:55 +01:00
|
|
|
return output.Print(os.Stdout, conf, &attr, entry)
|
2024-12-18 14:06:21 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-20 11:53:09 +01:00
|
|
|
cmd.PersistentFlags().StringVarP(&attr.File, "output", "o", "", "output value to file (ignores -m)")
|
2024-12-19 09:04:55 +01:00
|
|
|
cmd.PersistentFlags().StringVarP(&conf.Mode, "mode", "m", "", "output format (simple|wide|json) (default 'simple')")
|
|
|
|
|
cmd.PersistentFlags().BoolVarP(&conf.NoHeaders, "no-headers", "n", false, "omit headers in tables")
|
2024-12-20 11:53:09 +01:00
|
|
|
cmd.PersistentFlags().BoolVarP(&conf.NoHumanize, "no-human", "N", false, "do not translate to human readable values")
|
|
|
|
|
cmd.PersistentFlags().StringVarP(&conf.Template, "template", "T", "", "go template for '-m template'")
|
2024-12-18 14:06:21 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-20 11:53:09 +01:00
|
|
|
return output.WriteJSON(&attr, conf, entries)
|
2024-12-18 14:06:21 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
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{
|
2024-12-20 11:53:09 +01:00
|
|
|
Use: "list [<filter-regex>] [-t <tag>] [-m <mode>] [-n -N] [-T <tpl>]",
|
2024-12-17 14:23:56 +01:00
|
|
|
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-19 09:04:55 +01:00
|
|
|
cmd.PersistentFlags().StringVarP(&conf.Mode, "mode", "m", "", "output format (table|wide|json), wide is a verbose table. (default 'table')")
|
2024-12-20 11:53:09 +01:00
|
|
|
cmd.PersistentFlags().StringVarP(&conf.Template, "template", "T", "", "go template for '-m template'")
|
2024-12-18 14:06:21 +01:00
|
|
|
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-20 11:53:09 +01:00
|
|
|
cmd.PersistentFlags().BoolVarP(&conf.NoHumanize, "no-human", "N", false, "do not translate to human readable values")
|
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
|
|
|
}
|
2024-12-20 18:59:59 +01:00
|
|
|
|
|
|
|
|
func Serve(conf *cfg.Config) *cobra.Command {
|
|
|
|
|
var cmd = &cobra.Command{
|
|
|
|
|
Use: "serve [-l host:port]",
|
|
|
|
|
Short: "run REST API listener",
|
|
|
|
|
Long: `run REST API listener`,
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
// errors at this stage do not cause the usage to be shown
|
|
|
|
|
cmd.SilenceUsage = true
|
|
|
|
|
|
2024-12-20 23:04:31 +01:00
|
|
|
return rest.Runserver(conf, nil)
|
2024-12-20 18:59:59 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd.PersistentFlags().StringVarP(&conf.Listen, "listen", "l", "localhost:8787", "host:port")
|
|
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
}
|