refactor code

Signed-off-by: Ajay Sharma <ajaysharma.13122000@gmail.com>
This commit is contained in:
Ajay Sharma
2025-02-07 14:55:43 +00:00
parent e9bd9f3c8d
commit 06fc9da925
2 changed files with 4 additions and 10 deletions

View File

@@ -221,19 +221,13 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
}
func (o *CreateOptions) validateFromScheduleFlag(c *cobra.Command) error {
fromSchedule, err := c.Flags().GetString("from-schedule")
if err != nil {
return err
}
trimmed := strings.TrimSpace(fromSchedule)
trimmed := strings.TrimSpace(o.FromSchedule)
if c.Flags().Changed("from-schedule") && trimmed == "" {
return fmt.Errorf("flag must have a non-empty value: --from-schedule")
}
// Assign the trimmed value back
o.FromSchedule = trimmed
return nil
}

View File

@@ -88,10 +88,9 @@ func TestCreateOptions_BuildBackup(t *testing.T) {
}
func TestCreateOptions_ValidateFromScheduleFlag(t *testing.T) {
o := &CreateOptions{}
cmd := &cobra.Command{}
cmd.Flags().String("from-schedule", "", "Test from-schedule flag")
o := NewCreateOptions()
o.BindFromSchedule(cmd.Flags())
t.Run("from-schedule with empty or no value", func(t *testing.T) {
cmd.Flags().Set("from-schedule", "")
@@ -104,6 +103,7 @@ func TestCreateOptions_ValidateFromScheduleFlag(t *testing.T) {
t.Run("from-schedule with spaces only", func(t *testing.T) {
cmd.Flags().Set("from-schedule", " ")
err := o.validateFromScheduleFlag(cmd)
require.True(t, cmd.Flags().Changed("from-schedule"))
require.Error(t, err)
require.Equal(t, "flag must have a non-empty value: --from-schedule", err.Error())
})