## Description Adds a new package- Observe- for owning user- oriented displays like progress bars. This PR adds an initial progress bar to onedrive backups as a proof-of-concept. The API is more important than the specific progress bar package at this time. Future changes may opt for a different pkg. Display format currently looks like: ``` 59% [=============> ] (6.9/12 kB, 14 MB/s) | Item_Name.txt ``` Known Issues: * the `progressbar` package does not support multiline output, and [the author is not planning to add support](https://github.com/schollz/progressbar/issues/6). This causes concurrent items to overwrite each other. We will either need to fork the library, or change to a different one. ## Type of change - [x] 🌻 Feature ## Issue(s) * #1112 ## Test Plan - [x] 💪 Manual - [x] ⚡ Unit test
117 lines
3.2 KiB
Go
117 lines
3.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"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/internal/observe"
|
|
"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.SetUsageTemplate(indentExamplesTemplate(corsoCmd.UsageTemplate()))
|
|
|
|
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)
|
|
observe.SeedWriter(print.StderrWriter(ctx))
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
// Adjust the default usage template which does not properly indent examples
|
|
func indentExamplesTemplate(template string) string {
|
|
cobra.AddTemplateFunc("indent", func(spaces int, v string) string {
|
|
pad := strings.Repeat(" ", spaces)
|
|
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
|
|
})
|
|
|
|
e := regexp.MustCompile(`{{\.Example}}`)
|
|
|
|
return e.ReplaceAllString(template, "{{.Example | indent 2}}")
|
|
}
|