From 74a6defe64b4cac538e9ba1c7f2e83c503354726 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Tue, 24 Sep 2024 11:35:36 -0700 Subject: [PATCH] implement removing user from config based on name, only w specified config path --- cmd/offboard/offboard.go | 55 ++++++++++++++++++++++++++++++++++++---- cmd/offboard/output.go | 35 +++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 cmd/offboard/output.go diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go index 4e0390a..834eab7 100644 --- a/cmd/offboard/offboard.go +++ b/cmd/offboard/offboard.go @@ -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 [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") } - options.offboardingUsers = args + opts.offboardingUsers = args return nil }, RunE: func(cmd *cobra.Command, args []string) error { + 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 +} diff --git a/cmd/offboard/output.go b/cmd/offboard/output.go new file mode 100644 index 0000000..5f092a9 --- /dev/null +++ b/cmd/offboard/output.go @@ -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 +}