add abstract request wrapper to allow you to access/process random apis

supported by Jira but not yet supported by go-jira
This commit is contained in:
Cory Bennett
2015-12-03 11:35:51 -08:00
parent e35e518368
commit f904f3c089
3 changed files with 35 additions and 0 deletions
+22
View File
@@ -585,3 +585,25 @@ func (c *Cli) CmdExportTemplates() error {
}
return nil
}
func (c *Cli) CmdRequest(uri, content string) (err error) {
log.Debug("request called")
if ! strings.HasPrefix(uri, "http") {
uri = fmt.Sprintf("%s%s", c.endpoint, uri)
}
method := strings.ToUpper(c.opts["method"].(string))
var data interface{}
if method == "GET" {
data, err = responseToJson(c.get(uri))
} else if method == "POST" {
data, err = responseToJson(c.post(uri, content))
} else if method == "PUT" {
data, err = responseToJson(c.put(uri, content))
}
if err != nil {
return err
}
return runTemplate(c.getTemplate("request"), data, nil)
}
+1
View File
@@ -16,6 +16,7 @@ var all_templates = map[string]string{
"create": default_create_template,
"comment": default_comment_template,
"transition": default_transition_template,
"request": default_debug_template,
}
const default_debug_template = "{{ . | toJson}}\n"
+12
View File
@@ -78,6 +78,7 @@ Usage:
jira export-templates [-d DIR] [-t template]
jira (b|browse) ISSUE
jira login
jira request [-M METHOD] URI [DATA]
jira ISSUE
General Options:
@@ -149,6 +150,8 @@ Command Options:
"export-templates": "export-templates",
"browse": "browse",
"login": "login",
"req": "request",
"request": "request",
}
defaults := map[string]interface{}{
@@ -157,6 +160,7 @@ Command Options:
"directory": fmt.Sprintf("%s/.jira.d/templates", home),
"sort": defaultSort,
"max_results": defaultMaxResults,
"method": "GET",
}
opts := make(map[string]interface{})
@@ -196,6 +200,7 @@ Command Options:
"edit": setopt,
"m|comment=s": setopt,
"d|dir|directory=s": setopt,
"M|method=s": setopt,
})
if err := op.ProcessAll(os.Args[1:]); err != nil {
@@ -388,6 +393,13 @@ Command Options:
case "view":
requireArgs(1)
err = c.CmdView(args[0])
case "request":
requireArgs(1)
data := ""
if len(args) > 1 {
data = args[1]
}
err = c.CmdRequest(args[0], data)
default:
log.Error("Unknown command %s", command)
os.Exit(1)