Skip to content

Commit

Permalink
add --quiet command to not print the OK ..
Browse files Browse the repository at this point in the history
add --saveFile option to print the issue/link to a file on create command
  • Loading branch information
coryb committed Dec 3, 2015
1 parent eaddfe6 commit c9ac162
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
7 changes: 7 additions & 0 deletions jira/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 31 additions & 11 deletions jira/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions jira/cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"gopkg.in/coryb/yaml.v2"
"github.com/mgutz/ansi"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions jira/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c9ac162

Please sign in to comment.