|
| 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 | +} |
0 commit comments