Skip to content

Commit 5a4e17c

Browse files
committed
update for golint
1 parent 355fb42 commit 5a4e17c

16 files changed

+1072
-271
lines changed

cli.go

Lines changed: 83 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ import (
2323
)
2424

2525
var (
26-
log = logging.MustGetLogger("jira")
26+
log = logging.MustGetLogger("jira")
27+
// VERSION is the go-jira library version
2728
VERSION string
2829
)
2930

31+
// Cli is go-jira client object
3032
type Cli struct {
3133
endpoint *url.URL
3234
opts map[string]interface{}
3335
cookieFile string
3436
ua *http.Client
3537
}
3638

39+
// New creates go-jira client object
3740
func New(opts map[string]interface{}) *Cli {
3841
homedir := homedir()
3942
cookieJar, _ := cookiejar.New(nil)
@@ -120,7 +123,7 @@ func (c *Cli) loadCookies() []*http.Cookie {
120123
log.Errorf("Failed to open %s: %s", c.cookieFile, err)
121124
panic(err)
122125
}
123-
cookies := make([]*http.Cookie, 0)
126+
cookies := []*http.Cookie{}
124127
err = json.Unmarshal(bytes, &cookies)
125128
if err != nil {
126129
log.Errorf("Failed to parse json from file %s: %s", c.cookieFile, err)
@@ -140,44 +143,42 @@ func (c *Cli) put(uri string, content string) (*http.Response, error) {
140143
return c.makeRequestWithContent("PUT", uri, content)
141144
}
142145

143-
func (c *Cli) delete(uri string) (*http.Response, error) {
146+
func (c *Cli) delete(uri string) (resp *http.Response, err error) {
144147
method := "DELETE"
145148
req, _ := http.NewRequest(method, uri, nil)
146149
log.Infof("%s %s", req.Method, req.URL.String())
147-
if resp, err := c.makeRequest(req); err != nil {
150+
if resp, err = c.makeRequest(req); err != nil {
148151
return nil, err
149-
} else {
150-
if resp.StatusCode == 401 {
151-
if err := c.CmdLogin(); err != nil {
152-
return nil, err
153-
}
154-
req, _ = http.NewRequest(method, uri, nil)
155-
return c.makeRequest(req)
152+
}
153+
if resp.StatusCode == 401 {
154+
if err = c.CmdLogin(); err != nil {
155+
return nil, err
156156
}
157-
return resp, err
157+
req, _ = http.NewRequest(method, uri, nil)
158+
return c.makeRequest(req)
158159
}
160+
return resp, err
159161
}
160162

161-
func (c *Cli) makeRequestWithContent(method string, uri string, content string) (*http.Response, error) {
163+
func (c *Cli) makeRequestWithContent(method string, uri string, content string) (resp *http.Response, err error) {
162164
buffer := bytes.NewBufferString(content)
163165
req, _ := http.NewRequest(method, uri, buffer)
164166

165167
log.Infof("%s %s", req.Method, req.URL.String())
166-
if resp, err := c.makeRequest(req); err != nil {
168+
if resp, err = c.makeRequest(req); err != nil {
167169
return nil, err
168-
} else {
169-
if resp.StatusCode == 401 {
170-
if err := c.CmdLogin(); err != nil {
171-
return nil, err
172-
}
173-
req, _ = http.NewRequest(method, uri, bytes.NewBufferString(content))
174-
return c.makeRequest(req)
170+
}
171+
if resp.StatusCode == 401 {
172+
if err = c.CmdLogin(); err != nil {
173+
return nil, err
175174
}
176-
return resp, err
175+
req, _ = http.NewRequest(method, uri, bytes.NewBufferString(content))
176+
return c.makeRequest(req)
177177
}
178+
return resp, err
178179
}
179180

180-
func (c *Cli) get(uri string) (*http.Response, error) {
181+
func (c *Cli) get(uri string) (resp *http.Response, err error) {
181182
req, _ := http.NewRequest("GET", uri, nil)
182183
log.Infof("%s %s", req.Method, req.URL.String())
183184
if log.IsEnabledFor(logging.DEBUG) {
@@ -186,17 +187,16 @@ func (c *Cli) get(uri string) (*http.Response, error) {
186187
log.Debugf("%s", logBuffer)
187188
}
188189

189-
if resp, err := c.makeRequest(req); err != nil {
190+
if resp, err = c.makeRequest(req); err != nil {
190191
return nil, err
191-
} else {
192-
if resp.StatusCode == 401 {
193-
if err := c.CmdLogin(); err != nil {
194-
return nil, err
195-
}
196-
return c.makeRequest(req)
192+
}
193+
if resp.StatusCode == 401 {
194+
if err := c.CmdLogin(); err != nil {
195+
return nil, err
197196
}
198-
return resp, err
197+
return c.makeRequest(req)
199198
}
199+
return resp, err
200200
}
201201

202202
func (c *Cli) makeRequest(req *http.Request) (resp *http.Response, err error) {
@@ -217,18 +217,17 @@ func (c *Cli) makeRequest(req *http.Request) (resp *http.Response, err error) {
217217
if resp, err = c.ua.Do(req); err != nil {
218218
log.Errorf("Failed to %s %s: %s", req.Method, req.URL.String(), err)
219219
return nil, err
220-
} else {
221-
if resp.StatusCode < 200 || resp.StatusCode >= 300 && resp.StatusCode != 401 {
222-
log.Errorf("response status: %s", resp.Status)
223-
}
220+
}
221+
if resp.StatusCode < 200 || resp.StatusCode >= 300 && resp.StatusCode != 401 {
222+
log.Errorf("response status: %s", resp.Status)
223+
}
224224

225-
runtime.SetFinalizer(resp, func(r *http.Response) {
226-
r.Body.Close()
227-
})
225+
runtime.SetFinalizer(resp, func(r *http.Response) {
226+
r.Body.Close()
227+
})
228228

229-
if _, ok := resp.Header["Set-Cookie"]; ok {
230-
c.saveCookies(resp)
231-
}
229+
if _, ok := resp.Header["Set-Cookie"]; ok {
230+
c.saveCookies(resp)
232231
}
233232
if log.IsEnabledFor(logging.DEBUG) {
234233
out, _ := httputil.DumpResponse(resp, true)
@@ -237,6 +236,7 @@ func (c *Cli) makeRequest(req *http.Request) (resp *http.Response, err error) {
237236
return resp, nil
238237
}
239238

239+
// GetTemplate will return the text/template for the given command name
240240
func (c *Cli) GetTemplate(name string) string {
241241
return c.getTemplate(name)
242242
}
@@ -256,20 +256,21 @@ func (c *Cli) getTemplate(name string) string {
256256
if override, ok := c.opts["template"].(string); ok {
257257
if _, err := os.Stat(override); err == nil {
258258
return readFile(override)
259-
} else {
260-
if t := getLookedUpTemplate(override, all_templates[override]); t != "" {
261-
return t
262-
}
259+
}
260+
if t := getLookedUpTemplate(override, allTemplates[override]); t != "" {
261+
return t
263262
}
264263
}
265264
// create-bug etc are special, if we dont find it in the path
266265
// then just return the create template
267266
if strings.HasPrefix(name, "create-") {
268267
return getLookedUpTemplate(name, c.getTemplate("create"))
269268
}
270-
return getLookedUpTemplate(name, all_templates[name])
269+
return getLookedUpTemplate(name, allTemplates[name])
271270
}
272271

272+
// NoChangesFound is an error returned from when editing templates
273+
// and no modifications were made while editing
273274
type NoChangesFound struct{}
274275

275276
func (f NoChangesFound) Error() string {
@@ -360,27 +361,27 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m
360361
}
361362

362363
edited := make(map[string]interface{})
363-
if fh, err := ioutil.ReadFile(tmpFileName); err != nil {
364+
var data []byte
365+
if data, err = ioutil.ReadFile(tmpFileName); err != nil {
364366
log.Errorf("Failed to read tmpfile %s: %s", tmpFileName, err)
365367
if editing && promptYN("edit again?", true) {
366368
continue
367369
}
368370
return err
369-
} else {
370-
if err := yaml.Unmarshal(fh, &edited); err != nil {
371-
log.Errorf("Failed to parse YAML: %s", err)
372-
if editing && promptYN("edit again?", true) {
373-
continue
374-
}
375-
return err
371+
}
372+
if err := yaml.Unmarshal(data, &edited); err != nil {
373+
log.Errorf("Failed to parse YAML: %s", err)
374+
if editing && promptYN("edit again?", true) {
375+
continue
376376
}
377+
return err
377378
}
378379

379-
if fixed, err := yamlFixup(edited); err != nil {
380+
var fixed interface{}
381+
if fixed, err = yamlFixup(edited); err != nil {
380382
return err
381-
} else {
382-
edited = fixed.(map[string]interface{})
383383
}
384+
edited = fixed.(map[string]interface{})
384385

385386
// if you want to abort editing a jira issue then
386387
// you can add the "abort: true" flag to the document
@@ -422,6 +423,7 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m
422423
return nil
423424
}
424425

426+
// Browse will open up your default browser to the provided issue
425427
func (c *Cli) Browse(issue string) error {
426428
if val, ok := c.opts["browse"].(bool); ok && val {
427429
if runtime.GOOS == "darwin" {
@@ -433,40 +435,47 @@ func (c *Cli) Browse(issue string) error {
433435
return nil
434436
}
435437

438+
// SaveData will write out the yaml formated --saveFile file with provided data
436439
func (c *Cli) SaveData(data interface{}) error {
437440
if val, ok := c.opts["saveFile"].(string); ok && val != "" {
438441
yamlWrite(val, data)
439442
}
440443
return nil
441444
}
442445

446+
// ViewIssue will return the details for the given issue id
443447
func (c *Cli) ViewIssue(issue string) (interface{}, error) {
444448
uri := fmt.Sprintf("%s/rest/api/2/issue/%s", c.endpoint, issue)
445449
if x := c.expansions(); len(x) > 0 {
446450
uri = fmt.Sprintf("%s?expand=%s", uri, strings.Join(x, ","))
447451
}
448452

449-
data, err := responseToJson(c.get(uri))
453+
data, err := responseToJSON(c.get(uri))
450454
if err != nil {
451455
return nil, err
452-
} else {
453-
return data, nil
454456
}
457+
return data, nil
455458
}
456459

460+
// FindIssues will return a list of issues that match the given options.
461+
// If the "query" option is undefined it will generate a JQL query
462+
// using any/all of the provide options: project, component, assignee,
463+
// issuetype, watcher, reporter, sort
464+
// Further it will restrict the fields being extracted from the jira
465+
// response with the 'queryfields' option
457466
func (c *Cli) FindIssues() (interface{}, error) {
458467
var query string
459468
var ok bool
460469
// project = BAKERY and status not in (Resolved, Closed)
461470
if query, ok = c.opts["query"].(string); !ok {
462471
qbuff := bytes.NewBufferString("resolution = unresolved")
463-
if project, ok := c.opts["project"]; !ok {
472+
var project string
473+
if project, ok = c.opts["project"].(string); !ok {
464474
err := fmt.Errorf("Missing required arguments, either 'query' or 'project' are required")
465475
log.Errorf("%s", err)
466476
return nil, err
467-
} else {
468-
qbuff.WriteString(fmt.Sprintf(" AND project = '%s'", project))
469477
}
478+
qbuff.WriteString(fmt.Sprintf(" AND project = '%s'", project))
470479

471480
if component, ok := c.opts["component"]; ok {
472481
qbuff.WriteString(fmt.Sprintf(" AND component = '%s'", component))
@@ -495,11 +504,9 @@ func (c *Cli) FindIssues() (interface{}, error) {
495504
query = qbuff.String()
496505
}
497506

498-
fields := make([]string, 0)
507+
fields := []string{"summary"}
499508
if qf, ok := c.opts["queryfields"].(string); ok {
500509
fields = strings.Split(qf, ",")
501-
} else {
502-
fields = append(fields, "summary")
503510
}
504511

505512
json, err := jsonEncode(map[string]interface{}{
@@ -514,40 +521,42 @@ func (c *Cli) FindIssues() (interface{}, error) {
514521
}
515522

516523
uri := fmt.Sprintf("%s/rest/api/2/search", c.endpoint)
517-
if data, err := responseToJson(c.post(uri, json)); err != nil {
524+
var data interface{}
525+
if data, err = responseToJSON(c.post(uri, json)); err != nil {
518526
return nil, err
519-
} else {
520-
return data, nil
521527
}
528+
return data, nil
522529
}
523530

531+
// GetOptString will extract the string from the Cli object options
532+
// otherwise return the provided default
524533
func (c *Cli) GetOptString(optName string, dflt string) string {
525534
return c.getOptString(optName, dflt)
526535
}
527536

528537
func (c *Cli) getOptString(optName string, dflt string) string {
529538
if val, ok := c.opts[optName].(string); ok {
530539
return val
531-
} else {
532-
return dflt
533540
}
541+
return dflt
534542
}
535543

544+
// GetOptBool will extract the boolean value from the Client object options
545+
// otherwise return the provided default\
536546
func (c *Cli) GetOptBool(optName string, dflt bool) bool {
537547
return c.getOptBool(optName, dflt)
538548
}
539549

540550
func (c *Cli) getOptBool(optName string, dflt bool) bool {
541551
if val, ok := c.opts[optName].(bool); ok {
542552
return val
543-
} else {
544-
return dflt
545553
}
554+
return dflt
546555
}
547556

548557
// expansions returns a comma-separated list of values for field expansion
549558
func (c *Cli) expansions() []string {
550-
expansions := make([]string, 0)
559+
var expansions []string
551560
if x, ok := c.opts["expand"].(string); ok {
552561
expansions = strings.Split(x, ",")
553562
}

0 commit comments

Comments
 (0)