Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update CODEOWNERS copy with command #130

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/generate/codeowners/codeowners.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func NewCodeownersCommand() *cobra.Command {
opts.loglevel = logging.LogDebug
}

return run(opts)
return run(opts, cmd)
},
}

Expand All @@ -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),
Expand Down Expand Up @@ -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)
}
Expand Down
24 changes: 18 additions & 6 deletions cmd/generate/codeowners/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@ import (
"strings"

"github.com/open-sauced/pizza-cli/pkg/config"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
zeucapua marked this conversation as resolved.
Show resolved Hide resolved

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
zeucapua marked this conversation as resolved.
Show resolved Hide resolved

cmd.Flags().Visit(func(f *pflag.Flag) {
flags = append(flags, fmt.Sprintf("--%s %v", f.Name, f.Value.String()))
})
generatedCommand := fmt.Sprintf("# $ pizza generate codeowners %s ", opts.path)
zeucapua marked this conversation as resolved.
Show resolved Hide resolved
if (len(flags) > 0) {
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)
}
Expand All @@ -34,20 +46,20 @@ 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)
}
}
}

return nil
return nil
zeucapua marked this conversation as resolved.
Show resolved Hide resolved
}

func writeGitHubCodeownersChunk(authorStats AuthorStats, config *config.Spec, file *os.File, srcFilename string, outputPath string) ([]string, error) {
Expand Down
Loading