Skip to content

Commit

Permalink
work in progress, minor refactor. Added commands:
Browse files Browse the repository at this point in the history
* login
* editmeta ISSUE
* edit ISSUE
* issuetypes [-p PROJECT]
* createmeta [-p PROJECT] [-i ISSUETYPE]
* transitions ISSUE

make --template argumetn work
  • Loading branch information
coryb committed Feb 12, 2015
1 parent 1d96b55 commit acbc24b
Show file tree
Hide file tree
Showing 6 changed files with 504 additions and 109 deletions.
107 changes: 84 additions & 23 deletions jira/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"
"net/url"
"time"
"io"
"bytes"
"runtime"
)

Expand Down Expand Up @@ -87,38 +87,99 @@ func (c *Cli) loadCookies() []*http.Cookie {
return cookies
}

func (c *Cli) post(uri string, content io.Reader) *http.Response {
req, _ := http.NewRequest("POST", uri, content)
return c.makeRequest(req)
func (c *Cli) post(uri string, content string) (*http.Response, error) {
return c.makeRequestWithContent("POST", uri, content)
}

func (c *Cli) get(uri string) *http.Response {
req, _ := http.NewRequest("GET", uri, nil)
return c.makeRequest(req)
func (c *Cli) put(uri string, content string) (*http.Response, error) {
return c.makeRequestWithContent("PUT", uri, content)
}

func (c *Cli) makeRequest(req *http.Request) *http.Response {

req.Header.Set("Content-Type", "application/json")
func (c *Cli) makeRequestWithContent(method string, uri string, content string) (*http.Response, error) {
buffer := bytes.NewBufferString(content)
req, _ := http.NewRequest(method, uri, buffer)

log.Info("%s %s", req.Method, req.URL.String())
if log.IsEnabledFor(logging.DEBUG) {
logBuffer := bytes.NewBuffer(make([]byte,0,len(content)))
req.Write(logBuffer)
log.Debug("%s", logBuffer)
// need to recreate the buffer since the offset is now at the end
// need to be able to rewind the buffer offset, dont know how yet
req, _ = http.NewRequest(method, uri, bytes.NewBufferString(content))
}

resp, err := c.ua.Do(req)

if err != nil {
fmt.Printf("Error: %s", err)
if resp, err := c.makeRequest(req); err != nil {
return nil, err
} else {
if resp.StatusCode == 401 {
if err := c.CmdLogin(); err != nil {
return nil, err
}
req, _ = http.NewRequest(method, uri, bytes.NewBufferString(content))
return c.makeRequest(req)
}
return resp, err
}
}

if resp.StatusCode != 200 {
log.Error("response status: %s", resp.Status)
resp.Write(os.Stderr)
func (c *Cli) get(uri string) (*http.Response, error) {
req, _ := http.NewRequest("GET", uri, nil)
log.Info("%s %s", req.Method, req.URL.String())
if log.IsEnabledFor(logging.DEBUG) {
logBuffer := bytes.NewBuffer(make([]byte,0))
req.Write(logBuffer)
log.Debug("%s", logBuffer)
}

runtime.SetFinalizer(resp, func(r *http.Response) {
r.Body.Close()
})
if resp, err := c.makeRequest(req); err != nil {
return nil, err
} else {
if resp.StatusCode == 401 {
if err := c.CmdLogin(); err != nil {
return nil, err
}
return c.makeRequest(req)
}
return resp, err
}
}

if _, ok := resp.Header["Set-Cookie"]; ok {
c.saveCookies(resp.Cookies())
func (c *Cli) makeRequest(req *http.Request) (resp *http.Response, err error) {
req.Header.Set("Content-Type", "application/json")
if resp, err = c.ua.Do(req); err != nil {
log.Error("Failed to %s %s: %s", req.Method, req.URL.String(), err)
return nil, err
} else {
if resp.StatusCode < 200 || resp.StatusCode >= 300 && resp.StatusCode != 401 {
log.Error("response status: %s", resp.Status)
resp.Write(os.Stderr)
}

runtime.SetFinalizer(resp, func(r *http.Response) {
r.Body.Close()
})

if _, ok := resp.Header["Set-Cookie"]; ok {
c.saveCookies(resp.Cookies())
}
}
return resp, nil
}

return resp
func (c *Cli) getTemplate(path string, dflt string) string {
if override, ok := c.opts["template"]; ok {
if _, err := os.Stat(override); err == nil {
return readFile(override)
} else {
if file, err := FindClosestParentPath(fmt.Sprintf(".jira.d/templates/%s", override)); err == nil {
return readFile(file)
}
}
}
if file, err := FindClosestParentPath(path); err != nil {
return dflt
} else {
return readFile(file)
}
}
Loading

0 comments on commit acbc24b

Please sign in to comment.