corso/src/cli/cli.go
Georgi Matev 5e8b67a606
Explicit ordering of flags based on importance and use frequency (#1002)
## Description

Disable automatic flag sorting and explicitly order based on something more sensible. Fixes the order in both inline help output and docs. 

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [x] 🌻 Feature
- [ ] 🐛 Bugfix
- [x] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [ ] 🐹 Trivial/Minor

## Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* Fixes # 783

## Test Plan

<!-- How will this be tested prior to merging.-->
- [x] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
2022-09-30 19:01:39 +00:00

99 lines
2.6 KiB
Go

package cli
import (
"context"
"os"
"github.com/spf13/cobra"
"github.com/alcionai/corso/src/cli/backup"
"github.com/alcionai/corso/src/cli/config"
"github.com/alcionai/corso/src/cli/help"
"github.com/alcionai/corso/src/cli/options"
"github.com/alcionai/corso/src/cli/print"
"github.com/alcionai/corso/src/cli/repo"
"github.com/alcionai/corso/src/cli/restore"
"github.com/alcionai/corso/src/pkg/logger"
)
// ------------------------------------------------------------------------------------------
// Corso Command
// ------------------------------------------------------------------------------------------
// 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(cmd.Context(), "Corso\nversion:\tpre-alpha\n")
return nil
}
return cmd.Help()
}
// CorsoCommand produces a copy of the cobra command used by Corso.
// The command tree is built and attached to the returned command.
func CorsoCommand() *cobra.Command {
c := &cobra.Command{}
*c = *corsoCmd
BuildCommandTree(c)
return c
}
// BuildCommandTree builds out the command tree used by the Corso library.
func BuildCommandTree(cmd *cobra.Command) {
// want to order flags explicitly
cmd.PersistentFlags().SortFlags = false
cmd.Flags().BoolP("version", "v", version, "current version info")
cmd.PersistentPostRunE = config.InitFunc()
config.AddConfigFlags(cmd)
logger.AddLogLevelFlag(cmd)
print.AddOutputFlag(cmd)
options.AddGlobalOperationFlags(cmd)
cmd.CompletionOptions.DisableDefaultCmd = true
repo.AddCommands(cmd)
backup.AddCommands(cmd)
restore.AddCommands(cmd)
help.AddCommands(cmd)
}
// ------------------------------------------------------------------------------------------
// Running Corso
// ------------------------------------------------------------------------------------------
// Handle builds and executes the cli processor.
func Handle() {
ctx := config.Seed(context.Background())
ctx = print.SetRootCmd(ctx, corsoCmd)
BuildCommandTree(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)
}
}