options cli pkg cleanup (#2999)
Cleans up and adds unit tests for options flags. Removes variadic func builder pattern for single func versions. --- #### Does this PR need a docs update or release note? - [x] ⛔ No #### Type of change - [x] 🧹 Tech Debt/Cleanup #### Issue(s) * #2024 #### Test Plan - [x] ⚡ Unit test
This commit is contained in:
parent
57b143e358
commit
e4be050ac1
@ -78,8 +78,6 @@ func addExchangeCommands(cmd *cobra.Command) *cobra.Command {
|
|||||||
c, fs = utils.AddCommand(cmd, exchangeCreateCmd())
|
c, fs = utils.AddCommand(cmd, exchangeCreateCmd())
|
||||||
fs.SortFlags = false
|
fs.SortFlags = false
|
||||||
|
|
||||||
options.AddFeatureToggle(cmd, options.DisableIncrementals())
|
|
||||||
|
|
||||||
c.Use = c.Use + " " + exchangeServiceCommandCreateUseSuffix
|
c.Use = c.Use + " " + exchangeServiceCommandCreateUseSuffix
|
||||||
c.Example = exchangeServiceCommandCreateExamples
|
c.Example = exchangeServiceCommandCreateExamples
|
||||||
|
|
||||||
@ -88,7 +86,8 @@ func addExchangeCommands(cmd *cobra.Command) *cobra.Command {
|
|||||||
utils.AddUserFlag(c)
|
utils.AddUserFlag(c)
|
||||||
utils.AddDataFlag(c, []string{dataEmail, dataContacts, dataEvents}, false)
|
utils.AddDataFlag(c, []string{dataEmail, dataContacts, dataEvents}, false)
|
||||||
options.AddFetchParallelismFlag(c)
|
options.AddFetchParallelismFlag(c)
|
||||||
options.AddOperationFlags(c)
|
options.AddFailFastFlag(c)
|
||||||
|
options.AddDisableIncrementalsFlag(c)
|
||||||
|
|
||||||
case listCommand:
|
case listCommand:
|
||||||
c, fs = utils.AddCommand(cmd, exchangeListCmd())
|
c, fs = utils.AddCommand(cmd, exchangeListCmd())
|
||||||
|
|||||||
@ -68,13 +68,11 @@ func addOneDriveCommands(cmd *cobra.Command) *cobra.Command {
|
|||||||
c, fs = utils.AddCommand(cmd, oneDriveCreateCmd())
|
c, fs = utils.AddCommand(cmd, oneDriveCreateCmd())
|
||||||
fs.SortFlags = false
|
fs.SortFlags = false
|
||||||
|
|
||||||
options.AddFeatureToggle(cmd)
|
|
||||||
|
|
||||||
c.Use = c.Use + " " + oneDriveServiceCommandCreateUseSuffix
|
c.Use = c.Use + " " + oneDriveServiceCommandCreateUseSuffix
|
||||||
c.Example = oneDriveServiceCommandCreateExamples
|
c.Example = oneDriveServiceCommandCreateExamples
|
||||||
|
|
||||||
utils.AddUserFlag(c)
|
utils.AddUserFlag(c)
|
||||||
options.AddOperationFlags(c)
|
options.AddFailFastFlag(c)
|
||||||
|
|
||||||
case listCommand:
|
case listCommand:
|
||||||
c, fs = utils.AddCommand(cmd, oneDriveListCmd())
|
c, fs = utils.AddCommand(cmd, oneDriveListCmd())
|
||||||
|
|||||||
@ -83,8 +83,7 @@ func addSharePointCommands(cmd *cobra.Command) *cobra.Command {
|
|||||||
utils.AddSiteFlag(c)
|
utils.AddSiteFlag(c)
|
||||||
utils.AddSiteIDFlag(c)
|
utils.AddSiteIDFlag(c)
|
||||||
utils.AddDataFlag(c, []string{dataLibraries}, true)
|
utils.AddDataFlag(c, []string{dataLibraries}, true)
|
||||||
|
options.AddFailFastFlag(c)
|
||||||
options.AddOperationFlags(c)
|
|
||||||
|
|
||||||
case listCommand:
|
case listCommand:
|
||||||
c, fs = utils.AddCommand(cmd, sharePointListCmd())
|
c, fs = utils.AddCommand(cmd, sharePointListCmd())
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package options
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
|
||||||
|
|
||||||
"github.com/alcionai/corso/src/pkg/control"
|
"github.com/alcionai/corso/src/pkg/control"
|
||||||
)
|
)
|
||||||
@ -11,15 +10,15 @@ import (
|
|||||||
func Control() control.Options {
|
func Control() control.Options {
|
||||||
opt := control.Defaults()
|
opt := control.Defaults()
|
||||||
|
|
||||||
if fastFail {
|
if failFastFV {
|
||||||
opt.FailureHandling = control.FailFast
|
opt.FailureHandling = control.FailFast
|
||||||
}
|
}
|
||||||
|
|
||||||
opt.DisableMetrics = noStats
|
opt.DisableMetrics = noStatsFV
|
||||||
opt.RestorePermissions = restorePermissions
|
opt.RestorePermissions = restorePermissionsFV
|
||||||
opt.SkipReduce = skipReduce
|
opt.SkipReduce = skipReduceFV
|
||||||
opt.ToggleFeatures.DisableIncrementals = disableIncrementals
|
opt.ToggleFeatures.DisableIncrementals = disableIncrementalsFV
|
||||||
opt.ItemFetchParallelism = fetchParallelism
|
opt.ItemFetchParallelism = fetchParallelismFV
|
||||||
|
|
||||||
return opt
|
return opt
|
||||||
}
|
}
|
||||||
@ -28,78 +27,78 @@ func Control() control.Options {
|
|||||||
// Operations Flags
|
// Operations Flags
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
var (
|
const (
|
||||||
fastFail bool
|
failFastFN = "fail-fast"
|
||||||
noStats bool
|
fetchParallelismFN = "fetch-parallelism"
|
||||||
restorePermissions bool
|
noStatsFN = "no-stats"
|
||||||
skipReduce bool
|
restorePermissionsFN = "restore-permissions"
|
||||||
fetchParallelism int
|
skipReduceFN = "skip-reduce"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddOperationFlags adds command-local operation flags
|
var (
|
||||||
func AddOperationFlags(cmd *cobra.Command) {
|
failFastFV bool
|
||||||
fs := cmd.Flags()
|
fetchParallelismFV int
|
||||||
fs.BoolVar(&fastFail, "fast-fail", false, "stop processing immediately if any error occurs")
|
noStatsFV bool
|
||||||
// TODO: reveal this flag when fail-fast support is implemented
|
restorePermissionsFV bool
|
||||||
cobra.CheckErr(fs.MarkHidden("fast-fail"))
|
skipReduceFV bool
|
||||||
}
|
)
|
||||||
|
|
||||||
// AddGlobalOperationFlags adds the global operations flag set.
|
// AddGlobalOperationFlags adds the global operations flag set.
|
||||||
func AddGlobalOperationFlags(cmd *cobra.Command) {
|
func AddGlobalOperationFlags(cmd *cobra.Command) {
|
||||||
fs := cmd.PersistentFlags()
|
fs := cmd.PersistentFlags()
|
||||||
fs.BoolVar(&noStats, "no-stats", false, "disable anonymous usage statistics gathering")
|
fs.BoolVar(&noStatsFV, noStatsFN, false, "disable anonymous usage statistics gathering")
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddFailFastFlag adds a flag to toggle fail-fast error handling behavior.
|
||||||
|
func AddFailFastFlag(cmd *cobra.Command) {
|
||||||
|
fs := cmd.Flags()
|
||||||
|
fs.BoolVar(&failFastFV, failFastFN, false, "stop processing immediately if any error occurs")
|
||||||
|
// TODO: reveal this flag when fail-fast support is implemented
|
||||||
|
cobra.CheckErr(fs.MarkHidden(failFastFN))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddRestorePermissionsFlag adds OneDrive flag for restoring permissions
|
// AddRestorePermissionsFlag adds OneDrive flag for restoring permissions
|
||||||
func AddRestorePermissionsFlag(cmd *cobra.Command) {
|
func AddRestorePermissionsFlag(cmd *cobra.Command) {
|
||||||
fs := cmd.Flags()
|
fs := cmd.Flags()
|
||||||
fs.BoolVar(&restorePermissions, "restore-permissions", false, "Restore permissions for files and folders")
|
fs.BoolVar(&restorePermissionsFV, restorePermissionsFN, false, "Restore permissions for files and folders")
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddSkipReduceFlag adds a hidden flag that allows callers to skip the selector
|
// AddSkipReduceFlag adds a hidden flag that allows callers to skip the selector
|
||||||
// reduction step. Currently only intended for details commands, not restore.
|
// reduction step. Currently only intended for details commands, not restore.
|
||||||
func AddSkipReduceFlag(cmd *cobra.Command) {
|
func AddSkipReduceFlag(cmd *cobra.Command) {
|
||||||
fs := cmd.Flags()
|
fs := cmd.Flags()
|
||||||
fs.BoolVar(&skipReduce, "skip-reduce", false, "Skip the selector reduce filtering")
|
fs.BoolVar(&skipReduceFV, skipReduceFN, false, "Skip the selector reduce filtering")
|
||||||
cobra.CheckErr(fs.MarkHidden("skip-reduce"))
|
cobra.CheckErr(fs.MarkHidden(skipReduceFN))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddFetchParallelismFlag adds a hidden flag that allows callers to reduce call
|
||||||
|
// paralellism (ie, the corso worker pool size) from 4 to as low as 1.
|
||||||
func AddFetchParallelismFlag(cmd *cobra.Command) {
|
func AddFetchParallelismFlag(cmd *cobra.Command) {
|
||||||
fs := cmd.Flags()
|
fs := cmd.Flags()
|
||||||
fs.IntVar(
|
fs.IntVar(
|
||||||
&fetchParallelism,
|
&fetchParallelismFV,
|
||||||
"fetch-parallelism",
|
fetchParallelismFN,
|
||||||
4,
|
4,
|
||||||
"Control the number of concurrent data fetches for Exchange. Valid range is [1-4]. Default: 4")
|
"Control the number of concurrent data fetches for Exchange. Valid range is [1-4]. Default: 4")
|
||||||
cobra.CheckErr(fs.MarkHidden("fetch-parallelism"))
|
cobra.CheckErr(fs.MarkHidden(fetchParallelismFN))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Feature Flags
|
// Feature Flags
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
var disableIncrementals bool
|
const disableIncrementalsFN = "disable-incrementals"
|
||||||
|
|
||||||
type exposeFeatureFlag func(*pflag.FlagSet)
|
var disableIncrementalsFV bool
|
||||||
|
|
||||||
// AddFeatureToggle adds CLI flags for each exposed feature toggle to the
|
|
||||||
// persistent flag set within the command.
|
|
||||||
func AddFeatureToggle(cmd *cobra.Command, effs ...exposeFeatureFlag) {
|
|
||||||
fs := cmd.PersistentFlags()
|
|
||||||
for _, fflag := range effs {
|
|
||||||
fflag(fs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adds the hidden '--disable-incrementals' cli flag which, when set, disables
|
// Adds the hidden '--disable-incrementals' cli flag which, when set, disables
|
||||||
// incremental backups.
|
// incremental backups.
|
||||||
func DisableIncrementals() func(*pflag.FlagSet) {
|
func AddDisableIncrementalsFlag(cmd *cobra.Command) {
|
||||||
return func(fs *pflag.FlagSet) {
|
fs := cmd.Flags()
|
||||||
fs.BoolVar(
|
fs.BoolVar(
|
||||||
&disableIncrementals,
|
&disableIncrementalsFV,
|
||||||
"disable-incrementals",
|
disableIncrementalsFN,
|
||||||
false,
|
false,
|
||||||
"Disable incremental data retrieval in backups.")
|
"Disable incremental data retrieval in backups.")
|
||||||
cobra.CheckErr(fs.MarkHidden("disable-incrementals"))
|
cobra.CheckErr(fs.MarkHidden(disableIncrementalsFN))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
62
src/cli/options/options_test.go
Normal file
62
src/cli/options/options_test.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package options
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/alcionai/clues"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
|
||||||
|
"github.com/alcionai/corso/src/internal/tester"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OptionsUnitSuite struct {
|
||||||
|
tester.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOptionsUnitSuite(t *testing.T) {
|
||||||
|
suite.Run(t, &OptionsUnitSuite{Suite: tester.NewUnitSuite(t)})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *OptionsUnitSuite) TestAddExchangeCommands() {
|
||||||
|
t := suite.T()
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "test",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
assert.True(t, failFastFV, failFastFN)
|
||||||
|
assert.True(t, disableIncrementalsFV, disableIncrementalsFN)
|
||||||
|
assert.True(t, noStatsFV, noStatsFN)
|
||||||
|
assert.True(t, restorePermissionsFV, restorePermissionsFN)
|
||||||
|
assert.True(t, skipReduceFV, skipReduceFN)
|
||||||
|
assert.Equal(t, 2, fetchParallelismFV, fetchParallelismFN)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// adds no-stats
|
||||||
|
AddGlobalOperationFlags(cmd)
|
||||||
|
|
||||||
|
AddFailFastFlag(cmd)
|
||||||
|
AddDisableIncrementalsFlag(cmd)
|
||||||
|
AddRestorePermissionsFlag(cmd)
|
||||||
|
AddSkipReduceFlag(cmd)
|
||||||
|
|
||||||
|
AddFetchParallelismFlag(cmd)
|
||||||
|
|
||||||
|
// Test arg parsing for few args
|
||||||
|
cmd.SetArgs([]string{
|
||||||
|
"test",
|
||||||
|
"--" + failFastFN,
|
||||||
|
"--" + disableIncrementalsFN,
|
||||||
|
"--" + noStatsFN,
|
||||||
|
"--" + restorePermissionsFN,
|
||||||
|
"--" + skipReduceFN,
|
||||||
|
|
||||||
|
"--" + fetchParallelismFN, "2",
|
||||||
|
})
|
||||||
|
|
||||||
|
err := cmd.Execute()
|
||||||
|
require.NoError(t, err, clues.ToCore(err))
|
||||||
|
}
|
||||||
@ -36,9 +36,7 @@ func addExchangeCommands(cmd *cobra.Command) *cobra.Command {
|
|||||||
|
|
||||||
utils.AddBackupIDFlag(c, true)
|
utils.AddBackupIDFlag(c, true)
|
||||||
utils.AddExchangeDetailsAndRestoreFlags(c)
|
utils.AddExchangeDetailsAndRestoreFlags(c)
|
||||||
|
options.AddFailFastFlag(c)
|
||||||
// others
|
|
||||||
options.AddOperationFlags(c)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
|
|||||||
@ -38,9 +38,7 @@ func addOneDriveCommands(cmd *cobra.Command) *cobra.Command {
|
|||||||
|
|
||||||
// restore permissions
|
// restore permissions
|
||||||
options.AddRestorePermissionsFlag(c)
|
options.AddRestorePermissionsFlag(c)
|
||||||
|
options.AddFailFastFlag(c)
|
||||||
// others
|
|
||||||
options.AddOperationFlags(c)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
|
|||||||
@ -35,9 +35,7 @@ func addSharePointCommands(cmd *cobra.Command) *cobra.Command {
|
|||||||
|
|
||||||
utils.AddBackupIDFlag(c, true)
|
utils.AddBackupIDFlag(c, true)
|
||||||
utils.AddSharePointDetailsAndRestoreFlags(c)
|
utils.AddSharePointDetailsAndRestoreFlags(c)
|
||||||
|
options.AddFailFastFlag(c)
|
||||||
// others
|
|
||||||
options.AddOperationFlags(c)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user