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"`
}
func newIssue(f []string) *issue {
func newIssue(f []string, prj string) *issue {
return &issue{
Fields: fields{
Title: f[0],
@ -36,7 +36,7 @@ func newIssue(f []string) *issue {
Description: f[2],
Labels: strings.Split(f[3], ","),
Project: project{
Key: *prj,
Key: prj,
},
},
}

20
jira.go
View File

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

23
main.go
View File

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