Skip to content

Commit

Permalink
feat(document): Add attribute selection flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarlier committed Aug 20, 2016
1 parent 8dbfc2d commit 96853e2
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions cmd/get_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package cmd

import (
"errors"
"fmt"
"io"
"reflect"
"text/template"

"github.com/spf13/cobra"

"github.com/ncarlier/keeper-cli/api"
cmdutil "github.com/ncarlier/keeper-cli/cmd/util"
)

var DocumentTmpl = `Document:
ID: {{.Id}}
Id: {{.Id}}
Title: {{.Title}}
ContentType: {{.ContentType}}
Content: {{.Content}}
Expand All @@ -20,7 +23,18 @@ var DocumentTmpl = `Document:
Ghost: {{.Ghost}}
`

type getOptions struct {
attribute string
}

func getField(d *api.DocumentResponse, field string) string {
r := reflect.ValueOf(d)
f := reflect.Indirect(r).FieldByName(field)
return f.String()
}

func NewCmdGetDocument(f *cmdutil.Factory, out io.Writer) *cobra.Command {
var opts getOptions
cmd := &cobra.Command{
Use: "get (ID)",
Short: "Get a document",
Expand All @@ -30,14 +44,18 @@ func NewCmdGetDocument(f *cmdutil.Factory, out io.Writer) *cobra.Command {
}
docid := args[0]

return runGetDocument(f, out, cmd, docid)
return runGetDocument(f, out, cmd, docid, &opts)
},
ValidArgs: []string{"create", "rm", "restore", "destroy"},
}

flags := cmd.Flags()
flags.StringVarP(&opts.attribute, "attr", "a", "", "Attribute selection")

return cmd
}

func runGetDocument(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, docid string) error {
func runGetDocument(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, docid string, opts *getOptions) error {
c, err := f.Client()
if err != nil {
return err
Expand All @@ -48,6 +66,11 @@ func runGetDocument(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, docid
return err
}

if opts.attribute != "" {
fmt.Fprintln(out, getField(document, opts.attribute))
return nil
}

tmpl, err := template.New("document").Parse(DocumentTmpl)
if err != nil {
return err
Expand Down

0 comments on commit 96853e2

Please sign in to comment.