Skip to content

Commit

Permalink
Merge pull request #13 from iamthen0ise/split-project-fix-errors
Browse files Browse the repository at this point in the history
Split project. Add tests. Fix Errors
  • Loading branch information
Eugene Uvarov authored Aug 1, 2021
2 parents 4b44ffa + 9dd5a22 commit bcb258a
Show file tree
Hide file tree
Showing 11 changed files with 317 additions and 162 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Go Build & Test

on:
push:
branches: [ stable ]
pull_request:
branches: [ stable ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16

- name: Build
run: make build

- name: Test
run: make test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,5 @@ FodyWeavers.xsd
# End of https://www.toptal.com/developers/gitignore/api/windows,linux,osx,go,visualstudio,visualstudiocode,intellij

.env
.goreleaser.yml
.goreleaser.yml
.dist/*
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build:
go build -v ./...
test:
go test -cover -coverprofile=cover.test -v ./... -coverpkg=./...
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
module github.io/iamthen0ise/bb

go 1.16
186 changes: 25 additions & 161 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,204 +5,68 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
)

var RE = regexp.MustCompile(JIRA_RE)

const (
// ISSUE FLAVOUR REGEX
JIRA_RE = `([A-Z]+-[\d]+)`

// ANSI TERMINAL CURSOR MANIPULATION
RESET = "\u001b[0m"
SAVE_POSITION = "\033[s"
RESTORE_POSITION = "\033[u"
ERASE_LINE_TO_END = "\033[K"

// ANSI TERMINAL FOREGROUND COLOR CODES
MAGENTA = "\u001b[36m"
YELLOW = "\u001b[33m"
BRIGHT_BLACK = "\u001b[30;1m"
p "github.io/iamthen0ise/bb/src/parsing"
s "github.io/iamthen0ise/bb/src/screen"
)

func colorize(t *string, c string) string {
var sb strings.Builder

sb.WriteString(c)
sb.WriteString(*t)
sb.WriteString(RESET)

return sb.String()

}

type ParsedArgs struct {
prefix string
issueID string
customText string
}

func (p *ParsedArgs) parseRawInput(t *string) {
var matches []string = RE.FindAllString(strings.ToUpper(*t), -1)
if len(matches) > 0 {
p.issueID = matches[0]
} else if *t == "f" {
p.prefix = "feature"
} else if *t == "h" {
p.prefix = "hotfix"

} else {
p.customText = *t
}
}

type BranchArgs struct {
prefix string
issueID string
customText string
value string
}

func (b *BranchArgs) updateValue() {
var sb strings.Builder
if len(b.prefix) > 0 {
sb.WriteString(b.prefix)
sb.WriteString("/")
}

sb.WriteString(b.issueID)
if len(b.issueID) > 0 && len(b.customText) > 0 {
sb.WriteString("-")
}
sb.WriteString(strings.Join(strings.Fields(b.customText), "-"))

b.value = sb.String()
}

func (b *BranchArgs) updateFields(p string, i string, t string) {
b.prefix = p
b.issueID = i
b.customText = t
b.updateValue()
}

func (b *BranchArgs) createBranch(checkout bool) error {
err := executeGitCommand(b.value, checkout)
return err
}

func executeGitCommand(branchName string, checkout bool) error {
app := "git"

var subcommand string
var args []string

if checkout {
subcommand = "checkout"
args = append(args, "-b")
} else {
subcommand = "branch"
}

cmd := exec.Command(app, subcommand, strings.Join(args, " "), branchName)
_, err := cmd.Output()

return err
}

func main() {
var (
branchArgs = BranchArgs{}

issue string
ct string
prefix string

feature string
hotfix string

withCheckout bool = true
inputArgs = p.InputArgs{}
gitBranchName = p.GitBranchName{}
)

flag.StringVar(&issue, "i", "", "JIRA Link or issue")
flag.StringVar(&ct, "t", "", "Custom Issue Text")
flag.StringVar(&feature, "f", "", "set `feature` prefix")
flag.StringVar(&hotfix, "h", "", "set `hotfix` prefix")
flag.BoolVar(&withCheckout, "c", true, "Checkout to new branch (default `true`")

flag.String("i", "", "JIRA Link or issue")
flag.String("t", "", "Custom Issue Text")
flag.Bool("f", false, "Set `feature` prefix")
flag.Bool("h", false, "Set `hotfix` prefix")
flag.Bool("c", true, "Checkout to new branch (default `true`")
flag.Parse()

var pargs = ParsedArgs{}
if len(os.Args) > 1 {
inputArgs.ParseArgs(os.Args[1:])
gitBranchName.UpdateFields(inputArgs.Prefix, inputArgs.IssueID, inputArgs.CustomTextParts)

if len(os.Args[1:]) < 1 {
} else {
inputScanner := bufio.NewScanner(os.Stdin)

var promptDefault string = "Enter JIRA Issue link or some text. Press Enter once to submit or twice when ready > "
var promptCurrentLine *string = &branchArgs.value
var promptDefault = "Enter JIRA Issue link or some text. Press Enter once to submit or twice when ready > "
var promptCurrentLine = &gitBranchName.BranchName

fmt.Print(SAVE_POSITION)
fmt.Print(s.SavePosition)

for {
fmt.Print(RESTORE_POSITION, ERASE_LINE_TO_END)
fmt.Print(s.RestorePosition, s.EraseLineToEnd)

if len(*promptCurrentLine) > 0 {
fmt.Print("Branch Name is: ", colorize(promptCurrentLine, YELLOW), " [Enter to finish] > ")
fmt.Print("Branch Name is: ", s.Colorize(promptCurrentLine, s.Yellow), " [Enter to finish] > ")
} else {
fmt.Print(colorize(&promptDefault, YELLOW), " ")
fmt.Print(s.Colorize(&promptDefault, s.Yellow), " ")
}

inputScanner.Scan()
text := inputScanner.Text()
pargs.parseRawInput(&text)
inputArgs.ParseArg(&text)

if pargs.issueID == "" && len(text) > 1 {
pargs.customText = text
} else if strings.ContainsAny(text, "fh") {
pargs.customText = ""
if inputArgs.IssueID == "" && len(text) > 1 {
inputArgs.CustomTextParts = append(inputArgs.CustomTextParts, text)
} else if len(text) == 0 {
break
}
branchArgs.updateFields(pargs.prefix, pargs.issueID, pargs.customText)
}
} else {
pargs.parseRawInput(&issue)

if len(pargs.issueID) < 1 {
var arg = flag.Arg(0)
pargs.parseRawInput(&arg)
}

if prefix != "" {
pargs.prefix = prefix
} else if feature != "" {
pargs.prefix = "feature"
} else if hotfix != "" {
pargs.prefix = "hotfix"
}

if len(pargs.customText) < 1 {
pargs.customText = ct
gitBranchName.UpdateFields(inputArgs.Prefix, inputArgs.IssueID, inputArgs.CustomTextParts)
}

if flag.NArg() > 0 {
pargs.customText = pargs.customText + " " + strings.Join(flag.Args()[1:], " ")
}

branchArgs.updateFields(pargs.prefix, pargs.issueID, pargs.customText)
}

fmt.Println("Your branch name is:", colorize(&branchArgs.value, MAGENTA))
fmt.Println("Your branch name is:", s.Colorize(&gitBranchName.BranchName, s.Magenta))
fmt.Println("Do you want to continue and create branch? [Enter to continue]")

scannerCreateBranch := bufio.NewScanner(os.Stdin)
scannerCreateBranch.Scan()

switch strings.ToLower(scannerCreateBranch.Text()) {
case "":
err := branchArgs.createBranch(withCheckout)
err := gitBranchName.CreateBranch(true)
if err != nil {
fmt.Print("Something went wrong,", err.Error())
}
Expand Down
25 changes: 25 additions & 0 deletions src/git/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package git

import (
"os/exec"
"strings"
)

func ExecuteGitCommand(branchName string, checkout bool) error {
app := "git"

var subcommand string
var args []string

if checkout {
subcommand = "checkout"
args = append(args, "-b")
} else {
subcommand = "branch"
}

cmd := exec.Command(app, subcommand, strings.Join(args, " "), branchName)
_, err := cmd.Output()

return err
}
42 changes: 42 additions & 0 deletions src/parsing/branch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package parsing

import (
"strings"

"github.io/iamthen0ise/bb/src/git"
)

type GitBranchName struct {
Prefix string
IssueID string
CustomTextParts []string
BranchName string
}

func (o *GitBranchName) BuildBranchName() {
var sb strings.Builder
if len(o.Prefix) > 0 {
sb.WriteString(o.Prefix)
sb.WriteString("/")
}

sb.WriteString(o.IssueID)
if len(o.IssueID) > 0 && len(o.CustomTextParts) > 0 {
sb.WriteString("-")
}
sb.WriteString(strings.Join(o.CustomTextParts, "-"))

o.BranchName = sb.String()
}

func (o *GitBranchName) UpdateFields(p string, i string, tp []string) {
o.Prefix = p
o.IssueID = i
o.CustomTextParts = tp
o.BuildBranchName()
}

func (o *GitBranchName) CreateBranch(checkout bool) error {
err := git.ExecuteGitCommand(o.BranchName, checkout)
return err
}
44 changes: 44 additions & 0 deletions src/parsing/parsing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package parsing

import (
"regexp"
"strings"
)

const JiraRe = `([A-Z]+-[\d]+)`

var (
RE = regexp.MustCompile(JiraRe)
FlagConstants = []string{"-i", "--i", "-t", "--t", "-c", "--c"}
)

type InputArgs struct {
Prefix string
IssueID string
CustomTextParts []string
}

func (a *InputArgs) ParseArg(t *string) {
for _, char := range FlagConstants {
if *t == char {
return
}
}

var issuerIdMatches = RE.FindAllString(strings.ToUpper(*t), -1)
if len(issuerIdMatches) > 0 {
a.IssueID = issuerIdMatches[0]
} else if strings.Trim(*t, "-") == "f" {
a.Prefix = "feature"
} else if strings.Trim(*t, "-") == "h" {
a.Prefix = "hotfix"
} else {
a.CustomTextParts = append(a.CustomTextParts, *t)
}
}

func (a *InputArgs) ParseArgs(s []string) {
for _, c := range s {
a.ParseArg(&c)
}
}
Loading

0 comments on commit bcb258a

Please sign in to comment.