Skip to content

Commit b3a03da

Browse files
committed
change checkout command to have list of branchTypes and jiraTicket selections
1 parent 6795d13 commit b3a03da

File tree

6 files changed

+294
-130
lines changed

6 files changed

+294
-130
lines changed

cmd/checkout/config.go

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/charmbracelet/bubbles/list"
7+
"os"
8+
"path/filepath"
9+
)
10+
11+
type branchType struct {
12+
T string `json:"type"`
13+
D string `json:"description"`
14+
}
15+
16+
type jiraBoard struct {
17+
Name string `json:"name"`
18+
D string `json:"description"`
19+
}
20+
21+
type config struct {
22+
BranchTypes []branchType `json:"branchTypes"`
23+
JiraBoards []jiraBoard `json:"jiraBoards"`
24+
}
25+
26+
const configFile = ".git-branch-checkout-2.json"
27+
28+
var defaultBranchTypes = []list.Item{
29+
branchType{"FEAT", "A new feature"},
30+
branchType{"FIX", "A bug fix"},
31+
branchType{"IMPR", "An improvement to a feature or enhancement"},
32+
branchType{"OPS", "Changes to our CI configuration files and scripts"},
33+
branchType{"CHORE", "Updating grunt tasks etc; no production code change"},
34+
}
35+
36+
var defaultJiraBoards = []list.Item{
37+
jiraBoard{"EPD", "CPO team board"},
38+
jiraBoard{"IB", "Interim billing board"},
39+
}
40+
41+
func convertBranchTypes(branchTypes []branchType) []list.Item {
42+
items := []list.Item{}
43+
for _, branchType := range branchTypes {
44+
items = append(items, branchType)
45+
}
46+
if len(items) == 0 {
47+
return defaultBranchTypes
48+
}
49+
return items
50+
}
51+
52+
func convertBoards(boards []jiraBoard) []list.Item {
53+
items := []list.Item{}
54+
for _, board := range boards {
55+
items = append(items, board)
56+
}
57+
if len(items) == 0 {
58+
return nil
59+
}
60+
return items
61+
}
62+
63+
func loadConfigFile(path string) ([]list.Item, []list.Item, error) {
64+
data, err := os.ReadFile(path)
65+
if err != nil {
66+
return nil, nil, fmt.Errorf("error reading config file: %w", err)
67+
}
68+
var c config
69+
if err := json.Unmarshal(data, &c); err != nil {
70+
return nil, nil, fmt.Errorf("error parsing config file: %w", err)
71+
}
72+
return convertBranchTypes(c.BranchTypes), convertBoards(c.JiraBoards), nil
73+
}
74+
75+
func loadConfig() ([]list.Item, []list.Item, error) {
76+
basePath, err := os.UserHomeDir()
77+
if err != nil {
78+
return nil, nil, fmt.Errorf("error getting home dir: %w", err)
79+
}
80+
targetPath, err := os.Getwd()
81+
if err != nil {
82+
return nil, nil, fmt.Errorf("error getting current dir: %w", err)
83+
}
84+
for {
85+
rel, _ := filepath.Rel(basePath, targetPath)
86+
if rel == "." {
87+
break
88+
}
89+
filePath := filepath.Join(targetPath, configFile)
90+
if _, err := os.Open(filePath); err == nil {
91+
fmt.Println("Found config file at", filePath)
92+
return loadConfigFile(filePath)
93+
}
94+
95+
targetPath += "/.."
96+
}
97+
fmt.Println("No config file found, using default config")
98+
return defaultBranchTypes, defaultJiraBoards, nil
99+
}

cmd/checkout/git.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
)
7+
8+
func checkoutNewBranch(branchName string) error {
9+
cmd := exec.Command("git", "checkout", "-b", branchName)
10+
cmd.Stdout = os.Stdout
11+
cmd.Stderr = os.Stderr
12+
return cmd.Run()
13+
}
14+
15+
func checkIfGitRepo() bool {
16+
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
17+
cmd.Stdout = os.Stdout
18+
cmd.Stderr = os.Stderr
19+
err := cmd.Run()
20+
if err != nil {
21+
return false
22+
}
23+
return true
24+
}

0 commit comments

Comments
 (0)