From 8d36d187c29ee55a7c4996115b02db4e833c7a67 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Tue, 24 Sep 2024 10:40:09 -0700 Subject: [PATCH 01/15] init offboard command --- cmd/offboard/offboard.go | 37 +++++++++++++++++++++++++++++++++++++ cmd/root/root.go | 2 ++ 2 files changed, 39 insertions(+) create mode 100644 cmd/offboard/offboard.go diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go new file mode 100644 index 0000000..4e0390a --- /dev/null +++ b/cmd/offboard/offboard.go @@ -0,0 +1,37 @@ +package offboard + +import ( + "fmt" + "errors" + + "github.com/spf13/cobra" +) + +type Options struct { + offboardingUsers []string +} + +const configLongDesc 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{} + cmd := &cobra.Command{ + Use: "offboard [flags]", + Short: "[WIP] Removes a user from the \".sauced.yaml\" config and \"CODEOWNERS\" files.", + Long: configLongDesc, + 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 + + return nil + }, + RunE: func(cmd *cobra.Command, args []string) error { + return nil + }, + } + return cmd +} diff --git a/cmd/root/root.go b/cmd/root/root.go index d41a435..0cd054f 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -10,6 +10,7 @@ import ( "github.com/open-sauced/pizza-cli/v2/cmd/docs" "github.com/open-sauced/pizza-cli/v2/cmd/generate" "github.com/open-sauced/pizza-cli/v2/cmd/insights" + "github.com/open-sauced/pizza-cli/v2/cmd/offboard" "github.com/open-sauced/pizza-cli/v2/cmd/version" "github.com/open-sauced/pizza-cli/v2/pkg/constants" ) @@ -44,6 +45,7 @@ func NewRootCommand() (*cobra.Command, error) { cmd.AddCommand(generate.NewGenerateCommand()) cmd.AddCommand(insights.NewInsightsCommand()) cmd.AddCommand(version.NewVersionCommand()) + cmd.AddCommand(offboard.NewConfigCommand()) // The docs command is hidden as it's only used by the pizza-cli maintainers docsCmd := docs.NewDocsCommand() From 74a6defe64b4cac538e9ba1c7f2e83c503354726 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Tue, 24 Sep 2024 11:35:36 -0700 Subject: [PATCH 02/15] 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 +} From 8660829a5db494d30b290698d06f42b3b02124b6 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Tue, 24 Sep 2024 12:00:30 -0700 Subject: [PATCH 03/15] remove a user based on email from config --- cmd/offboard/offboard.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go index 834eab7..2272517 100644 --- a/cmd/offboard/offboard.go +++ b/cmd/offboard/offboard.go @@ -3,6 +3,7 @@ package offboard import ( "errors" "fmt" + "slices" "github.com/open-sauced/pizza-cli/v2/pkg/config" @@ -69,6 +70,13 @@ func run(opts *Options) error { for _, user := range opts.offboardingUsers { // 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) { + delete(attributions, k) + } + } } fmt.Print(attributions) From 80dbb31e87d232593f78132a6f7d07c0e4174561 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Wed, 25 Sep 2024 01:00:59 -0700 Subject: [PATCH 04/15] wip implement regenerating owners file --- cmd/offboard/offboard.go | 29 ++++++++++++++++-------- cmd/offboard/output.go | 49 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go index 2272517..ccb5fac 100644 --- a/cmd/offboard/offboard.go +++ b/cmd/offboard/offboard.go @@ -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 @@ -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 @@ -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 diff --git a/cmd/offboard/output.go b/cmd/offboard/output.go index 5f092a9..554978f 100644 --- a/cmd/offboard/output.go +++ b/cmd/offboard/output.go @@ -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) @@ -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 +} From db866b3b9b50a83e019ae43835383a6407108652 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Wed, 25 Sep 2024 10:13:00 -0700 Subject: [PATCH 05/15] implement remove by name --- cmd/offboard/output.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/cmd/offboard/output.go b/cmd/offboard/output.go index 554978f..f076838 100644 --- a/cmd/offboard/output.go +++ b/cmd/offboard/output.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "path/filepath" "strings" "github.com/open-sauced/pizza-cli/v2/pkg/config" @@ -37,37 +38,40 @@ func generateConfigFile(outputPath string, attributionMap map[string][]string) e } func generateOwnersFile(path string, offboardingUsers []string) error { - outputType := "CODEOWNERS" + outputType := "/CODEOWNERS" var owners []byte var err error - if _, err = os.Stat(path + "/CODEOWNERS"); !errors.Is(err, os.ErrNotExist) { + var ownersPath string + + if _, err = os.Stat(filepath.Join(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) { + ownersPath = filepath.Join(path, "/CODEOWNERS") + owners, err = os.ReadFile(ownersPath) + } else if _, err = os.Stat(filepath.Join(path, "OWNERS")); !errors.Is(err, os.ErrNotExist) { fmt.Print("OWNERS EXISTS") outputType = "OWNERS" - owners, err = os.ReadFile(path + "/OWNERS") + ownersPath = filepath.Join(path, "/OWNERS") + owners, err = os.ReadFile(ownersPath) } 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") + var newLines []string for _, line := range lines { + var result string for _, name := range offboardingUsers { - fmt.Println(name) - strings.Cut(line, name) + result, _, _ = strings.Cut(line, "@"+name) } + newLines = append(newLines, result) } - output := strings.Join(lines, "\n") - file, err := os.Create(path+"/"+outputType) - + output := strings.Join(newLines, "\n") + file, err := os.Create(ownersPath) if err != nil { return fmt.Errorf("error creating %s file: %w", outputType, err) } From 1661bad61b77dc84f2e6cd4b781937afac78163d Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Wed, 25 Sep 2024 15:38:08 -0700 Subject: [PATCH 06/15] remove users by email working --- cmd/offboard/offboard.go | 2 +- cmd/offboard/output.go | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go index ccb5fac..354a03b 100644 --- a/cmd/offboard/offboard.go +++ b/cmd/offboard/offboard.go @@ -34,7 +34,7 @@ func NewConfigCommand() *cobra.Command { 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") + return errors.New("you must provide at least one argument: the offboarding user's email/username") } opts.offboardingUsers = args diff --git a/cmd/offboard/output.go b/cmd/offboard/output.go index f076838..3563f98 100644 --- a/cmd/offboard/output.go +++ b/cmd/offboard/output.go @@ -63,11 +63,14 @@ func generateOwnersFile(path string, offboardingUsers []string) error { lines := strings.Split(string(owners), "\n") var newLines []string for _, line := range lines { - var result string + newLine := line for _, name := range offboardingUsers { - result, _, _ = strings.Cut(line, "@"+name) + result, _, found := strings.Cut(newLine, "@"+name) + if found { + newLine = result + } } - newLines = append(newLines, result) + newLines = append(newLines, newLine) } output := strings.Join(newLines, "\n") From bf119b9168ca520a894750f193d9ed4c13d9328b Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Wed, 25 Sep 2024 15:43:49 -0700 Subject: [PATCH 07/15] remove logging prints --- cmd/offboard/output.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmd/offboard/output.go b/cmd/offboard/output.go index 3563f98..f429fa0 100644 --- a/cmd/offboard/output.go +++ b/cmd/offboard/output.go @@ -45,12 +45,10 @@ func generateOwnersFile(path string, offboardingUsers []string) error { var ownersPath string if _, err = os.Stat(filepath.Join(path, "/CODEOWNERS")); !errors.Is(err, os.ErrNotExist) { - fmt.Print("CODEOWNERS EXISTS") outputType = "CODEOWNERS" ownersPath = filepath.Join(path, "/CODEOWNERS") owners, err = os.ReadFile(ownersPath) } else if _, err = os.Stat(filepath.Join(path, "OWNERS")); !errors.Is(err, os.ErrNotExist) { - fmt.Print("OWNERS EXISTS") outputType = "OWNERS" ownersPath = filepath.Join(path, "/OWNERS") owners, err = os.ReadFile(ownersPath) From 649be323f77d84d059637151cd2a6882ad01a31f Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Wed, 25 Sep 2024 15:58:04 -0700 Subject: [PATCH 08/15] rename args param --- cmd/offboard/offboard.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go index 354a03b..625dbb7 100644 --- a/cmd/offboard/offboard.go +++ b/cmd/offboard/offboard.go @@ -41,7 +41,7 @@ func NewConfigCommand() *cobra.Command { return nil }, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, _ []string) error { opts.ttyDisabled, _ = cmd.Flags().GetBool("tty-disable") opts.configPath, _ = cmd.Flags().GetString("config") From 34aed665e5f49bc6a88c123ce63eefa310cb843f Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Wed, 25 Sep 2024 16:02:36 -0700 Subject: [PATCH 09/15] gci lint? --- cmd/offboard/offboard.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go index 625dbb7..0f1e738 100644 --- a/cmd/offboard/offboard.go +++ b/cmd/offboard/offboard.go @@ -5,9 +5,9 @@ import ( "fmt" "slices" - "github.com/open-sauced/pizza-cli/v2/pkg/config" - "github.com/spf13/cobra" + + "github.com/open-sauced/pizza-cli/v2/pkg/config" ) type Options struct { From d3684bbccc162394d6f51026854b468f45bf95a3 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Thu, 3 Oct 2024 13:32:29 -0700 Subject: [PATCH 10/15] add posthog telemetry --- cmd/offboard/offboard.go | 19 +++++++++++++++---- pkg/utils/posthog.go | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go index 0f1e738..9ba3275 100644 --- a/cmd/offboard/offboard.go +++ b/cmd/offboard/offboard.go @@ -8,6 +8,8 @@ import ( "github.com/spf13/cobra" "github.com/open-sauced/pizza-cli/v2/pkg/config" + "github.com/open-sauced/pizza-cli/v2/pkg/constants" + "github.com/open-sauced/pizza-cli/v2/pkg/utils" ) type Options struct { @@ -21,6 +23,9 @@ type Options struct { // from global config ttyDisabled bool + + // telemetry for capturing CLI events via PostHog + telemetry *utils.PosthogCliClient } const offboardLongDesc string = `[WIP] Removes a user from the \".sauced.yaml\" config and \"CODEOWNERS\" files. @@ -44,13 +49,15 @@ func NewConfigCommand() *cobra.Command { RunE: func(cmd *cobra.Command, _ []string) error { opts.ttyDisabled, _ = cmd.Flags().GetBool("tty-disable") opts.configPath, _ = cmd.Flags().GetString("config") + disableTelem, _ := cmd.Flags().GetBool(constants.FlagNameTelemetry) + + opts.telemetry = utils.NewPosthogCliClient(!disableTelem) opts.path, _ = cmd.Flags().GetString("path") err := run(opts) - if err != nil { - return err - } - return nil + _ = opts.telemetry.Done() + + return err }, } @@ -62,6 +69,7 @@ func run(opts *Options) error { spec, _, err := config.LoadConfig(opts.configPath) if err != nil { + _ = opts.telemetry.CaptureFailedOffboard() return fmt.Errorf("error loading config: %v", err) } @@ -89,13 +97,16 @@ func run(opts *Options) error { err = generateConfigFile(opts.configPath, attributions) if err != nil { + _ = opts.telemetry.CaptureFailedOffboard() return fmt.Errorf("error generating config file: %v", err) } err = generateOwnersFile(opts.path, offboardingNames) if err != nil { + _ = opts.telemetry.CaptureFailedOffboard() return fmt.Errorf("error generating owners file: %v", err) } + _ = opts.telemetry.CaptureOffboard() return nil } diff --git a/pkg/utils/posthog.go b/pkg/utils/posthog.go index 9a9fb41..32503d8 100644 --- a/pkg/utils/posthog.go +++ b/pkg/utils/posthog.go @@ -198,6 +198,28 @@ func (p *PosthogCliClient) CaptureFailedConfigGenerate() error { return nil } +func (p *PosthogCliClient) CaptureOffboard() error { + if p.activated { + return p.client.Enqueue(posthog.Capture{ + DistinctId: p.uniqueID, + Event: "pizza_cli_offboard", + }) + } + + return nil +} + +func (p *PosthogCliClient) CaptureFailedOffboard() error { + if p.activated { + return p.client.Enqueue(posthog.Capture{ + DistinctId: p.uniqueID, + Event: "pizza_cli_failed_to_offboard", + }) + } + + return nil +} + // CaptureInsights gathers telemetry on successful Insights command runs func (p *PosthogCliClient) CaptureInsights() error { if p.activated { From bfaf37ffb584aac34cd8246b8acf0c897af7c8cf Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Thu, 3 Oct 2024 14:36:52 -0700 Subject: [PATCH 11/15] update offboard cmd description copy --- cmd/offboard/offboard.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go index 9ba3275..3b226b6 100644 --- a/cmd/offboard/offboard.go +++ b/cmd/offboard/offboard.go @@ -28,14 +28,14 @@ type Options struct { telemetry *utils.PosthogCliClient } -const offboardLongDesc string = `[WIP] Removes a user from the \".sauced.yaml\" config and \"CODEOWNERS\" files. -Requires the user's name OR email.` +const offboardLongDesc string = `CAUTION: Experimental Command. Removes users from the \".sauced.yaml\" config and \"CODEOWNERS\" files. +Requires the users' name OR email.` func NewConfigCommand() *cobra.Command { opts := &Options{} cmd := &cobra.Command{ Use: "offboard [flags]", - Short: "[WIP] Removes a user from the \".sauced.yaml\" config and \"CODEOWNERS\" files.", + Short: "CAUTION: Experimental Command. Removes users from the \".sauced.yaml\" config and \"CODEOWNERS\" files.", Long: offboardLongDesc, Args: func(_ *cobra.Command, args []string) error { if !(len(args) > 0) { From d19e8d1e9d847926491a61fbb06dd0123686b125 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Thu, 3 Oct 2024 15:12:49 -0700 Subject: [PATCH 12/15] make --path flag required with no default, check current path for config if flag not set --- cmd/offboard/offboard.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go index 3b226b6..1b6c410 100644 --- a/cmd/offboard/offboard.go +++ b/cmd/offboard/offboard.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "slices" + "strings" "github.com/spf13/cobra" @@ -61,12 +62,28 @@ func NewConfigCommand() *cobra.Command { }, } - cmd.PersistentFlags().StringP("path", "p", "./", "the path to the repository") + cmd.Flags().StringP("path", "p", "", "the path to the repository") + cmd.MarkFlagRequired("path") return cmd } func run(opts *Options) error { - spec, _, err := config.LoadConfig(opts.configPath) + var spec *config.Spec + var err error + if len(opts.configPath) != 0 { + fmt.Printf("IF != 0 %s", opts.configPath) + spec, _, err = config.LoadConfig(opts.configPath) + } else { + var dir string + if (strings.Compare(string(opts.path[len(opts.path)-1]), "/") == 0) { + dir = fmt.Sprintf("%s.sauced.yaml", opts.path) + } else { + dir = fmt.Sprintf("%s/.sauced.yaml", opts.path) + } + fmt.Printf("ELSE %s", dir) + spec, _, err = config.LoadConfig(dir) + } + if err != nil { _ = opts.telemetry.CaptureFailedOffboard() @@ -95,7 +112,13 @@ func run(opts *Options) error { } } - err = generateConfigFile(opts.configPath, attributions) + + if len(opts.configPath) != 0 { + err = generateConfigFile(opts.configPath, attributions) + } else { + err = generateConfigFile(fmt.Sprintf("%s/.sauced.yaml", opts.path), attributions) + } + if err != nil { _ = opts.telemetry.CaptureFailedOffboard() return fmt.Errorf("error generating config file: %v", err) From c18ca7227e37cd276ae8a1df45c1f242fe5d1067 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Thu, 3 Oct 2024 15:31:47 -0700 Subject: [PATCH 13/15] handle persistent flag error, gci --- cmd/offboard/offboard.go | 28 ++++++++++++++++------------ pkg/utils/posthog.go | 4 ++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go index 1b6c410..4e282f9 100644 --- a/cmd/offboard/offboard.go +++ b/cmd/offboard/offboard.go @@ -62,8 +62,10 @@ func NewConfigCommand() *cobra.Command { }, } - cmd.Flags().StringP("path", "p", "", "the path to the repository") - cmd.MarkFlagRequired("path") + cmd.PersistentFlags().StringP("path", "p", "", "the path to the repository (required)") + if err := cmd.MarkPersistentFlagRequired("path"); err != nil { + fmt.Printf("error MarkPersistentFlagRequired: %v", err) + } return cmd } @@ -71,20 +73,17 @@ func run(opts *Options) error { var spec *config.Spec var err error if len(opts.configPath) != 0 { - fmt.Printf("IF != 0 %s", opts.configPath) spec, _, err = config.LoadConfig(opts.configPath) } else { - var dir string - if (strings.Compare(string(opts.path[len(opts.path)-1]), "/") == 0) { - dir = fmt.Sprintf("%s.sauced.yaml", opts.path) + var configPath string + if strings.Compare(string(opts.path[len(opts.path)-1]), "/") == 0 { + configPath = opts.path + ".sauced.yaml" } else { - dir = fmt.Sprintf("%s/.sauced.yaml", opts.path) + configPath = opts.path + "/.sauced.yaml" } - fmt.Printf("ELSE %s", dir) - spec, _, err = config.LoadConfig(dir) + spec, _, err = config.LoadConfig(configPath) } - if err != nil { _ = opts.telemetry.CaptureFailedOffboard() return fmt.Errorf("error loading config: %v", err) @@ -112,11 +111,16 @@ func run(opts *Options) error { } } - if len(opts.configPath) != 0 { err = generateConfigFile(opts.configPath, attributions) } else { - err = generateConfigFile(fmt.Sprintf("%s/.sauced.yaml", opts.path), attributions) + var configPath string + if strings.Compare(string(opts.path[len(opts.path)-1]), "/") == 0 { + configPath = opts.path + ".sauced.yaml" + } else { + configPath = opts.path + "/.sauced.yaml" + } + err = generateConfigFile(configPath, attributions) } if err != nil { diff --git a/pkg/utils/posthog.go b/pkg/utils/posthog.go index 32503d8..10d66d1 100644 --- a/pkg/utils/posthog.go +++ b/pkg/utils/posthog.go @@ -202,7 +202,7 @@ func (p *PosthogCliClient) CaptureOffboard() error { if p.activated { return p.client.Enqueue(posthog.Capture{ DistinctId: p.uniqueID, - Event: "pizza_cli_offboard", + Event: "pizza_cli_offboard", }) } @@ -213,7 +213,7 @@ func (p *PosthogCliClient) CaptureFailedOffboard() error { if p.activated { return p.client.Enqueue(posthog.Capture{ DistinctId: p.uniqueID, - Event: "pizza_cli_failed_to_offboard", + Event: "pizza_cli_failed_to_offboard", }) } From 60c919a0e4e23077210592e2373368262816fc0d Mon Sep 17 00:00:00 2001 From: zeudev Date: Mon, 7 Oct 2024 09:08:31 -0700 Subject: [PATCH 14/15] Update cmd/offboard/offboard.go Co-authored-by: John McBride --- cmd/offboard/offboard.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go index 4e282f9..64c1f3c 100644 --- a/cmd/offboard/offboard.go +++ b/cmd/offboard/offboard.go @@ -39,7 +39,7 @@ func NewConfigCommand() *cobra.Command { Short: "CAUTION: Experimental Command. Removes users from the \".sauced.yaml\" config and \"CODEOWNERS\" files.", Long: offboardLongDesc, Args: func(_ *cobra.Command, args []string) error { - if !(len(args) > 0) { + if (len(args) <= 0) { return errors.New("you must provide at least one argument: the offboarding user's email/username") } From 5a60659bdfec594f7fb24d105435870268accfb8 Mon Sep 17 00:00:00 2001 From: Zeu Capua Date: Mon, 7 Oct 2024 13:02:42 -0700 Subject: [PATCH 15/15] gci lint --- cmd/offboard/offboard.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/offboard/offboard.go b/cmd/offboard/offboard.go index 64c1f3c..feabfd8 100644 --- a/cmd/offboard/offboard.go +++ b/cmd/offboard/offboard.go @@ -39,7 +39,7 @@ func NewConfigCommand() *cobra.Command { Short: "CAUTION: Experimental Command. Removes users from the \".sauced.yaml\" config and \"CODEOWNERS\" files.", Long: offboardLongDesc, Args: func(_ *cobra.Command, args []string) error { - if (len(args) <= 0) { + if len(args) == 0 { return errors.New("you must provide at least one argument: the offboarding user's email/username") }