Skip to content

Commit

Permalink
feat: add logic to handle output path flag for generating codeowners …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
udaypatel1 committed Oct 16, 2024
1 parent 999f84d commit 09ed80b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
30 changes: 23 additions & 7 deletions cmd/generate/codeowners/codeowners.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Options struct {
// The default should be to generate a GitHub style "CODEOWNERS" file.
ownersStyleFile bool

// where the output file will go
outputPath string

// the number of days to look back
previousDays int

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

opts.ownersStyleFile, _ = cmd.Flags().GetBool("owners-style-file")
opts.outputPath, _ = cmd.Flags().GetString("output-path")

// Default the outputPath to the base path if no flag value is given
if opts.outputPath == "" {
opts.outputPath = opts.path
}

opts.previousDays, _ = cmd.Flags().GetInt("range")
opts.tty, _ = cmd.Flags().GetBool("tty-disable")

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

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

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

// Bootstrap codeowners
var outputPath string
// Define which file to generate based on a flag
var fileType string
if opts.ownersStyleFile {
outputPath = filepath.Join(opts.path, "OWNERS")
fileType = "OWNERS"
} else {
outputPath = filepath.Join(opts.path, "CODEOWNERS")
fileType = "CODEOWNERS"
}

opts.logger.V(logging.LogDebug).Style(0, colors.FgBlue).Infof("Processing codeowners file at: %s\n", outputPath)
err = generateOutputFile(codeowners, outputPath, opts, cmd)
opts.logger.V(logging.LogDebug).Style(0, colors.FgBlue).Infof("Processing codeowners file at: %s\n", opts.outputPath)

err = generateOutputFile(codeowners, filepath.Join(opts.outputPath, fileType), opts, cmd)
if err != nil {
_ = opts.telemetry.CaptureFailedCodeownersGenerate()
return fmt.Errorf("error generating github style codeowners file: %w", err)
}
opts.logger.V(logging.LogInfo).Style(0, colors.FgGreen).Infof("Finished generating file: %s\n", outputPath)

opts.logger.V(logging.LogInfo).Style(0, colors.FgGreen).Infof("Finished generating file: %s\n", filepath.Join(opts.outputPath, fileType))
_ = opts.telemetry.CaptureCodeownersGenerate()

opts.logger.V(logging.LogInfo).Style(0, colors.FgCyan).Infof("\nCreate an OpenSauced Contributor Insight to get metrics and insights on these codeowners:\n")
Expand Down
9 changes: 9 additions & 0 deletions cmd/generate/codeowners/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ import (
)

func generateOutputFile(fileStats FileStats, outputPath string, opts *Options, cmd *cobra.Command) error {

// Create specified output directories if necessary
err := os.MkdirAll(filepath.Dir(outputPath), os.ModePerm)
if err != nil {
if !os.IsExist(err) {
return fmt.Errorf("error creating directory at %s filepath: %w", outputPath, err)
}
}

// Open the file for writing
file, err := os.Create(outputPath)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion docs/pizza_generate_codeowners.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,19 @@ pizza generate codeowners . --owners-style-file
# Specify a custom location for the .sauced.yaml file
pizza generate codeowners . --config /path/to/.sauced.yaml
# Specify a custom output location for the CODEOWNERS file
pizza generate codeowners . --output-path /path/to/directory
```

### Options

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

### Options inherited from parent commands
Expand Down

0 comments on commit 09ed80b

Please sign in to comment.