Episode #15 — Github & Jira CLI with Golang part 4: Regular expressions, cli interactions and more

Stephens Xu
Fullstack Network
Published in
2 min readJun 5, 2017

--

In this session we continued to work on Gong, our CLI tool for Jira & Github.

We implemented two features: gong browse, which opens up a web browser and navigate to the Jira web page of our current ticket. gong comment, which adds multiline comments to the ticket.

In implementing gong browse, we ran into a bit of interesting regular expression problem. Basically we need to extract a ticket ID name from a string that contains the ID, construct an URL based on that ID, open up the browser from CLI and then navigate to that URL.

The Jira url we want to open for this particular ticket looks like this:

<jira_doman>/browse/<ticket_id>

Our current git branch name is always a combination of three elements in the format of <type>/<ticket_id>-<name-of-ticke>like so:

feature/GLOB-1111-build-this-GID-1234hh

feature is type, GLOB-1111 is the ticket ID and the name of ticket is followed by a dash(all dash separated string).

We know that the ticket ID(GLOB-1111) is ALWAYS followed by / and is a combination of all capitalized alphabetical letters, followed by - , then followed by a series of numbers. We also know the end of ticket ID is also followed by a -. So this is how we implemented:

import (
"errors"
"regexp"
)

func GetIssueID(branchName string) (string, error) {
re := regexp.MustCompile(`([A-Z]+-[\d]+)`)
matches := re.FindAllString(branchName, -1)

if len(matches) == 0 {
return "", errors.New("No matches found in the branch name")
}

return matches[0], nil
}

regexplanet.com is a great tool to test Go regex

As shown, ([A-Z]+-[\d]+) would give us what we want. One detail worth mentioning here is that is we use regex.MustCompie method, we need to wrap the regex in ` ` instead of quotations, otherwise the forward slash would break.

CLI interactions

Working on this CLI is very interesting since the interactions with it makes the tool good/bad.

UX of a CLI is very important, you need operations to be clear and smooth, have a clear input and a clear output and even better error messages.

I hope this small post would help you a bit with your Golang skills. We’ll be doing more streaming this week, welcomed to join us!

--

--