Skip to content

Commit 1c5ce09

Browse files
authored
feat: --output-path flag for pizza generate codeowners (#223)
1 parent 39e7f74 commit 1c5ce09

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

cmd/generate/codeowners/codeowners.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type Options struct {
2626
// The default should be to generate a GitHub style "CODEOWNERS" file.
2727
ownersStyleFile bool
2828

29+
// where the output file will go
30+
outputPath string
31+
2932
// the number of days to look back
3033
previousDays int
3134

@@ -74,6 +77,9 @@ pizza generate codeowners . --owners-style-file
7477
7578
# Specify a custom location for the .sauced.yaml file
7679
pizza generate codeowners . --config /path/to/.sauced.yaml
80+
81+
# Specify a custom output location for the CODEOWNERS file
82+
pizza generate codeowners . --output-path /path/to/directory
7783
`,
7884
Args: func(_ *cobra.Command, args []string) error {
7985
if len(args) != 1 {
@@ -113,6 +119,13 @@ pizza generate codeowners . --config /path/to/.sauced.yaml
113119
}
114120

115121
opts.ownersStyleFile, _ = cmd.Flags().GetBool("owners-style-file")
122+
opts.outputPath, _ = cmd.Flags().GetString("output-path")
123+
124+
// Default the outputPath to the base path if no flag value is given
125+
if opts.outputPath == "" {
126+
opts.outputPath = opts.path
127+
}
128+
116129
opts.previousDays, _ = cmd.Flags().GetInt("range")
117130
opts.tty, _ = cmd.Flags().GetBool("tty-disable")
118131

@@ -139,6 +152,7 @@ pizza generate codeowners . --config /path/to/.sauced.yaml
139152

140153
cmd.PersistentFlags().IntP("range", "r", 90, "The number of days to analyze commit history (default 90)")
141154
cmd.PersistentFlags().Bool("owners-style-file", false, "Generate an agnostic OWNERS style file instead of CODEOWNERS.")
155+
cmd.PersistentFlags().StringP("output-path", "o", "", "Directory to create the output file.")
142156

143157
return cmd
144158
}
@@ -176,21 +190,23 @@ func run(opts *Options, cmd *cobra.Command) error {
176190
return fmt.Errorf("error traversing git log: %w", err)
177191
}
178192

179-
// Bootstrap codeowners
180-
var outputPath string
193+
// Define which file to generate based on a flag
194+
var fileType string
181195
if opts.ownersStyleFile {
182-
outputPath = filepath.Join(opts.path, "OWNERS")
196+
fileType = "OWNERS"
183197
} else {
184-
outputPath = filepath.Join(opts.path, "CODEOWNERS")
198+
fileType = "CODEOWNERS"
185199
}
186200

187-
opts.logger.V(logging.LogDebug).Style(0, colors.FgBlue).Infof("Processing codeowners file at: %s\n", outputPath)
188-
err = generateOutputFile(codeowners, outputPath, opts, cmd)
201+
opts.logger.V(logging.LogDebug).Style(0, colors.FgBlue).Infof("Processing codeowners file at: %s\n", opts.outputPath)
202+
203+
err = generateOutputFile(codeowners, filepath.Join(opts.outputPath, fileType), opts, cmd)
189204
if err != nil {
190205
_ = opts.telemetry.CaptureFailedCodeownersGenerate()
191206
return fmt.Errorf("error generating github style codeowners file: %w", err)
192207
}
193-
opts.logger.V(logging.LogInfo).Style(0, colors.FgGreen).Infof("Finished generating file: %s\n", outputPath)
208+
209+
opts.logger.V(logging.LogInfo).Style(0, colors.FgGreen).Infof("Finished generating file: %s\n", filepath.Join(opts.outputPath, fileType))
194210
_ = opts.telemetry.CaptureCodeownersGenerate()
195211

196212
opts.logger.V(logging.LogInfo).Style(0, colors.FgCyan).Infof("\nCreate an OpenSauced Contributor Insight to get metrics and insights on these codeowners:\n")

cmd/generate/codeowners/output.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ import (
1515
)
1616

1717
func generateOutputFile(fileStats FileStats, outputPath string, opts *Options, cmd *cobra.Command) error {
18+
19+
// Create specified output directories if necessary
20+
err := os.MkdirAll(filepath.Dir(outputPath), os.ModePerm)
21+
if err != nil {
22+
if !os.IsExist(err) {
23+
return fmt.Errorf("error creating directory at %s filepath: %w", outputPath, err)
24+
}
25+
}
26+
1827
// Open the file for writing
1928
file, err := os.Create(outputPath)
2029
if err != nil {

docs/pizza_generate_codeowners.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,19 @@ pizza generate codeowners . --owners-style-file
3838
3939
# Specify a custom location for the .sauced.yaml file
4040
pizza generate codeowners . --config /path/to/.sauced.yaml
41+
42+
# Specify a custom output location for the CODEOWNERS file
43+
pizza generate codeowners . --output-path /path/to/directory
4144
4245
```
4346

4447
### Options
4548

4649
```
4750
-h, --help help for codeowners
51+
-o, --output-path string Directory to create the output file.
4852
--owners-style-file Generate an agnostic OWNERS style file instead of CODEOWNERS.
49-
-r, --range int The number of days to analyze commit history (default 90) (default 90)
53+
-r, --range int The number of days to analyze commit history (default 90)
5054
```
5155

5256
### Options inherited from parent commands

0 commit comments

Comments
 (0)