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())
|
||||
fs.SortFlags = false
|
||||
|
||||
options.AddFeatureToggle(cmd, options.DisableIncrementals())
|
||||
|
||||
c.Use = c.Use + " " + exchangeServiceCommandCreateUseSuffix
|
||||
c.Example = exchangeServiceCommandCreateExamples
|
||||
|
||||
@ -88,7 +86,8 @@ func addExchangeCommands(cmd *cobra.Command) *cobra.Command {
|
||||
utils.AddUserFlag(c)
|
||||
utils.AddDataFlag(c, []string{dataEmail, dataContacts, dataEvents}, false)
|
||||
options.AddFetchParallelismFlag(c)
|
||||
options.AddOperationFlags(c)
|
||||
options.AddFailFastFlag(c)
|
||||
options.AddDisableIncrementalsFlag(c)
|
||||
|
||||
case listCommand:
|
||||
c, fs = utils.AddCommand(cmd, exchangeListCmd())
|
||||
|
||||
@ -68,13 +68,11 @@ func addOneDriveCommands(cmd *cobra.Command) *cobra.Command {
|
||||
c, fs = utils.AddCommand(cmd, oneDriveCreateCmd())
|
||||
fs.SortFlags = false
|
||||
|
||||
options.AddFeatureToggle(cmd)
|
||||
|
||||
c.Use = c.Use + " " + oneDriveServiceCommandCreateUseSuffix
|
||||
c.Example = oneDriveServiceCommandCreateExamples
|
||||
|
||||
utils.AddUserFlag(c)
|
||||
options.AddOperationFlags(c)
|
||||
options.AddFailFastFlag(c)
|
||||
|
||||
case listCommand:
|
||||
c, fs = utils.AddCommand(cmd, oneDriveListCmd())
|
||||
|
||||
@ -83,8 +83,7 @@ func addSharePointCommands(cmd *cobra.Command) *cobra.Command {
|
||||
utils.AddSiteFlag(c)
|
||||
utils.AddSiteIDFlag(c)
|
||||
utils.AddDataFlag(c, []string{dataLibraries}, true)
|
||||
|
||||
options.AddOperationFlags(c)
|
||||
options.AddFailFastFlag(c)
|
||||
|
||||
case listCommand:
|
||||
c, fs = utils.AddCommand(cmd, sharePointListCmd())
|
||||
|
||||
@ -2,7 +2,6 @@ package options
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
"github.com/alcionai/corso/src/pkg/control"
|
||||
)
|
||||
@ -11,15 +10,15 @@ import (
|
||||
func Control() control.Options {
|
||||
opt := control.Defaults()
|
||||
|
||||
if fastFail {
|
||||
if failFastFV {
|
||||
opt.FailureHandling = control.FailFast
|
||||
}
|
||||
|
||||
opt.DisableMetrics = noStats
|
||||
opt.RestorePermissions = restorePermissions
|
||||
opt.SkipReduce = skipReduce
|
||||
opt.ToggleFeatures.DisableIncrementals = disableIncrementals
|
||||
opt.ItemFetchParallelism = fetchParallelism
|
||||
opt.DisableMetrics = noStatsFV
|
||||
opt.RestorePermissions = restorePermissionsFV
|
||||
opt.SkipReduce = skipReduceFV
|
||||
opt.ToggleFeatures.DisableIncrementals = disableIncrementalsFV
|
||||
opt.ItemFetchParallelism = fetchParallelismFV
|
||||
|
||||
return opt
|
||||
}
|
||||
@ -28,78 +27,78 @@ func Control() control.Options {
|
||||
// Operations Flags
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
var (
|
||||
fastFail bool
|
||||
noStats bool
|
||||
restorePermissions bool
|
||||
skipReduce bool
|
||||
fetchParallelism int
|
||||
const (
|
||||
failFastFN = "fail-fast"
|
||||
fetchParallelismFN = "fetch-parallelism"
|
||||
noStatsFN = "no-stats"
|
||||
restorePermissionsFN = "restore-permissions"
|
||||
skipReduceFN = "skip-reduce"
|
||||
)
|
||||
|
||||
// AddOperationFlags adds command-local operation flags
|
||||
func AddOperationFlags(cmd *cobra.Command) {
|
||||
fs := cmd.Flags()
|
||||
fs.BoolVar(&fastFail, "fast-fail", false, "stop processing immediately if any error occurs")
|
||||
// TODO: reveal this flag when fail-fast support is implemented
|
||||
cobra.CheckErr(fs.MarkHidden("fast-fail"))
|
||||
}
|
||||
var (
|
||||
failFastFV bool
|
||||
fetchParallelismFV int
|
||||
noStatsFV bool
|
||||
restorePermissionsFV bool
|
||||
skipReduceFV bool
|
||||
)
|
||||
|
||||
// AddGlobalOperationFlags adds the global operations flag set.
|
||||
func AddGlobalOperationFlags(cmd *cobra.Command) {
|
||||
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
|
||||
func AddRestorePermissionsFlag(cmd *cobra.Command) {
|
||||
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
|
||||
// reduction step. Currently only intended for details commands, not restore.
|
||||
func AddSkipReduceFlag(cmd *cobra.Command) {
|
||||
fs := cmd.Flags()
|
||||
fs.BoolVar(&skipReduce, "skip-reduce", false, "Skip the selector reduce filtering")
|
||||
cobra.CheckErr(fs.MarkHidden("skip-reduce"))
|
||||
fs.BoolVar(&skipReduceFV, skipReduceFN, false, "Skip the selector reduce filtering")
|
||||
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) {
|
||||
fs := cmd.Flags()
|
||||
fs.IntVar(
|
||||
&fetchParallelism,
|
||||
"fetch-parallelism",
|
||||
&fetchParallelismFV,
|
||||
fetchParallelismFN,
|
||||
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
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
var disableIncrementals bool
|
||||
const disableIncrementalsFN = "disable-incrementals"
|
||||
|
||||
type exposeFeatureFlag func(*pflag.FlagSet)
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
var disableIncrementalsFV bool
|
||||
|
||||
// Adds the hidden '--disable-incrementals' cli flag which, when set, disables
|
||||
// incremental backups.
|
||||
func DisableIncrementals() func(*pflag.FlagSet) {
|
||||
return func(fs *pflag.FlagSet) {
|
||||
fs.BoolVar(
|
||||
&disableIncrementals,
|
||||
"disable-incrementals",
|
||||
false,
|
||||
"Disable incremental data retrieval in backups.")
|
||||
cobra.CheckErr(fs.MarkHidden("disable-incrementals"))
|
||||
}
|
||||
func AddDisableIncrementalsFlag(cmd *cobra.Command) {
|
||||
fs := cmd.Flags()
|
||||
fs.BoolVar(
|
||||
&disableIncrementalsFV,
|
||||
disableIncrementalsFN,
|
||||
false,
|
||||
"Disable incremental data retrieval in backups.")
|
||||
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.AddExchangeDetailsAndRestoreFlags(c)
|
||||
|
||||
// others
|
||||
options.AddOperationFlags(c)
|
||||
options.AddFailFastFlag(c)
|
||||
}
|
||||
|
||||
return c
|
||||
|
||||
@ -38,9 +38,7 @@ func addOneDriveCommands(cmd *cobra.Command) *cobra.Command {
|
||||
|
||||
// restore permissions
|
||||
options.AddRestorePermissionsFlag(c)
|
||||
|
||||
// others
|
||||
options.AddOperationFlags(c)
|
||||
options.AddFailFastFlag(c)
|
||||
}
|
||||
|
||||
return c
|
||||
|
||||
@ -35,9 +35,7 @@ func addSharePointCommands(cmd *cobra.Command) *cobra.Command {
|
||||
|
||||
utils.AddBackupIDFlag(c, true)
|
||||
utils.AddSharePointDetailsAndRestoreFlags(c)
|
||||
|
||||
// others
|
||||
options.AddOperationFlags(c)
|
||||
options.AddFailFastFlag(c)
|
||||
}
|
||||
|
||||
return c
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user