This commit is contained in:
Cory Bennett
2015-02-13 15:44:07 -08:00
parent 4c1e0ec93e
commit 97ad931f79
5 changed files with 187 additions and 153 deletions
+20 -14
View File
@@ -1,19 +1,19 @@
package cli
import (
"github.com/op/go-logging"
"net/http"
"net/http/cookiejar"
"bytes"
"encoding/json"
"fmt"
"github.com/op/go-logging"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"os/exec"
"gopkg.in/yaml.v2"
"net/url"
"time"
"bytes"
"runtime"
"time"
)
var log = logging.MustGetLogger("jira.cli")
@@ -189,7 +189,8 @@ func (c *Cli) getTemplate(path string, dflt string) string {
func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData map[string]interface{}, templateProcessor func(string) error) error {
tmpdir := fmt.Sprintf("%s/.jira.d/tmp", os.Getenv("HOME"))
fh, err := ioutil.TempFile(tmpdir, tmpFilePrefix); if err != nil {
fh, err := ioutil.TempFile(tmpdir, tmpFilePrefix)
if err != nil {
log.Error("Failed to make temp file in %s: %s", tmpdir, err)
return err
}
@@ -201,20 +202,24 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m
return err
}
err = runTemplate(template, templateData, fh); if err != nil {
err = runTemplate(template, templateData, fh)
if err != nil {
return err
}
fh.Close()
editor, ok := c.opts["editor"]; if !ok {
editor = os.Getenv("JIRA_EDITOR"); if editor == "" {
editor = os.Getenv("EDITOR"); if editor == "" {
editor, ok := c.opts["editor"]
if !ok {
editor = os.Getenv("JIRA_EDITOR")
if editor == "" {
editor = os.Getenv("EDITOR")
if editor == "" {
editor = "vim"
}
}
}
for ; true ; {
for true {
log.Debug("Running: %s %s", editor, tmpFileName)
cmd := exec.Command(editor, tmpFileName)
cmd.Stdout, cmd.Stderr, cmd.Stdin = os.Stdout, os.Stderr, os.Stdin
@@ -264,7 +269,8 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m
}
}
json, err := jsonEncode(edited); if err != nil {
json, err := jsonEncode(edited)
if err != nil {
return err
}
+61 -37
View File
@@ -1,23 +1,23 @@
package cli
import (
"net/http"
"fmt"
"code.google.com/p/gopass"
"bytes"
"strings"
"code.google.com/p/gopass"
"fmt"
"net/http"
"os"
"strings"
// "github.com/kr/pretty"
)
func (c *Cli) CmdLogin() (error) {
func (c *Cli) CmdLogin() error {
uri := fmt.Sprintf("%s/rest/auth/1/session", c.endpoint)
for ; true ; {
for true {
req, _ := http.NewRequest("GET", uri, nil)
user, _ := c.opts["user"]
prompt := fmt.Sprintf("Enter Password for %s: ", user)
passwd, _ := gopass.GetPass(prompt);
passwd, _ := gopass.GetPass(prompt)
req.SetBasicAuth(user, passwd)
if resp, err := c.makeRequest(req); err != nil {
@@ -47,14 +47,14 @@ func (c *Cli) CmdLogin() (error) {
func (c *Cli) CmdFields() error {
log.Debug("fields called")
uri := fmt.Sprintf("%s/rest/api/2/field", c.endpoint)
data, err := responseToJson(c.get(uri)); if err != nil {
data, err := responseToJson(c.get(uri))
if err != nil {
return err
}
return runTemplate(c.getTemplate(".jira.d/templates/fields", default_fields_template), data, nil)
}
func (c *Cli) CmdList() error {
log.Debug("list called")
@@ -89,12 +89,14 @@ func (c *Cli) CmdList() error {
"jql": query,
"startAt": "0",
"maxResults": "500",
}); if err != nil {
})
if err != nil {
return err
}
uri := fmt.Sprintf("%s/rest/api/2/search", c.endpoint)
data, err := responseToJson(c.post(uri, json)); if err != nil {
data, err := responseToJson(c.post(uri, json))
if err != nil {
return err
}
@@ -104,7 +106,8 @@ func (c *Cli) CmdList() error {
func (c *Cli) CmdView(issue string) error {
log.Debug("view called")
uri := fmt.Sprintf("%s/rest/api/2/issue/%s", c.endpoint, issue)
data, err := responseToJson(c.get(uri)); if err != nil {
data, err := responseToJson(c.get(uri))
if err != nil {
return err
}
@@ -115,7 +118,8 @@ func (c *Cli) CmdEdit(issue string) error {
log.Debug("edit called")
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/editmeta", c.endpoint, issue)
editmeta, err := responseToJson(c.get(uri)); if err != nil {
editmeta, err := responseToJson(c.get(uri))
if err != nil {
return err
}
@@ -135,7 +139,8 @@ func (c *Cli) CmdEdit(issue string) error {
fmt.Sprintf("%s-edit-", issue),
issueData,
func(json string) error {
resp, err := c.put(uri, json); if err != nil {
resp, err := c.put(uri, json)
if err != nil {
return err
}
@@ -156,7 +161,8 @@ func (c *Cli) CmdEdit(issue string) error {
func (c *Cli) CmdEditMeta(issue string) error {
log.Debug("editMeta called")
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/editmeta", c.endpoint, issue)
data, err := responseToJson(c.get(uri)); if err != nil {
data, err := responseToJson(c.get(uri))
if err != nil {
return err
}
@@ -166,7 +172,8 @@ func (c *Cli) CmdEditMeta(issue string) error {
func (c *Cli) CmdTransitionMeta(issue string) error {
log.Debug("tranisionMeta called")
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/transitions?expand=transitions.fields", c.endpoint, issue)
data, err := responseToJson(c.get(uri)); if err != nil {
data, err := responseToJson(c.get(uri))
if err != nil {
return err
}
@@ -176,7 +183,8 @@ func (c *Cli) CmdTransitionMeta(issue string) error {
func (c *Cli) CmdIssueTypes(project string) error {
log.Debug("issueTypes called")
uri := fmt.Sprintf("%s/rest/api/2/issue/createmeta?projectKeys=%s", c.endpoint, project)
data, err := responseToJson(c.get(uri)); if err != nil {
data, err := responseToJson(c.get(uri))
if err != nil {
return err
}
@@ -186,7 +194,8 @@ func (c *Cli) CmdIssueTypes(project string) error {
func (c *Cli) CmdCreateMeta(project string, issuetype string) error {
log.Debug("createMeta called")
uri := fmt.Sprintf("%s/rest/api/2/issue/createmeta?projectKeys=%s&issuetypeNames=%s&expand=projects.issuetypes.fields", c.endpoint, project, issuetype)
data, err := responseToJson(c.get(uri)); if err != nil {
data, err := responseToJson(c.get(uri))
if err != nil {
return err
}
@@ -202,7 +211,8 @@ func (c *Cli) CmdCreateMeta(project string, issuetype string) error {
func (c *Cli) CmdTransitions(issue string) error {
log.Debug("Transitions called")
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/transitions", c.endpoint, issue)
data, err := responseToJson(c.get(uri)); if err != nil {
data, err := responseToJson(c.get(uri))
if err != nil {
return err
}
return runTemplate(c.getTemplate(".jira.d/templates/transitions", default_transitions_template), data, nil)
@@ -212,7 +222,8 @@ func (c *Cli) CmdCreate(project string, issuetype string) error {
log.Debug("create called")
uri := fmt.Sprintf("%s/rest/api/2/issue/createmeta?projectKeys=%s&issuetypeNames=%s&expand=projects.issuetypes.fields", c.endpoint, project, issuetype)
data, err := responseToJson(c.get(uri)); if err != nil {
data, err := responseToJson(c.get(uri))
if err != nil {
return err
}
@@ -233,7 +244,8 @@ func (c *Cli) CmdCreate(project string, issuetype string) error {
func(json string) error {
log.Debug("JSON: %s", json)
uri := fmt.Sprintf("%s/rest/api/2/issue", c.endpoint)
resp, err := c.post(uri, json); if err != nil {
resp, err := c.post(uri, json)
if err != nil {
return err
}
@@ -261,7 +273,8 @@ func (c *Cli) CmdCreate(project string, issuetype string) error {
func (c *Cli) CmdIssueLinkTypes() error {
log.Debug("Transitions called")
uri := fmt.Sprintf("%s/rest/api/2/issueLinkType", c.endpoint)
data, err := responseToJson(c.get(uri)); if err != nil {
data, err := responseToJson(c.get(uri))
if err != nil {
return err
}
return runTemplate(c.getTemplate(".jira.d/templates/issuelinktypes", default_fields_template), data, nil)
@@ -280,12 +293,14 @@ func (c *Cli) CmdBlocks(blocker string, issue string) error {
"outwardIssue": map[string]string{
"key": blocker,
},
}); if err != nil {
})
if err != nil {
return err
}
uri := fmt.Sprintf("%s/rest/api/2/issueLink", c.endpoint)
resp, err := c.post(uri, json); if err != nil {
resp, err := c.post(uri, json)
if err != nil {
return err
}
if resp.StatusCode == 201 {
@@ -313,12 +328,14 @@ func (c *Cli) CmdDups(duplicate string, issue string) error {
"outwardIssue": map[string]string{
"key": issue,
},
}); if err != nil {
})
if err != nil {
return err
}
uri := fmt.Sprintf("%s/rest/api/2/issueLink", c.endpoint)
resp, err := c.post(uri, json); if err != nil {
resp, err := c.post(uri, json)
if err != nil {
return err
}
if resp.StatusCode == 201 {
@@ -333,16 +350,17 @@ func (c *Cli) CmdDups(duplicate string, issue string) error {
return nil
}
func (c *Cli) CmdWatch(issue string, watcher string) error {
log.Debug("watch called")
json, err := jsonEncode(watcher); if err != nil {
json, err := jsonEncode(watcher)
if err != nil {
return err
}
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/watchers", c.endpoint, issue)
resp, err := c.post(uri, json); if err != nil {
resp, err := c.post(uri, json)
if err != nil {
return err
}
if resp.StatusCode == 204 {
@@ -360,7 +378,8 @@ func (c *Cli) CmdWatch(issue string, watcher string) error {
func (c *Cli) CmdTransition(issue string, trans string) error {
log.Debug("transition called")
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/transitions", c.endpoint, issue)
data, err := responseToJson(c.get(uri)); if err != nil {
data, err := responseToJson(c.get(uri))
if err != nil {
return err
}
@@ -381,7 +400,6 @@ func (c *Cli) CmdTransition(issue string, trans string) error {
return err
}
payload := map[string]interface{}{
"transition": map[string]interface{}{
"id": transId,
@@ -400,12 +418,14 @@ func (c *Cli) CmdTransition(issue string, trans string) error {
}
}
json, err := jsonEncode(payload); if err != nil {
json, err := jsonEncode(payload)
if err != nil {
return err
}
uri = fmt.Sprintf("%s/rest/api/2/issue/%s/transitions", c.endpoint, issue)
resp, err := c.post(uri, json); if err != nil {
resp, err := c.post(uri, json)
if err != nil {
return err
}
if resp.StatusCode == 204 {
@@ -426,7 +446,8 @@ func (c *Cli) CmdComment(issue string) error {
handlePost := func(json string) error {
log.Debug("JSON: %s", json)
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/comment", c.endpoint, issue)
resp, err := c.post(uri, json); if err != nil {
resp, err := c.post(uri, json)
if err != nil {
return err
}
@@ -445,7 +466,8 @@ func (c *Cli) CmdComment(issue string) error {
if comment, ok := c.opts["comment"]; ok {
json, err := jsonEncode(map[string]interface{}{
"body": comment,
}); if err != nil {
})
if err != nil {
return err
}
return handlePost(json)
@@ -465,12 +487,14 @@ func (c *Cli) CmdAssign(issue string, user string) error {
json, err := jsonEncode(map[string]interface{}{
"name": user,
}); if err != nil {
})
if err != nil {
return err
}
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/assignee", c.endpoint, issue)
resp, err := c.put(uri, json); if err != nil {
resp, err := c.put(uri, json)
if err != nil {
return err
}
if resp.StatusCode == 204 {
+20 -16
View File
@@ -1,18 +1,18 @@
package cli
import (
"os"
"fmt"
"errors"
"strings"
"net/http"
"encoding/json"
"io/ioutil"
"text/template"
"io"
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/mgutz/ansi"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"text/template"
)
func FindParentPaths(fileName string) []string {
@@ -78,9 +78,12 @@ func runTemplate(templateContent string, data interface{}, out io.Writer) error
},
"append": func(more string, content interface{}) (string, error) {
switch value := content.(type) {
case string: return string(append([]byte(content.(string)), []byte(more)...)), nil
case []byte: return string(append(content.([]byte), []byte(more)...)), nil
default: return "", errors.New(fmt.Sprintf("Unknown type: %s", value))
case string:
return string(append([]byte(content.(string)), []byte(more)...)), nil
case []byte:
return string(append(content.([]byte), []byte(more)...)), nil
default:
return "", errors.New(fmt.Sprintf("Unknown type: %s", value))
}
},
"indent": func(spaces int, content string) string {
@@ -132,7 +135,8 @@ func jsonEncode(data interface{}) (string, error) {
buffer := bytes.NewBuffer(make([]byte, 0))
enc := json.NewEncoder(buffer)
err := enc.Encode(data); if err != nil {
err := enc.Encode(data)
if err != nil {
log.Error("Failed to encode data %s: %s", data, err)
return "", err
}
@@ -164,7 +168,7 @@ func promptYN(prompt string, yes bool) bool {
if ans == "" {
return yes
}
if( strings.HasPrefix(ans, "y") ) {
if strings.HasPrefix(ans, "y") {
return true
}
return false
@@ -208,7 +212,7 @@ func yamlFixup( data interface{} ) (interface{}, error) {
}
}
return data, nil
default: return d, nil
default:
return d, nil
}
}
+8 -8
View File
@@ -1,14 +1,14 @@
package main
import (
"fmt"
"github.com/Netflix-Skunkworks/go-jira/jira/cli"
"github.com/docopt/docopt-go"
"github.com/op/go-logging"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"strings"
"io/ioutil"
"gopkg.in/yaml.v2"
"github.com/Netflix-Skunkworks/go-jira/jira/cli"
)
var log = logging.MustGetLogger("jira")
@@ -67,7 +67,8 @@ Command Options:
-w --watcher=USER Watcher to add to issue (default: %s)
`, user, fmt.Sprintf("%s/.jira.d/templates", home), user)
args, err := docopt.Parse(usage, nil, true, "0.0.1", false, false); if err != nil {
args, err := docopt.Parse(usage, nil, true, "0.0.1", false, false)
if err != nil {
log.Error("Failed to parse options: %s", err)
os.Exit(1)
}
@@ -136,7 +137,7 @@ Command Options:
c := cli.New(opts)
log.Debug("opts: %s", opts);
log.Debug("opts: %s", opts)
validCommand := func(cmd string) bool {
if val, ok := args[cmd]; ok && val.(bool) {
@@ -249,8 +250,7 @@ func loadConfigs(opts map[string]string) {
// prepend
paths = append([]string{"/etc/jira-cli.yml"}, paths...)
for _, file := range(paths) {
for _, file := range paths {
parseYaml(file, opts)
}
}