forked from marouni/adr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd.go
83 lines (70 loc) · 2.17 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"github.com/fatih/color"
"os"
"github.com/spf13/cobra"
)
func cmdExecInit() *cobra.Command {
var directory, readme string
var command = &cobra.Command{
Use: "init",
Short: "Initializes the ADR configurations",
Long: "Initializes the ADR configuration with an optional ADR base directory\n This is a a prerequisite to running any other adr sub-command",
Run: func(cmd *cobra.Command, args []string) {
helper := NewAdrHelper(directory, readme)
if err := helper.InitBaseDir(directory); err != nil {
msg := fmt.Sprintf("ops failed to init dir: %s\n", err)
color.Red(msg)
return
}
if err := helper.InitConfig(); err != nil {
msg := fmt.Sprintf("ops failed to init config: %s\n", err)
color.Red(msg)
return
}
if err := helper.InitTemplate(); err != nil {
msg := fmt.Sprintf("ops failed to init template: %s\n", err)
color.Red(msg)
return
}
color.Green("Initializing ADR base at " + helper.baseDir)
return
},
}
command.Flags().StringVarP(&directory, "directory", "d", adrDefaultBaseDirName, "adr directory path")
command.Flags().StringVarP(&readme, "readme", "r", adrDefaultReadmeName, "Readme.md to append index of new records to")
return command
}
func cmdExecNew() *cobra.Command {
var directory, title string
var command = &cobra.Command{
Use: "new",
Short: "Create a new ADR",
Long: "",
Run: func(cmd *cobra.Command, args []string) {
helper := NewAdrHelper(directory, "")
currentConfig := helper.GetConfig()
currentConfig.CurrentAdr++
if err := helper.UpdateConfig(currentConfig); err != nil {
msg := fmt.Sprintf("ops failed to init template: %s\n", err)
color.Red(msg)
}
helper.NewAdr(currentConfig, title)
},
}
command.Flags().StringVarP(&directory, "directory", "d", adrDefaultBaseDirName, "adr directory path")
command.Flags().StringVarP(&title, "title", "t", "", "adr title")
return command
}
func runCmd() {
var rootCmd = &cobra.Command{Use: "adr"}
rootCmd.AddCommand(cmdExecInit())
rootCmd.AddCommand(cmdExecNew())
err := rootCmd.Execute()
if err != nil {
fmt.Printf("ops.. failed to execute command: %s\n", err)
os.Exit(1)
}
return
}