Skip to content

Commit

Permalink
use optigo for option parsing, drop docopt
Browse files Browse the repository at this point in the history
  • Loading branch information
Cory Bennett committed Sep 14, 2015
1 parent ea67a77 commit 7bbd571
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 269 deletions.
16 changes: 8 additions & 8 deletions jira/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ var log = logging.MustGetLogger("jira.cli")

type Cli struct {
endpoint *url.URL
opts map[string]string
opts map[string]interface{}
cookieFile string
ua *http.Client
}

func New(opts map[string]string) *Cli {
func New(opts map[string]interface{}) *Cli {
homedir := os.Getenv("HOME")
cookieJar, _ := cookiejar.New(nil)
endpoint, _ := opts["endpoint"]
endpoint, _ := opts["endpoint"].(string)
url, _ := url.Parse(strings.TrimRight(endpoint, "/"))

if project, ok := opts["project"]; ok {
if project, ok := opts["project"].(string); ok {
opts["project"] = strings.ToUpper(project)
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func (c *Cli) makeRequest(req *http.Request) (resp *http.Response, err error) {
}

func (c *Cli) getTemplate(name string) string {
if override, ok := c.opts["template"]; ok {
if override, ok := c.opts["template"].(string); ok {
if _, err := os.Stat(override); err == nil {
return readFile(override)
} else {
Expand Down Expand Up @@ -233,7 +233,7 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m

fh.Close()

editor, ok := c.opts["editor"]
editor, ok := c.opts["editor"].(string)
if !ok {
editor = os.Getenv("JIRA_EDITOR")
if editor == "" {
Expand All @@ -245,7 +245,7 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m
}

editing := true
if val, ok := c.opts["edit"]; ok && val == "false" {
if val, ok := c.opts["edit"].(bool); ok && !val {
editing = false
}

Expand Down Expand Up @@ -340,7 +340,7 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m
}

func (c *Cli) Browse(issue string) error {
if val, ok := c.opts["browse"]; ok && val == "true" {
if val, ok := c.opts["browse"].(bool); ok && val {
if runtime.GOOS == "darwin" {
return exec.Command("open", fmt.Sprintf("%s/browse/%s", c.endpoint, issue)).Run()
} else if runtime.GOOS == "linux" {
Expand Down
22 changes: 14 additions & 8 deletions jira/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (c *Cli) CmdLogin() error {
uri := fmt.Sprintf("%s/rest/auth/1/session", c.endpoint)
for true {
req, _ := http.NewRequest("GET", uri, nil)
user, _ := c.opts["user"]
user, _ := c.opts["user"].(string)

prompt := fmt.Sprintf("Enter Password for %s: ", user)
passwd, _ := gopass.GetPass(prompt)
Expand Down Expand Up @@ -69,7 +69,7 @@ func (c *Cli) CmdList() error {
var query string
var ok bool
// project = BAKERY and status not in (Resolved, Closed)
if query, ok = c.opts["query"]; !ok {
if query, ok = c.opts["query"].(string); !ok {
qbuff := bytes.NewBufferString("resolution = unresolved")
if project, ok := c.opts["project"]; !ok {
err := fmt.Errorf("Missing required arguments, either 'query' or 'project' are required")
Expand Down Expand Up @@ -107,7 +107,7 @@ func (c *Cli) CmdList() error {
}

fields := make([]string, 0)
if qf, ok := c.opts["queryfields"]; ok {
if qf, ok := c.opts["queryfields"].(string); ok {
fields = strings.Split(qf, ",")
} else {
fields = append(fields, "summary")
Expand Down Expand Up @@ -213,7 +213,8 @@ func (c *Cli) CmdTransitionMeta(issue string) error {
return runTemplate(c.getTemplate("transmeta"), data, nil)
}

func (c *Cli) CmdIssueTypes(project string) error {
func (c *Cli) CmdIssueTypes() error {
project := c.opts["project"].(string)
log.Debug("issueTypes called")
uri := fmt.Sprintf("%s/rest/api/2/issue/createmeta?projectKeys=%s", c.endpoint, project)
data, err := responseToJson(c.get(uri))
Expand All @@ -224,7 +225,9 @@ func (c *Cli) CmdIssueTypes(project string) error {
return runTemplate(c.getTemplate("issuetypes"), data, nil)
}

func (c *Cli) CmdCreateMeta(project string, issuetype string) error {
func (c *Cli) CmdCreateMeta() error {
project := c.opts["project"].(string)
issuetype := c.opts["issuetype"].(string)
log.Debug("createMeta called")
uri := fmt.Sprintf("%s/rest/api/2/issue/createmeta?projectKeys=%s&issuetypeNames=%s&expand=projects.issuetypes.fields", c.endpoint, project, issuetype)
data, err := responseToJson(c.get(uri))
Expand Down Expand Up @@ -257,7 +260,9 @@ func (c *Cli) CmdTransitions(issue string) error {
return runTemplate(c.getTemplate("transitions"), data, nil)
}

func (c *Cli) CmdCreate(project string, issuetype string) error {
func (c *Cli) CmdCreate() error {
project := c.opts["project"].(string)
issuetype := c.opts["issuetype"].(string)
log.Debug("create called")

uri := fmt.Sprintf("%s/rest/api/2/issue/createmeta?projectKeys=%s&issuetypeNames=%s&expand=projects.issuetypes.fields", c.endpoint, project, issuetype)
Expand Down Expand Up @@ -399,7 +404,8 @@ func (c *Cli) CmdDups(duplicate string, issue string) error {
return nil
}

func (c *Cli) CmdWatch(issue string, watcher string) error {
func (c *Cli) CmdWatch(issue string) error {
watcher := c.opts["watcher"].(string)
log.Debug("watch called")

json, err := jsonEncode(watcher)
Expand Down Expand Up @@ -568,7 +574,7 @@ func (c *Cli) CmdAssign(issue string, user string) error {
}

func (c *Cli) CmdExportTemplates() error {
dir := c.opts["directory"]
dir := c.opts["directory"].(string)
if err := mkdir(dir); err != nil {
return err
}
Expand Down
Loading

0 comments on commit 7bbd571

Please sign in to comment.