-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(label): Full feature label commands.
- Loading branch information
Showing
9 changed files
with
360 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"text/template" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/ncarlier/keeper-cli/api" | ||
cmdutil "github.com/ncarlier/keeper-cli/cmd/util" | ||
) | ||
|
||
type createLabelOptions struct { | ||
label string | ||
color string | ||
} | ||
|
||
func NewCmdCreateLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command { | ||
var opts createLabelOptions | ||
cmd := &cobra.Command{ | ||
Use: "label", | ||
Short: "Create a label", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runCreateLabel(f, out, cmd, &opts) | ||
}, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.StringVarP(&opts.label, "label", "l", "", "Label name") | ||
flags.StringVarP(&opts.color, "color", "c", "", "Label color") | ||
return cmd | ||
} | ||
|
||
func runCreateLabel(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, opts *createLabelOptions) error { | ||
if opts.label == "" || opts.color == "" { | ||
return errors.New("You have to specify at least a label and a color.") | ||
} | ||
|
||
c, err := f.Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
lbl := &api.LabelResponse{ | ||
Label: opts.label, | ||
Color: opts.color, | ||
} | ||
label, err := c.CreateLabel(lbl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tmpl, err := template.New("label").Parse(LabelTmpl) | ||
if err != nil { | ||
return err | ||
} | ||
err = tmpl.Execute(out, label) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"text/template" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
cmdutil "github.com/ncarlier/keeper-cli/cmd/util" | ||
) | ||
|
||
var LabelTmpl = `Label: | ||
Id: {{.Id}} | ||
Label: {{.Label}} | ||
Color: {{.Color}} | ||
Date: {{.Date}} | ||
Ghost: {{.Ghost}} | ||
` | ||
|
||
func NewCmdGetLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "label (ID)", | ||
Short: "Get a label", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("Label ID required.") | ||
} | ||
docid := args[0] | ||
|
||
return runGetLabel(f, out, cmd, docid) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runGetLabel(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, id string) error { | ||
c, err := f.Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
label, err := c.GetLabel(id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tmpl, err := template.New("label").Parse(LabelTmpl) | ||
if err != nil { | ||
return err | ||
} | ||
err = tmpl.Execute(out, label) | ||
return err | ||
} |
Oops, something went wrong.