Skip to content

Commit

Permalink
implement removing user from config based on name, only w specified c…
Browse files Browse the repository at this point in the history
…onfig path
  • Loading branch information
zeucapua committed Sep 24, 2024
1 parent 8d36d18 commit 74a6def
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
55 changes: 50 additions & 5 deletions cmd/offboard/offboard.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,82 @@
package offboard

import (
"fmt"
"errors"
"fmt"

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

"github.com/spf13/cobra"
)

type Options struct {
offboardingUsers []string

// config file path
configPath string

// CODEOWNERS file path
ownersPath string

// from global config
ttyDisabled bool
}

const configLongDesc string = `[WIP] Removes a user from the \".sauced.yaml\" config and \"CODEOWNERS\" files.
const offboardLongDesc string = `[WIP] Removes a user from the \".sauced.yaml\" config and \"CODEOWNERS\" files.
Requires the user's name OR email.`

func NewConfigCommand() *cobra.Command {
options := &Options{}
opts := &Options{}
cmd := &cobra.Command{
Use: "offboard <username/email> [flags]",
Short: "[WIP] Removes a user from the \".sauced.yaml\" config and \"CODEOWNERS\" files.",
Long: configLongDesc,
Long: offboardLongDesc,
Args: func(_ *cobra.Command, args []string) error {
if !(len(args) > 0) {
errors.New("you must provide at least one argument: the offboarding user's email/username")

Check failure on line 36 in cmd/offboard/offboard.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `errors.New` is not checked (errcheck)
}

options.offboardingUsers = args
opts.offboardingUsers = args

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {

Check failure on line 43 in cmd/offboard/offboard.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'args' seems to be unused, consider removing or renaming it as _ (revive)
opts.ttyDisabled, _ = cmd.Flags().GetBool("tty-disable")
opts.configPath, _ = cmd.Flags().GetString("config")

opts.ownersPath, _ = cmd.Flags().GetString("owners-path")
err := run(opts)
if err != nil {
return err
}
return nil
},
}

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

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

if err != nil {
return err
}

attributions := spec.Attributions
for _, user := range opts.offboardingUsers {
// deletes if the user is a name (key)
delete(attributions, user)
}

fmt.Print(attributions)

err = generateOutputFile(opts.configPath, attributions)
if err != nil {
return err
}

return nil
}
35 changes: 35 additions & 0 deletions cmd/offboard/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package offboard

import (
"fmt"
"os"

"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 {
file, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("error creating %s file: %w", outputPath, err)
}
defer file.Close()

var config config.Spec
config.Attributions = attributionMap

// for pretty print test
yaml, err := utils.OutputYAML(config)

if err != nil {
return fmt.Errorf("failed to turn into YAML: %w", err)
}

_, err = file.WriteString(yaml)

if err != nil {
return fmt.Errorf("failed to turn into YAML: %w", err)
}

return nil
}

0 comments on commit 74a6def

Please sign in to comment.