diff --git a/cmd/generate/codeowners/codeowners.go b/cmd/generate/codeowners/codeowners.go index 133db8f..701c61b 100644 --- a/cmd/generate/codeowners/codeowners.go +++ b/cmd/generate/codeowners/codeowners.go @@ -94,7 +94,7 @@ func NewCodeownersCommand() *cobra.Command { opts.loglevel = logging.LogDebug } - return run(opts) + return run(opts, cmd) }, } @@ -104,7 +104,7 @@ func NewCodeownersCommand() *cobra.Command { return cmd } -func run(opts *Options) error { +func run(opts *Options, cmd *cobra.Command) error { logger, err := gopherlogs.NewLogger( gopherlogs.WithLogVerbosity(opts.loglevel), gopherlogs.WithTty(!opts.tty), @@ -142,7 +142,7 @@ func run(opts *Options) error { } logger.V(logging.LogDebug).Style(0, colors.FgBlue).Infof("Processing codeowners file at: %s\n", outputPath) - err = generateOutputFile(codeowners, outputPath, opts.ownersStyleFile, opts.config) + err = generateOutputFile(codeowners, outputPath, opts, cmd) if err != nil { return fmt.Errorf("error generating github style codeowners file: %w", err) } diff --git a/cmd/generate/codeowners/output.go b/cmd/generate/codeowners/output.go index 6e58b71..0925498 100644 --- a/cmd/generate/codeowners/output.go +++ b/cmd/generate/codeowners/output.go @@ -3,23 +3,38 @@ package codeowners import ( "fmt" "os" + "path/filepath" "regexp" "sort" "strings" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "github.com/open-sauced/pizza-cli/pkg/config" ) -func generateOutputFile(fileStats FileStats, outputPath string, ownersStyleFile bool, config *config.Spec) error { +func generateOutputFile(fileStats FileStats, outputPath string, opts *Options, cmd *cobra.Command) error { // Open the file for writing file, err := os.Create(outputPath) if err != nil { return fmt.Errorf("error creating %s file: %w", outputPath, err) } defer file.Close() + var flags []string + + cmd.Flags().Visit(func(f *pflag.Flag) { + flags = append(flags, fmt.Sprintf("--%s %s", f.Name, f.Value.String())) + }) + generatedCommand := fmt.Sprintf("# $ pizza generate codeowners %s/", filepath.Base(opts.path)) + if len(flags) > 0 { + generatedCommand += " " + generatedCommand += strings.Join(flags, " ") + } // Write the header - _, err = file.WriteString("# This file is generated automatically by OpenSauced pizza-cli. DO NOT EDIT. Stay saucy!\n\n") + _, err = file.WriteString(fmt.Sprintf("# This file is generated automatically by OpenSauced pizza-cli. DO NOT EDIT. Stay saucy!\n#\n# Generated with command:\n%s\n\n", generatedCommand)) + if err != nil { return fmt.Errorf("error writing to %s file: %w", outputPath, err) } @@ -34,13 +49,13 @@ func generateOutputFile(fileStats FileStats, outputPath string, ownersStyleFile // Process each file for _, filename := range filenames { authorStats := fileStats[filename] - if ownersStyleFile { - err = writeOwnersChunk(authorStats, config, file, filename, outputPath) + if opts.ownersStyleFile { + err = writeOwnersChunk(authorStats, opts.config, file, filename, outputPath) if err != nil { return fmt.Errorf("error writing to %s file: %w", outputPath, err) } } else { - _, err := writeGitHubCodeownersChunk(authorStats, config, file, filename, outputPath) + _, err := writeGitHubCodeownersChunk(authorStats, opts.config, file, filename, outputPath) if err != nil { return fmt.Errorf("error writing to %s file: %w", outputPath, err) }