corso/src/cli/cli.go
Keepers 342dd2e9f9
set up CLI for integration testing (#478)
Makes the necessary changes, including adding helper
funcs, to bring the CLI up to an integration-testable
state.  The changes made in this commit should be
sufficient for most other CLI tests.  Includes a single
test as verification.
2022-08-04 17:15:13 +00:00

66 lines
1.5 KiB
Go

package cli
import (
"context"
"os"
"github.com/spf13/cobra"
"github.com/alcionai/corso/cli/backup"
"github.com/alcionai/corso/cli/config"
"github.com/alcionai/corso/cli/print"
"github.com/alcionai/corso/cli/repo"
"github.com/alcionai/corso/cli/restore"
"github.com/alcionai/corso/pkg/logger"
)
// The root-level command.
// `corso <command> [<subcommand>] [<service>] [<flag>...]`
var corsoCmd = &cobra.Command{
Use: "corso",
Short: "Protect your Microsoft 365 data.",
Long: `Reliable, secure, and efficient data protection for Microsoft 365.`,
RunE: handleCorsoCmd,
PersistentPreRunE: config.InitFunc(),
}
// the root-level flags
var (
version bool
)
// Handler for flat calls to `corso`.
// Produces the same output as `corso --help`.
func handleCorsoCmd(cmd *cobra.Command, args []string) error {
if version {
print.Infof("Corso\nversion:\tpre-alpha\n")
return nil
}
return cmd.Help()
}
// Handle builds and executes the cli processor.
func Handle() {
ctx := config.Seed(context.Background())
corsoCmd.Flags().BoolP("version", "v", version, "current version info")
config.AddConfigFileFlag(corsoCmd)
print.SetRootCommand(corsoCmd)
print.AddOutputFlag(corsoCmd)
corsoCmd.CompletionOptions.DisableDefaultCmd = true
repo.AddCommands(corsoCmd)
backup.AddCommands(corsoCmd)
restore.AddCommands(corsoCmd)
ctx, log := logger.Seed(ctx)
defer func() {
_ = log.Sync() // flush all logs in the buffer
}()
if err := corsoCmd.ExecuteContext(ctx); err != nil {
os.Exit(1)
}
}