Debian Codegraph @

cmd/new.go

raw
1 package cmd
2
3 import (
4 "io"
5 "os"
6 "path/filepath"
7 "strings"
8
9 "github.com/spf13/cobra"
10
11 "github.com/miniscruff/changie/core"
12 )
13
14 type New struct {
15 *cobra.Command
16
17 // cli args
18 DryRun bool
19 Projects []string
20 Component string
21 Kind string
22 Body string
23 BodyEditor bool
24 Custom []string
25 Interactive bool
26
27 // dependencies
28 TimeNow core.TimeNow
29 TemplateCache *core.TemplateCache
30 }
31
32 func NewNew(
33 timeNow core.TimeNow,
34 templateCache *core.TemplateCache,
35 ) *New {
36 n := &New{
37 TimeNow: timeNow,
38 TemplateCache: templateCache,
39 }
40
41 cmd := &cobra.Command{
42 Use: "new",
43 Short: "Create a new change file",
44 Long: `Creates a new change file.
45 Change files are processed when batching a new release.
46 Each version is merged together for the overall project changelog.
47
48 Prompts are disabled and this command will fail if any values
49 are not defined or valid and if any of the following are true:
50
51 Custom prompt values can also be passed in via an environment variable.
52 Use the following format: "${env var prefix}_CUSTOM_${custom key}=value".
53 Example: "CHANGIE_CUSTOM_Author=miniscruff"
54
55 1. CI env var is true
56 2. --interactive=false
57 `,
58 Args: cobra.NoArgs,
59 RunE: n.Run,
60 }
61
62 cmd.Flags().BoolVarP(
63 &n.DryRun,
64 "dry-run", "d",
65 false,
66 "Print new fragment instead of writing to disk",
67 )
68 cmd.Flags().StringSliceVarP(
69 &n.Projects,
70 "projects", "j",
71 []string{},
72 "Set the change projects without a prompt",
73 )
74 cmd.Flags().StringVarP(
75 &n.Component,
76 "component", "c",
77 "",
78 "Set the change component without a prompt",
79 )
80 cmd.Flags().StringVarP(
81 &n.Kind,
82 "kind", "k",
83 "",
84 "Set the change kind without a prompt",
85 )
86 cmd.Flags().StringVarP(
87 &n.Body,
88 "body", "b",
89 "",
90 "Set the change body without a prompt",
91 )
92 cmd.Flags().BoolVarP(
93 &n.BodyEditor,
94 "editor", "e",
95 false,
96 "Edit body message using your text editor defined by 'EDITOR' env variable",
97 )
98 cmd.Flags().StringSliceVarP(
99 &n.Custom,
100 "custom", "m",
101 nil,
102 "Set custom values without a prompt",
103 )
104 cmd.Flags().BoolVarP(
105 &n.Interactive,
106 "interactive",
107 "i",
108 true,
109 "Set missing values with prompts",
110 )
111
112 n.Command = cmd
113
114 return n
115 }
116
117 func (n *New) Run(cmd *cobra.Command, args []string) error {
118 config, err := core.LoadConfig()
119 if err != nil {
120 return err
121 }
122
123 customValues, err := core.CustomMapFromStrings(n.Custom)
124 if err != nil {
125 return err
126 }
127
128 changieEnvs := config.EnvVars()
129 for key, value := range changieEnvs {
130 key, found := strings.CutPrefix(key, "CUSTOM_")
131 if found {
132 customValues[key] = value
133 }
134 }
135
136 prompts := &core.Prompts{
137 StdinReader: n.InOrStdin(),
138 BodyEditor: n.BodyEditor,
139 Projects: n.Projects,
140 Component: n.Component,
141 Kind: n.Kind,
142 Body: n.Body,
143 TimeNow: n.TimeNow,
144 Config: config,
145 Customs: customValues,
146 EditorCmdBuilder: core.BuildCommand,
147 Enabled: n.parsePromptEnabled(),
148 }
149
150 changes, err := prompts.BuildChanges()
151 if err != nil {
152 return err
153 }
154
155 for _, change := range changes {
156 var writer io.Writer
157
158 if n.DryRun {
159 writer = n.OutOrStdout()
160 } else {
161 var fragmentWriter strings.Builder
162
163 fileErr := n.TemplateCache.Execute(config.FragmentFileFormat, &fragmentWriter, change)
164 if fileErr != nil {
165 return fileErr
166 }
167
168 fragmentWriter.WriteString(".yaml")
169 outputFilename := fragmentWriter.String()
170
171 // Sanatize the filename to remove invalid characters such as slashes
172 replacer := strings.NewReplacer("/", "-", "\\", "-")
173 outputFilename = replacer.Replace(outputFilename)
174
175 outputPath := filepath.Join(config.ChangesDir, config.UnreleasedDir, outputFilename)
176
177 fileErr = os.MkdirAll(filepath.Dir(outputPath), core.CreateDirMode)
178 if fileErr != nil {
179 return fileErr
180 }
181
182 newFile, fileErr := os.Create(outputPath)
183 if fileErr != nil {
184 return fileErr
185 }
186
187 defer newFile.Close()
188
189 writer = newFile
190 }
191
192 _, err = change.WriteTo(writer)
193 if err != nil {
194 return err
195 }
196 }
197
198 return err
199 }
200
201 func (n *New) parsePromptEnabled() bool {
202 return n.Interactive && strings.ToLower(os.Getenv("CI")) != "true"
203 }