From 0949f9f82ac4e9fd18b0372c08bd710d6e5d5e17 Mon Sep 17 00:00:00 2001 From: Mateusz Hawrus <48822818+nieomylnieja@users.noreply.github.com> Date: Tue, 20 Aug 2024 17:32:41 +0200 Subject: [PATCH] fix: Add project flag back again for sloctl replay (#193) https://github.com/nobl9/sloctl/pull/177 removed default project flags, unfortunately we missed the `sloctl replay` command which also relied on them. This PR brings the `-p` flag back for `sloctl replay` command. --------- Co-authored-by: Tomek Labuk <89924840+labtom@users.noreply.github.com> --- internal/apply.go | 3 ++- internal/delete.go | 6 ++++-- internal/flags.go | 10 ---------- internal/get.go | 6 ++++-- internal/replay.go | 16 ++++++++++++---- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/internal/apply.go b/internal/apply.go index 946a11e..4132916 100644 --- a/internal/apply.go +++ b/internal/apply.go @@ -52,7 +52,8 @@ func (r *RootCmd) NewApplyCmd() *cobra.Command { registerFileFlag(cmd, true, &apply.definitionPaths) registerDryRunFlag(cmd, &apply.dryRun) registerAutoConfirmationFlag(cmd, &apply.autoConfirm) - registerProjectFlag(cmd, &apply.project) + cmd.Flags().StringVarP(&apply.project, "project", "p", "", + `Assigns the provided Project to the resources if no Project is defined in the object's definition.`) const ( replayFlagName = "replay" diff --git a/internal/delete.go b/internal/delete.go index 5cf234e..870ebbe 100644 --- a/internal/delete.go +++ b/internal/delete.go @@ -51,7 +51,8 @@ func (r *RootCmd) NewDeleteCmd() *cobra.Command { registerFileFlag(cmd, false, &deleteCmd.definitionPaths) registerDryRunFlag(cmd, &deleteCmd.dryRun) registerAutoConfirmationFlag(cmd, &deleteCmd.autoConfirm) - registerProjectFlag(cmd, &deleteCmd.project) + cmd.Flags().StringVarP(&deleteCmd.project, "project", "p", "", + `Assigns the provided Project to the resources if no Project is defined in the object's definition.`) // register all subcommands for delete for _, def := range []struct { @@ -129,7 +130,8 @@ func newSubcommand( }, } if objectKindSupportsProjectFlag(kind) { - registerProjectFlag(sc, &deleteCmd.project) + sc.Flags().StringVarP(&deleteCmd.project, "project", "p", "", + `Specifies the Project from which to delete the resources. If not provided, the default Project will be used.`) } registerDryRunFlag(sc, &deleteCmd.dryRun) return sc diff --git a/internal/flags.go b/internal/flags.go index c76310a..f0611a4 100644 --- a/internal/flags.go +++ b/internal/flags.go @@ -77,16 +77,6 @@ func objectKindSupportsProjectFlag(kind manifest.Kind) bool { return ok } -func registerProjectFlag(cmd *cobra.Command, storeIn *string) { - cmd.Flags().StringVarP(storeIn, "project", "p", "", - `List the requested object(s) which belong to the specified Project (name).`) -} - -func registerAllProjectsFlag(cmd *cobra.Command, storeIn *bool) { - cmd.Flags().BoolVarP(storeIn, "all-projects", "A", false, - `List the requested object(s) across all projects.`) -} - var labelSupportingKinds = map[manifest.Kind]struct{}{ manifest.KindProject: {}, manifest.KindService: {}, diff --git a/internal/get.go b/internal/get.go index 59ae34d..2d97e72 100644 --- a/internal/get.go +++ b/internal/get.go @@ -98,8 +98,10 @@ To get more details in output use one of the available flags.`, subCmd.Extender(sc) } if objectKindSupportsProjectFlag(subCmd.Kind) { - registerProjectFlag(sc, &get.project) - registerAllProjectsFlag(sc, &get.allProjects) + sc.Flags().StringVarP(&get.project, "project", "p", "", + `List the requested object(s) which belong to the specified Project (name).`) + sc.Flags().BoolVarP(&get.allProjects, "all-projects", "A", false, + `List the requested object(s) across all projects.`) } if objectKindSupportsLabelsFlag(subCmd.Kind) { registerLabelsFlag(sc, &get.labels) diff --git a/internal/replay.go b/internal/replay.go index 50a09b3..ff75d14 100644 --- a/internal/replay.go +++ b/internal/replay.go @@ -33,6 +33,7 @@ type ReplayCmd struct { from TimeValue configPaths []string sloName string + project string } //go:embed replay_example.sh @@ -54,13 +55,20 @@ func (r *RootCmd) NewReplayCmd() *cobra.Command { "Importing data takes time: Replay for a single SLO may take several minutes up to an hour. " + "During that time, the command keeps on running, periodically checking the status of Replay. " + "If you cancel the program execution at any time, the current Replay in progress will not be revoked.", - Example: replayExample, - Args: replay.arguments, - PersistentPreRun: func(cmd *cobra.Command, args []string) { replay.client = r.GetClient() }, - RunE: func(cmd *cobra.Command, args []string) error { return replay.Run(cmd) }, + Example: replayExample, + Args: replay.arguments, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + replay.client = r.GetClient() + if replay.project != "" { + replay.client.Config.Project = replay.project + } + }, + RunE: func(cmd *cobra.Command, args []string) error { return replay.Run(cmd) }, } registerFileFlag(cmd, false, &replay.configPaths) + cmd.Flags().StringVarP(&replay.project, "project", "p", "", + `Specifies the Project for the SLOs you want to Replay.`) cmd.Flags().Var(&replay.from, "from", "Sets the start of Replay time window.") return cmd