mirror of
https://codeberg.org/scip/anydb.git
synced 2025-12-17 20:41:00 +01:00
lots of changes:
- added man command - added unit tests - fixed import+export file parameters (now -o and -r respectively) - added README + License - added ci pipelines
This commit is contained in:
62
cmd/anydb.go
62
cmd/anydb.go
@@ -2,8 +2,68 @@ package cmd
|
||||
|
||||
var manpage = `
|
||||
anydb
|
||||
FIXME
|
||||
anydb - a personal key value store
|
||||
|
||||
SYNOPSIS
|
||||
Usage:
|
||||
anydb <command> [options] [flags]
|
||||
anydb [command]
|
||||
|
||||
Available Commands:
|
||||
completion Generate the autocompletion script for the specified shell
|
||||
del Delete key
|
||||
export Export database to json
|
||||
get Retrieve value for a key
|
||||
help Help about any command
|
||||
import Import database dump
|
||||
list List database contents
|
||||
set Insert key/value pair
|
||||
|
||||
Flags:
|
||||
-f, --dbfile string DB file to use (default "/home/scip/.config/anydb/default.db")
|
||||
-d, --debug Enable debugging
|
||||
-h, --help help for anydb
|
||||
-v, --version Print program version
|
||||
|
||||
Use "anydb [command] --help" for more information about a command.
|
||||
|
||||
DESCRIPTION
|
||||
Anydb is a simple to use commandline tool to store anything you'd like,
|
||||
even binary files etc. It uses a key/value store (bbolt) in your home
|
||||
directory.
|
||||
|
||||
LICENSE
|
||||
This software is licensed under the GNU GENERAL PUBLIC LICENSE version
|
||||
3.
|
||||
|
||||
Copyright (c) 2024 by Thomas von Dein
|
||||
|
||||
AUTHORS
|
||||
Thomas von Dein tom AT vondein DOT org
|
||||
|
||||
`
|
||||
var usage = `
|
||||
|
||||
Usage:
|
||||
anydb <command> [options] [flags]
|
||||
anydb [command]
|
||||
|
||||
Available Commands:
|
||||
completion Generate the autocompletion script for the specified shell
|
||||
del Delete key
|
||||
export Export database to json
|
||||
get Retrieve value for a key
|
||||
help Help about any command
|
||||
import Import database dump
|
||||
list List database contents
|
||||
set Insert key/value pair
|
||||
|
||||
Flags:
|
||||
-f, --dbfile string DB file to use (default "/home/scip/.config/anydb/default.db")
|
||||
-d, --debug Enable debugging
|
||||
-h, --help help for anydb
|
||||
-v, --version Print program version
|
||||
|
||||
Use "anydb [command] --help" for more information about a command.
|
||||
|
||||
`
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -115,8 +118,6 @@ func Del(conf *cfg.Config) *cobra.Command {
|
||||
},
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().StringVarP(&conf.Mode, "output", "o", "", "output to file")
|
||||
|
||||
cmd.Aliases = append(cmd.Aliases, "d")
|
||||
cmd.Aliases = append(cmd.Aliases, "rm")
|
||||
|
||||
@@ -129,19 +130,13 @@ func Export(conf *cfg.Config) *cobra.Command {
|
||||
)
|
||||
|
||||
var cmd = &cobra.Command{
|
||||
Use: "export [<json filename>]",
|
||||
Use: "export [-o <json filename>]",
|
||||
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
|
||||
|
||||
if len(args) == 0 {
|
||||
attr.File = "-"
|
||||
} else {
|
||||
attr.File = args[0]
|
||||
}
|
||||
|
||||
conf.Mode = "json"
|
||||
|
||||
entries, err := conf.DB.List(&attr)
|
||||
@@ -153,6 +148,8 @@ func Export(conf *cfg.Config) *cobra.Command {
|
||||
},
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().StringVarP(&attr.File, "output", "o", "", "output to file")
|
||||
|
||||
cmd.Aliases = append(cmd.Aliases, "dump")
|
||||
cmd.Aliases = append(cmd.Aliases, "backup")
|
||||
|
||||
@@ -219,12 +216,6 @@ func Import(conf *cfg.Config) *cobra.Command {
|
||||
// errors at this stage do not cause the usage to be shown
|
||||
cmd.SilenceUsage = true
|
||||
|
||||
if len(args) == 0 {
|
||||
attr.File = "-"
|
||||
} else {
|
||||
attr.File = args[0]
|
||||
}
|
||||
|
||||
return conf.DB.Import(&attr)
|
||||
},
|
||||
}
|
||||
@@ -244,5 +235,33 @@ func Help(conf *cfg.Config) *cobra.Command {
|
||||
}
|
||||
|
||||
func Man(conf *cfg.Config) *cobra.Command {
|
||||
return nil
|
||||
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
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func Execute() {
|
||||
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if ShowVersion {
|
||||
fmt.Println(cfg.Version)
|
||||
fmt.Printf("This is anydb version %s\n", cfg.Version)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -83,8 +83,7 @@ func Execute() {
|
||||
rootCmd.AddCommand(Del(&conf))
|
||||
rootCmd.AddCommand(Export(&conf))
|
||||
rootCmd.AddCommand(Import(&conf))
|
||||
// rootCmd.AddCommand(Help(&conf))
|
||||
// rootCmd.AddCommand(Man(&conf))
|
||||
rootCmd.AddCommand(Man(&conf))
|
||||
|
||||
err = rootCmd.Execute()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user