corso/src/cli/utils/export_config.go
Keepers 6159668b1d
utilize format validation check (#4288)
adds usage of the export format validation check
to export cli commands.  Also moves the restore
cfg validation check out of the service flags
validation checks and into the generic restore runner for better separation of concerns.

---

#### Does this PR need a docs update or release note?

- [x]  No

#### Type of change

- [x] 🌻 Feature

#### Issue(s)

* #3988 

#### Test Plan

- [x]  Unit test
- [x] 💚 E2E
2023-09-21 00:24:12 +00:00

65 lines
1.5 KiB
Go

package utils
import (
"context"
"strings"
"github.com/alcionai/clues"
"github.com/spf13/cobra"
"github.com/alcionai/corso/src/cli/flags"
"github.com/alcionai/corso/src/pkg/control"
"github.com/alcionai/corso/src/pkg/filters"
)
type ExportCfgOpts struct {
Archive bool
Format string
Populated flags.PopulatedFlags
}
func makeExportCfgOpts(cmd *cobra.Command) ExportCfgOpts {
return ExportCfgOpts{
Archive: flags.ArchiveFV,
Format: flags.FormatFV,
// populated contains the list of flags that appear in the
// command, according to pflags. Use this to differentiate
// between an "empty" and a "missing" value.
Populated: flags.GetPopulatedFlags(cmd),
}
}
func MakeExportConfig(
ctx context.Context,
opts ExportCfgOpts,
) control.ExportConfig {
exportCfg := control.DefaultExportConfig()
exportCfg.Archive = opts.Archive
exportCfg.Format = control.FormatType(opts.Format)
return exportCfg
}
// ValidateExportConfigFlags ensures all export config flags that utilize
// enumerated values match a well-known value.
func ValidateExportConfigFlags(opts *ExportCfgOpts) error {
acceptedFormatTypes := []string{
string(control.DefaultFormat),
string(control.JSONFormat),
}
if _, populated := opts.Populated[flags.FormatFN]; !populated {
opts.Format = string(control.DefaultFormat)
} else if !filters.Equal(acceptedFormatTypes).Compare(opts.Format) {
opts.Format = string(control.DefaultFormat)
return clues.New("unrecognized format type: " + opts.Format)
}
opts.Format = strings.ToLower(opts.Format)
return nil
}