Skip to content

Commit

Permalink
wip implement regenerating owners file
Browse files Browse the repository at this point in the history
  • Loading branch information
zeucapua committed Sep 25, 2024
1 parent 8660829 commit 80dbb31
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
29 changes: 20 additions & 9 deletions cmd/offboard/offboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Options struct {
// config file path
configPath string

// CODEOWNERS file path
ownersPath string
// repository path
path string

// from global config
ttyDisabled bool
Expand Down Expand Up @@ -45,7 +45,7 @@ func NewConfigCommand() *cobra.Command {
opts.ttyDisabled, _ = cmd.Flags().GetBool("tty-disable")
opts.configPath, _ = cmd.Flags().GetString("config")

opts.ownersPath, _ = cmd.Flags().GetString("owners-path")
opts.path, _ = cmd.Flags().GetString("path")
err := run(opts)
if err != nil {
return err
Expand All @@ -54,36 +54,47 @@ func NewConfigCommand() *cobra.Command {
},
}

cmd.PersistentFlags().StringP("owners-path", "o", "./CODEOWNERS", "the CODEOWNERS or OWNERS file to update")
cmd.PersistentFlags().StringP("path", "p", "./", "the path to the repository")
return cmd
}

func run(opts *Options) error {
// read config spec
spec, _, err := config.LoadConfig(opts.configPath)

if err != nil {
return err
return fmt.Errorf("error loading config: %v", err)
}

var offboardingNames []string
attributions := spec.Attributions
for _, user := range opts.offboardingUsers {
added := false

// deletes if the user is a name (key)
delete(attributions, user)

// delete if the user is an email (value)
for k, v := range attributions {
if slices.Contains(v, user) {
offboardingNames = append(offboardingNames, k)
delete(attributions, k)
added = true
}
}

if !added {
offboardingNames = append(offboardingNames, user)
}
}

fmt.Print(attributions)
err = generateConfigFile(opts.configPath, attributions)
if err != nil {
return fmt.Errorf("error generating config file: %v", err)
}

err = generateOutputFile(opts.configPath, attributions)
err = generateOwnersFile(opts.path, offboardingNames)
if err != nil {
return err
return fmt.Errorf("error generating owners file: %v", err)
}

return nil
Expand Down
49 changes: 48 additions & 1 deletion cmd/offboard/output.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package offboard

import (
"errors"
"fmt"
"os"
"strings"

"github.com/open-sauced/pizza-cli/v2/pkg/config"
"github.com/open-sauced/pizza-cli/v2/pkg/utils"
)

func generateOutputFile(outputPath string, attributionMap map[string][]string) error {
func generateConfigFile(outputPath string, attributionMap map[string][]string) error {
file, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("error creating %s file: %w", outputPath, err)
Expand All @@ -33,3 +35,48 @@ func generateOutputFile(outputPath string, attributionMap map[string][]string) e

return nil
}

func generateOwnersFile(path string, offboardingUsers []string) error {
outputType := "CODEOWNERS"
var owners []byte
var err error

if _, err = os.Stat(path + "/CODEOWNERS"); !errors.Is(err, os.ErrNotExist) {
fmt.Print("CODEOWNERS EXISTS")
outputType = "CODEOWNERS"
owners, err = os.ReadFile(path + "/CODEOWNERS")
} else if _, err = os.Stat(path + "/OWNERS"); !errors.Is(err, os.ErrNotExist) {
fmt.Print("OWNERS EXISTS")
outputType = "OWNERS"
owners, err = os.ReadFile(path + "/OWNERS")
}

if err != nil {
// fmt.Errorf("failed to find existing owners: %w", err)
fmt.Printf("WTF %v", err)
fmt.Printf("will create a new %s file in the path %s", outputType, path)
}

lines := strings.Split(string(owners), "\n")
for _, line := range lines {
for _, name := range offboardingUsers {
fmt.Println(name)
strings.Cut(line, name)
}
}

output := strings.Join(lines, "\n")
file, err := os.Create(path+"/"+outputType)

if err != nil {
return fmt.Errorf("error creating %s file: %w", outputType, err)
}
defer file.Close()

_, err = file.WriteString(output)
if err != nil {
return fmt.Errorf("failed writing file %s: %w", path+outputType, err)
}

return nil
}

0 comments on commit 80dbb31

Please sign in to comment.