stopped relying on globals; globals are bad

This commit is contained in:
Colin Henry 2015-10-24 18:19:24 -07:00
parent 65f2455009
commit 203ceb23a3
3 changed files with 29 additions and 18 deletions

View File

@ -26,7 +26,7 @@ type project struct {
Key string `json:"key"` Key string `json:"key"`
} }
func newIssue(f []string) *issue { func newIssue(f []string, prj string) *issue {
return &issue{ return &issue{
Fields: fields{ Fields: fields{
Title: f[0], Title: f[0],
@ -36,7 +36,7 @@ func newIssue(f []string) *issue {
Description: f[2], Description: f[2],
Labels: strings.Split(f[3], ","), Labels: strings.Split(f[3], ","),
Project: project{ Project: project{
Key: *prj, Key: prj,
}, },
}, },
} }

20
jira.go
View File

@ -7,12 +7,10 @@ import (
"net/http" "net/http"
) )
// func init() { var client *http.Client
// client =
// }
func submitIssue(i []string) error { func init() {
client := &http.Client{ client = &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
TLSClientConfig: &tls.Config{ TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, InsecureSkipVerify: true,
@ -20,17 +18,23 @@ func submitIssue(i []string) error {
}, },
} }
issue := newIssue(i) }
func submitIssue(i []string, user string, pass string, svr string, prj string) error {
issue := newIssue(i, prj)
issueJSON := toJSON(issue) issueJSON := toJSON(issue)
uri := fmt.Sprintf("%s/rest/api/2/issue/", svr) uri := fmt.Sprintf("%s/rest/api/2/issue/", svr)
req, _ := http.NewRequest("POST", uri, bytes.NewBuffer(issueJSON)) req, _ := http.NewRequest("POST", uri, bytes.NewBuffer(issueJSON))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(*user, *pass) req.SetBasicAuth(user, pass)
/*resp*/ _, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return err return err
} }
fmt.Printf(resp)
return nil return nil
} }

23
main.go
View File

@ -24,13 +24,20 @@ func main() {
bufrdr := bufio.NewReader(os.Stdin) bufrdr := bufio.NewReader(os.Stdin)
bufrdr.ReadLine() bufrdr.ReadLine()
bufrdr.ReadLine() bufrdr.ReadLine()
csvrdr := csv.NewReader(bufrdr) // processIssue(csvrdr, printJSON)
processIssue(csvrdr, printJSON)
processIssue(bufrdr, submitIssue, *user, *pass, *svr, *prj)
} }
func processIssue(r *csv.Reader, process func(issue []string) error) error { type procFunc func(i []string, user string, pass string, srv string, prj string) error
func processIssue(r io.Reader, process procFunc, user string, pass string, svr string, prj string) error {
csvrdr := csv.NewReader(r)
for { for {
record, err := r.Read() record, err := csvrdr.Read()
if err == io.EOF { if err == io.EOF {
break break
} }
@ -38,7 +45,7 @@ func processIssue(r *csv.Reader, process func(issue []string) error) error {
log.Fatal(err) log.Fatal(err)
} }
err = process(record) err = process(record, user, pass, svr, prj)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -47,12 +54,12 @@ func processIssue(r *csv.Reader, process func(issue []string) error) error {
return nil return nil
} }
func printCSV(issue []string) error { func printCSV(issue []string, user string, pass string, svr string, prj string) error {
fmt.Println(issue) fmt.Println(issue)
return nil return nil
} }
func printJSON(issue []string) error { func printJSON(issue []string, user string, pass string, svr string, prj string) error {
i := newIssue(issue) i := newIssue(issue, prj)
fmt.Println(string(toJSON(i))) fmt.Println(string(toJSON(i)))
return nil return nil
} }