mirror of
https://codeberg.org/scip/anydb.git
synced 2025-12-17 04:20:59 +01:00
added json support to get command, renamed -o <mode> to -m <mode>
This commit is contained in:
@@ -206,10 +206,6 @@ func (db *DB) Get(attr *DbAttr) (*DbEntry, error) {
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if err := attr.ParseKV(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entry := DbEntry{}
|
||||
|
||||
err := db.DB.View(func(tx *bolt.Tx) error {
|
||||
|
||||
@@ -60,7 +60,7 @@ func Get(conf *cfg.Config) *cobra.Command {
|
||||
)
|
||||
|
||||
var cmd = &cobra.Command{
|
||||
Use: "get <key> [-o <file>]",
|
||||
Use: "get <key> [-o <file>] [-m <mode>] [-n]",
|
||||
Short: "Retrieve value for a key",
|
||||
Long: `Retrieve value for a key`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
@@ -80,11 +80,13 @@ func Get(conf *cfg.Config) *cobra.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
return output.Print(os.Stdout, conf, entry)
|
||||
return output.Print(os.Stdout, conf, &attr, entry)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().StringVarP(&conf.Mode, "output", "o", "", "output to file")
|
||||
cmd.PersistentFlags().StringVarP(&attr.File, "output", "o", "", "output to file (ignores -m)")
|
||||
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")
|
||||
|
||||
cmd.Aliases = append(cmd.Aliases, "show")
|
||||
cmd.Aliases = append(cmd.Aliases, "g")
|
||||
@@ -192,7 +194,7 @@ func List(conf *cfg.Config) *cobra.Command {
|
||||
},
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().StringVarP(&conf.Mode, "output-mode", "o", "", "output format (table|wide|json), wide is a verbose table. (default 'table')")
|
||||
cmd.PersistentFlags().StringVarP(&conf.Mode, "mode", "m", "", "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")
|
||||
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -11,10 +12,9 @@ import (
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func Print(writer io.Writer, conf *cfg.Config, entry *app.DbEntry) error {
|
||||
if conf.Mode != "" {
|
||||
// consider this to be a file
|
||||
fd, err := os.OpenFile(conf.Mode, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
|
||||
func Print(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEntry) error {
|
||||
if attr.File != "" {
|
||||
fd, err := os.OpenFile(attr.File, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -41,6 +41,11 @@ func Print(writer io.Writer, conf *cfg.Config, entry *app.DbEntry) error {
|
||||
}
|
||||
|
||||
isatty := term.IsTerminal(int(os.Stdout.Fd()))
|
||||
|
||||
switch conf.Mode {
|
||||
case "simple":
|
||||
fallthrough
|
||||
case "":
|
||||
if len(entry.Bin) > 0 {
|
||||
if isatty {
|
||||
fmt.Println("binary data omitted")
|
||||
@@ -55,6 +60,16 @@ func Print(writer io.Writer, conf *cfg.Config, entry *app.DbEntry) error {
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
case "json":
|
||||
jsonentry, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("json marshalling failure: %s", err)
|
||||
}
|
||||
|
||||
fmt.Println(string(jsonentry))
|
||||
case "wide":
|
||||
ListTable(writer, conf, app.DbEntries{*entry})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user