From c9ac162990fa02a13d6c07efd58c3af4606240c0 Mon Sep 17 00:00:00 2001 From: Cory Bennett Date: Thu, 3 Dec 2015 12:55:14 -0800 Subject: [PATCH] add --quiet command to not print the OK .. add --saveFile option to print the issue/link to a file on create command --- jira/cli/cli.go | 7 +++++++ jira/cli/commands.go | 42 +++++++++++++++++++++++++++++++----------- jira/cli/util.go | 16 ++++++++++++++++ jira/main.go | 2 ++ 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/jira/cli/cli.go b/jira/cli/cli.go index 564950e4..c89dff15 100644 --- a/jira/cli/cli.go +++ b/jira/cli/cli.go @@ -354,6 +354,13 @@ func (c *Cli) Browse(issue string) error { return nil } +func (c *Cli) SaveData(data interface{}) error { + if val, ok := c.opts["saveFile"].(string); ok && val != "" { + yamlWrite(val, data) + } + return nil +} + func (c *Cli) FindIssues() (interface{}, error) { var query string var ok bool diff --git a/jira/cli/commands.go b/jira/cli/commands.go index d4c90cc8..3dc8db8c 100644 --- a/jira/cli/commands.go +++ b/jira/cli/commands.go @@ -126,7 +126,9 @@ func (c *Cli) CmdEdit(issue string) error { if resp.StatusCode == 204 { c.Browse(issueData["key"].(string)) - fmt.Printf("OK %s %s/browse/%s\n", issueData["key"], c.endpoint, issueData["key"]) + if ! c.opts["quiet"].(bool) { + fmt.Printf("OK %s %s/browse/%s\n", issueData["key"], c.endpoint, issueData["key"]) + } return nil } else { logBuffer := bytes.NewBuffer(make([]byte, 0)) @@ -260,10 +262,16 @@ func (c *Cli) CmdCreate() error { if json, err := responseToJson(resp, nil); err != nil { return err } else { - key := json.(map[string]interface{})["key"] - c.Browse(key.(string)) - fmt.Printf("OK %s %s/browse/%s\n", key, c.endpoint, key) - + key := json.(map[string]interface{})["key"].(string) + link := fmt.Sprintf("%s/browse/%s", c.endpoint, key) + c.Browse(key) + c.SaveData(map[string]string{ + "issue": key, + "link": link, + }) + if ! c.opts["quiet"].(bool) { + fmt.Printf("OK %s %s\n", key, link) + } } return nil } else { @@ -318,7 +326,9 @@ func (c *Cli) CmdBlocks(blocker string, issue string) error { } if resp.StatusCode == 201 { c.Browse(issue) - fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + if ! c.opts["quiet"].(bool) { + fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + } } else { logBuffer := bytes.NewBuffer(make([]byte, 0)) resp.Write(logBuffer) @@ -359,7 +369,9 @@ func (c *Cli) CmdDups(duplicate string, issue string) error { } if resp.StatusCode == 201 { c.Browse(issue) - fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + if ! c.opts["quiet"].(bool) { + fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + } } else { logBuffer := bytes.NewBuffer(make([]byte, 0)) resp.Write(logBuffer) @@ -391,7 +403,9 @@ func (c *Cli) CmdWatch(issue string) error { } if resp.StatusCode == 204 { c.Browse(issue) - fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + if ! c.opts["quiet"].(bool) { + fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + } } else { logBuffer := bytes.NewBuffer(make([]byte, 0)) resp.Write(logBuffer) @@ -445,7 +459,9 @@ func (c *Cli) CmdTransition(issue string, trans string) error { } if resp.StatusCode == 204 { c.Browse(issue) - fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + if ! c.opts["quiet"].(bool) { + fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + } } else { logBuffer := bytes.NewBuffer(make([]byte, 0)) resp.Write(logBuffer) @@ -496,7 +512,9 @@ func (c *Cli) CmdComment(issue string) error { if resp.StatusCode == 201 { c.Browse(issue) - fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + if ! c.opts["quiet"].(bool) { + fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + } return nil } else { logBuffer := bytes.NewBuffer(make([]byte, 0)) @@ -548,7 +566,9 @@ func (c *Cli) CmdAssign(issue string, user string) error { } if resp.StatusCode == 204 { c.Browse(issue) - fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + if ! c.opts["quiet"].(bool) { + fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + } } else { logBuffer := bytes.NewBuffer(make([]byte, 0)) resp.Write(logBuffer) diff --git a/jira/cli/util.go b/jira/cli/util.go index 01d9c795..1f15a679 100644 --- a/jira/cli/util.go +++ b/jira/cli/util.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "gopkg.in/coryb/yaml.v2" "github.com/mgutz/ansi" "io" "io/ioutil" @@ -245,6 +246,21 @@ func jsonWrite(file string, data interface{}) { enc.Encode(data) } +func yamlWrite(file string, data interface{}) { + fh, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + defer fh.Close() + if err != nil { + log.Error("Failed to open %s: %s", file, err) + os.Exit(1) + } + if out, err := yaml.Marshal(data); err != nil { + log.Error("Failed to marshal yaml %v: %s", data, err) + os.Exit(1) + } else { + fh.Write(out) + } +} + func promptYN(prompt string, yes bool) bool { reader := bufio.NewReader(os.Stdin) if !yes { diff --git a/jira/main.go b/jira/main.go index baa998e7..9477394b 100644 --- a/jira/main.go +++ b/jira/main.go @@ -199,6 +199,8 @@ Command Options: "m|comment=s": setopt, "d|dir|directory=s": setopt, "M|method=s": setopt, + "S|saveFile=s": setopt, + "Q|quiet": setopt, }) if err := op.ProcessAll(os.Args[1:]); err != nil {