diff --git a/app/db.go b/app/db.go index 450a3d1..25d2baa 100644 --- a/app/db.go +++ b/app/db.go @@ -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 { diff --git a/cmd/maincommands.go b/cmd/maincommands.go index 20a7e52..b8f5055 100644 --- a/cmd/maincommands.go +++ b/cmd/maincommands.go @@ -60,7 +60,7 @@ func Get(conf *cfg.Config) *cobra.Command { ) var cmd = &cobra.Command{ - Use: "get [-o ]", + Use: "get [-o ] [-m ] [-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") diff --git a/output/single.go b/output/single.go index a4fb263..5431bb8 100644 --- a/output/single.go +++ b/output/single.go @@ -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,19 +41,34 @@ func Print(writer io.Writer, conf *cfg.Config, entry *app.DbEntry) error { } 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") { - // always add a terminal newline - fmt.Println() + switch conf.Mode { + case "simple": + 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