validate --from-schedule flag

Signed-off-by: Ajay Sharma <ajaysharma.13122000@gmail.com>
This commit is contained in:
Ajay Sharma
2025-02-04 13:25:49 +00:00
parent 6ac38cde85
commit 3ca547f186
2 changed files with 59 additions and 0 deletions

View File

@@ -179,6 +179,11 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
return fmt.Errorf("either a 'selector' or an 'or-selector' can be specified, but not both")
}
// Ensure if FromSchedule is set, it has a non-empty value
if err := o.validateFromScheduleFlag(c); err != nil {
return err
}
// Ensure that unless FromSchedule is set, args contains a backup name
if o.FromSchedule == "" && len(args) != 1 {
return fmt.Errorf("a backup name is required, unless you are creating based on a schedule")
@@ -215,6 +220,23 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
return nil
}
func (o *CreateOptions) validateFromScheduleFlag(c *cobra.Command) error {
fromSchedule, err := c.Flags().GetString("from-schedule")
if err != nil {
return err
}
trimmed := strings.TrimSpace(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
}
func (o *CreateOptions) Complete(args []string, f client.Factory) error {
// If an explicit name is specified, use that name
if len(args) > 0 {

View File

@@ -24,6 +24,7 @@ import (
"testing"
"time"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
@@ -86,6 +87,42 @@ func TestCreateOptions_BuildBackup(t *testing.T) {
}, backup.Spec.OrderedResources)
}
func TestCreateOptions_ValidateFromScheduleFlag(t *testing.T) {
o := &CreateOptions{}
cmd := &cobra.Command{}
cmd.Flags().String("from-schedule", "", "Test from-schedule flag")
t.Run("from-schedule with empty or no value", 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())
})
t.Run("from-schedule with spaces only", func(t *testing.T) {
cmd.Flags().Set("from-schedule", " ")
err := o.validateFromScheduleFlag(cmd)
require.Error(t, err)
require.Equal(t, "flag must have a non-empty value: --from-schedule", err.Error())
})
t.Run("from-schedule with valid value", func(t *testing.T) {
cmd.Flags().Set("from-schedule", "daily")
err := o.validateFromScheduleFlag(cmd)
require.NoError(t, err)
require.Equal(t, "daily", o.FromSchedule)
})
t.Run("from-schedule with leading and trailing spaces", func(t *testing.T) {
cmd.Flags().Set("from-schedule", " daily ")
err := o.validateFromScheduleFlag(cmd)
require.NoError(t, err)
require.Equal(t, "daily", o.FromSchedule)
})
}
func TestCreateOptions_BuildBackupFromSchedule(t *testing.T) {
o := NewCreateOptions()
o.FromSchedule = "test"