Skip to content

Commit

Permalink
The code
Browse files Browse the repository at this point in the history
  • Loading branch information
ipanardian committed Aug 19, 2018
1 parent a50f103 commit 4441cf9
Show file tree
Hide file tree
Showing 7 changed files with 595 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
*.dll
*.so
*.dylib
gobranch

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# vscode
.vscode
62 changes: 62 additions & 0 deletions core/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 ipanardian <https://github.com/ipanardian>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, Subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or Substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package core

import (
"github.com/urfave/cli"
)

//InitializeApp Init App
func InitializeApp(app *cli.App) {
app.Name = "GoBranch"
app.Usage = "A command line app to help you quickly creating git branch without hassle step."
app.UsageText = "Just type gobranch"
app.Version = "0.0.0.1"
app.Author = "Ipan Ardian <https://github.com/ipanardian>"
}

//SetFlags Set the flags
func SetFlags(app *cli.App) {
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "tc",
Value: "_",
Usage: "Set type convention ('/', '-', '_'). e.g feature/{branch}, feature-{branch} or feature_{branch}",
},
cli.StringFlag{
Name: "nc",
Value: "snake",
Usage: "Set naming convention ('snake', 'kebab'). e.g branch_name or branch-name",
},
}
}

//SetActions Set the actions
func SetActions(app *cli.App) {
app.Action = func(c *cli.Context) error {
execute(c)
return nil
}
}
117 changes: 117 additions & 0 deletions core/ask.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 ipanardian <https://github.com/ipanardian>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, Subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or Substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package core

import (
"fmt"
"os"

"github.com/AlecAivazis/survey"
)

//Answers struct for answer
type Answers struct {
branchType string
baseBranchName string
branchName string
}

//getBranchTypeQuestion Question for type of branch
func getBranchTypeQuestion(branchType BranchType) []*survey.Question {
return []*survey.Question{
{
Name: "branchType",
Prompt: &survey.Select{
Message: "Please choose type of branch:",
Options: []string{
branchType.Feature,
branchType.Enhance,
branchType.Bugfix,
branchType.Hotfix,
branchType.Hotfeature,
branchType.Test,
branchType.Custom,
},
Default: "Feature",
},
Transform: TransformInput,
},
}
}

//getCustomBaseBranchQuestion Question for base branch
func getCustomBaseBranchQuestion() []*survey.Question {
return []*survey.Question{
{
Name: "baseBranchName",
Prompt: &survey.Input{Message: "What's your BASE branch name? e.g master"},
Validate: survey.Required,
Transform: TransformInput,
},
}
}

//getBranchNameQuestion Question for branch name
func getBranchNameQuestion() []*survey.Question {
return []*survey.Question{
{
Name: "branchName",
Prompt: &survey.Input{Message: "What's your branch name?"},
Validate: survey.Required,
Transform: TransformInput,
},
}
}

//createQuestions Create the questions
func createQuestions() Answers {
answers := Answers{}

branchTypeQuest := getBranchTypeQuestion(branchType)
err := survey.Ask(branchTypeQuest, &answers.branchType)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}

if answers.branchType == branchType.Custom {
baseBranchQuest := getCustomBaseBranchQuestion()
err2 := survey.Ask(baseBranchQuest, &answers.baseBranchName)
if err2 != nil {
fmt.Println(err2.Error())
os.Exit(1)
}
branchTypeMap[branchType.Custom] = answers.baseBranchName
}

branchNameQuest := getBranchNameQuestion()
err3 := survey.Ask(branchNameQuest, &answers.branchName)
if err3 != nil {
fmt.Println(err3.Error())
os.Exit(1)
}

return answers
}
139 changes: 139 additions & 0 deletions core/branch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package core

import (
"fmt"
"os"
"strings"

"github.com/urfave/cli"
)

//BranchType List predefined of BranchType
type BranchType struct {
Feature string
Enhance string
Bugfix string
Hotfix string
Hotfeature string
Test string
Custom string
}

//BranchTypeMap Collection of base branch
type BranchTypeMap map[string]string

//TypeConvention Type convention
type TypeConvention struct {
ForwardSlash string
Hyphen string
Underscore string
}

//NamingConvention Naming convention
type NamingConvention struct {
ToSnake string
ToKebab string
}

//typeConvention values
var typeConvention = TypeConvention{
"/",
"-",
"_",
}

//Default TC
var typeConventionSelected = typeConvention.Underscore

//namingConvention values
var namingConvention = NamingConvention{
"snake",
"kebab",
}

//Default NC
var namingConventionSelected = namingConvention.ToSnake

//branchType Init branchType
var branchType = BranchType{
"feature",
"enhance",
"bugfix",
"hotfix",
"hotfeature",
"test",
"custom",
}

//branchTypeMap Init branchTypeMap
var branchTypeMap = BranchTypeMap{
branchType.Feature: "development",
branchType.Enhance: "development",
branchType.Bugfix: "development",
branchType.Hotfix: "hotfix",
branchType.Hotfeature: "hotfix",
branchType.Test: "development",
branchType.Custom: "",
}

//createBranchName Transform branch name
func createBranchName(answer Answers) string {
if len(answer.branchName) == 0 {
fmt.Println("error: Branch name can not be empty.")
os.Exit(1)
}
newBranchName := ToValidBranchName(answer.branchName, namingConventionSelected)
if answer.branchType != branchType.Custom {
newBranchName = strings.Join([]string{answer.branchType, newBranchName}, typeConventionSelected)
}
return newBranchName
}

//createBranch Start create git branch
func createBranch(answer Answers) string {
newBranchName := createBranchName(answer)

checkoutBaseBranch(branchTypeMap[answer.branchType])
pullOriginBranch(branchTypeMap[answer.branchType])
checkoutNewBranch(newBranchName)

return newBranchName
}

//flagActions set actions for flag
func flagActions(c *cli.Context) {
tcFlag := c.String("tc")
ncFlag := c.String("nc")
if len(tcFlag) > 0 {
if tcFlag == typeConvention.ForwardSlash {
typeConventionSelected = typeConvention.ForwardSlash
} else if tcFlag == typeConvention.Hyphen {
typeConventionSelected = typeConvention.Hyphen
} else if tcFlag == typeConvention.Underscore {
typeConventionSelected = typeConvention.Underscore
} else {
fmt.Println("Invalid value of type convention. Available \"/\", \"-\", & \"_\" ")
os.Exit(1)
}
}

if len(ncFlag) > 0 {
if ncFlag == namingConvention.ToSnake {
namingConventionSelected = namingConvention.ToSnake
} else if ncFlag == namingConvention.ToKebab {
namingConventionSelected = namingConvention.ToKebab
} else {
fmt.Println("Invalid value of naming convention. Available \"snake\" & \"kebab\" ")
os.Exit(1)
}
}
}

//execute create question and branch
func execute(c *cli.Context) {
flagActions(c)
answers := createQuestions()
newBranchName := createBranch(answers)

fmt.Printf("Here you go, %s is ready!\n", newBranchName)
}
Loading

0 comments on commit 4441cf9

Please sign in to comment.