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()
|
defer db.Close()
|
||||||
|
|
||||||
if err := attr.ParseKV(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
entry := DbEntry{}
|
entry := DbEntry{}
|
||||||
|
|
||||||
err := db.DB.View(func(tx *bolt.Tx) error {
|
err := db.DB.View(func(tx *bolt.Tx) error {
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func Get(conf *cfg.Config) *cobra.Command {
|
|||||||
)
|
)
|
||||||
|
|
||||||
var cmd = &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",
|
Short: "Retrieve value for a key",
|
||||||
Long: `Retrieve value for a key`,
|
Long: `Retrieve value for a key`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
@@ -80,11 +80,13 @@ func Get(conf *cfg.Config) *cobra.Command {
|
|||||||
return err
|
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, "show")
|
||||||
cmd.Aliases = append(cmd.Aliases, "g")
|
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(&wide, "wide-output", "l", false, "output mode: wide")
|
||||||
cmd.PersistentFlags().BoolVarP(&conf.NoHeaders, "no-headers", "n", false, "omit headers in tables")
|
cmd.PersistentFlags().BoolVarP(&conf.NoHeaders, "no-headers", "n", false, "omit headers in tables")
|
||||||
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
|
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package output
|
package output
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@@ -11,10 +12,9 @@ import (
|
|||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Print(writer io.Writer, conf *cfg.Config, entry *app.DbEntry) error {
|
func Print(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEntry) error {
|
||||||
if conf.Mode != "" {
|
if attr.File != "" {
|
||||||
// consider this to be a file
|
fd, err := os.OpenFile(attr.File, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
|
||||||
fd, err := os.OpenFile(conf.Mode, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -41,19 +41,34 @@ func Print(writer io.Writer, conf *cfg.Config, entry *app.DbEntry) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isatty := term.IsTerminal(int(os.Stdout.Fd()))
|
isatty := term.IsTerminal(int(os.Stdout.Fd()))
|
||||||
if len(entry.Bin) > 0 {
|
|
||||||
if isatty {
|
|
||||||
fmt.Println("binary data omitted")
|
|
||||||
} else {
|
|
||||||
os.Stdout.Write(entry.Bin)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fmt.Print(entry.Value)
|
|
||||||
|
|
||||||
if !strings.HasSuffix(entry.Value, "\n") {
|
switch conf.Mode {
|
||||||
// always add a terminal newline
|
case "simple":
|
||||||
fmt.Println()
|
fallthrough
|
||||||
|
case "":
|
||||||
|
if len(entry.Bin) > 0 {
|
||||||
|
if isatty {
|
||||||
|
fmt.Println("binary data omitted")
|
||||||
|
} else {
|
||||||
|
os.Stdout.Write(entry.Bin)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Print(entry.Value)
|
||||||
|
|
||||||
|
if !strings.HasSuffix(entry.Value, "\n") {
|
||||||
|
// always add a terminal newline
|
||||||
|
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
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user