Final Examples indent fix to be consistent in docs and inline usage (#1056)

## Description

Previous indent fixed usage but screwed up generated docs. 

## Type of change

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

## Issue(s)


## Test Plan

<!-- How will this be tested prior to merging.-->
- [x] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
This commit is contained in:
Georgi Matev 2022-10-05 13:33:47 -07:00 committed by GitHub
parent 5bcdaef769
commit 010d8d5df1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 25 deletions

View File

@ -105,7 +105,7 @@ func addExchangeCommands(parent *cobra.Command) *cobra.Command {
c, fs = utils.AddCommand(parent, exchangeCreateCmd()) c, fs = utils.AddCommand(parent, exchangeCreateCmd())
c.Use = c.Use + exchangeServiceCommandCreateUseSuffix c.Use = c.Use + exchangeServiceCommandCreateUseSuffix
c.Example = utils.IndentExamples(exchangeServiceCommandCreateExamples) c.Example = exchangeServiceCommandCreateExamples
// Flags addition ordering should follow the order we want them to appear in help and docs: // Flags addition ordering should follow the order we want them to appear in help and docs:
// More generic (ex: --all) and more frequently used flags take precedence. // More generic (ex: --all) and more frequently used flags take precedence.
@ -129,7 +129,7 @@ func addExchangeCommands(parent *cobra.Command) *cobra.Command {
c, fs = utils.AddCommand(parent, exchangeDetailsCmd()) c, fs = utils.AddCommand(parent, exchangeDetailsCmd())
c.Use = c.Use + exchangeServiceCommandDetailsUseSuffix c.Use = c.Use + exchangeServiceCommandDetailsUseSuffix
c.Example = utils.IndentExamples(exchangeServiceCommandDetailsExamples) c.Example = exchangeServiceCommandDetailsExamples
// Flags addition ordering should follow the order we want them to appear in help and docs: // Flags addition ordering should follow the order we want them to appear in help and docs:
// More generic (ex: --all) and more frequently used flags take precedence. // More generic (ex: --all) and more frequently used flags take precedence.
@ -217,7 +217,7 @@ func addExchangeCommands(parent *cobra.Command) *cobra.Command {
c, fs = utils.AddCommand(parent, exchangeDeleteCmd()) c, fs = utils.AddCommand(parent, exchangeDeleteCmd())
c.Use = c.Use + exchangeServiceCommandDeleteUseSuffix c.Use = c.Use + exchangeServiceCommandDeleteUseSuffix
c.Example = utils.IndentExamples(exchangeServiceCommandDeleteExamples) c.Example = exchangeServiceCommandDeleteExamples
fs.StringVar(&backupID, "backup", "", "ID of the backup to delete. (required)") fs.StringVar(&backupID, "backup", "", "ID of the backup to delete. (required)")
cobra.CheckErr(c.MarkFlagRequired("backup")) cobra.CheckErr(c.MarkFlagRequired("backup"))

View File

@ -77,7 +77,7 @@ func addOneDriveCommands(parent *cobra.Command) *cobra.Command {
c, fs = utils.AddCommand(parent, oneDriveCreateCmd()) c, fs = utils.AddCommand(parent, oneDriveCreateCmd())
c.Use = c.Use + oneDriveServiceCommandCreateUseSuffix c.Use = c.Use + oneDriveServiceCommandCreateUseSuffix
c.Example = utils.IndentExamples(oneDriveServiceCommandCreateExamples) c.Example = oneDriveServiceCommandCreateExamples
fs.StringArrayVar(&user, "user", nil, fs.StringArrayVar(&user, "user", nil,
"Backup OneDrive data by user ID; accepts '"+utils.Wildcard+"' to select all users. (required)") "Backup OneDrive data by user ID; accepts '"+utils.Wildcard+"' to select all users. (required)")
@ -90,7 +90,7 @@ func addOneDriveCommands(parent *cobra.Command) *cobra.Command {
c, fs = utils.AddCommand(parent, oneDriveDetailsCmd()) c, fs = utils.AddCommand(parent, oneDriveDetailsCmd())
c.Use = c.Use + oneDriveServiceCommandDetailsUseSuffix c.Use = c.Use + oneDriveServiceCommandDetailsUseSuffix
c.Example = utils.IndentExamples(oneDriveServiceCommandDetailsExamples) c.Example = oneDriveServiceCommandDetailsExamples
fs.StringVar(&backupID, "backup", "", "ID of the backup to explore. (required)") fs.StringVar(&backupID, "backup", "", "ID of the backup to explore. (required)")
cobra.CheckErr(c.MarkFlagRequired("backup")) cobra.CheckErr(c.MarkFlagRequired("backup"))
@ -131,7 +131,7 @@ func addOneDriveCommands(parent *cobra.Command) *cobra.Command {
c, fs = utils.AddCommand(parent, oneDriveDeleteCmd()) c, fs = utils.AddCommand(parent, oneDriveDeleteCmd())
c.Use = c.Use + oneDriveServiceCommandDeleteUseSuffix c.Use = c.Use + oneDriveServiceCommandDeleteUseSuffix
c.Example = utils.IndentExamples(oneDriveServiceCommandDeleteExamples) c.Example = oneDriveServiceCommandDeleteExamples
fs.StringVar(&backupID, "backup", "", "ID of the backup to delete. (required)") fs.StringVar(&backupID, "backup", "", "ID of the backup to delete. (required)")
cobra.CheckErr(c.MarkFlagRequired("backup")) cobra.CheckErr(c.MarkFlagRequired("backup"))

View File

@ -3,6 +3,8 @@ package cli
import ( import (
"context" "context"
"os" "os"
"regexp"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -68,6 +70,8 @@ func BuildCommandTree(cmd *cobra.Command) {
print.AddOutputFlag(cmd) print.AddOutputFlag(cmd)
options.AddGlobalOperationFlags(cmd) options.AddGlobalOperationFlags(cmd)
cmd.SetUsageTemplate(indentExamplesTemplate(corsoCmd.UsageTemplate()))
cmd.CompletionOptions.DisableDefaultCmd = true cmd.CompletionOptions.DisableDefaultCmd = true
repo.AddCommands(cmd) repo.AddCommands(cmd)
@ -96,3 +100,15 @@ func Handle() {
os.Exit(1) 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}}")
}

View File

@ -38,6 +38,7 @@ func addS3Commands(parent *cobra.Command) *cobra.Command {
} }
c.Use = c.Use + s3ProviderCommandUseSuffix c.Use = c.Use + s3ProviderCommandUseSuffix
c.SetUsageTemplate(parent.UsageTemplate())
// Flags addition ordering should follow the order we want them to appear in help and docs: // Flags addition ordering should follow the order we want them to appear in help and docs:
// More generic (ex: --all) and more frequently used flags take precedence. // More generic (ex: --all) and more frequently used flags take precedence.
@ -90,7 +91,7 @@ func s3InitCmd() *cobra.Command {
Long: `Bootstraps a new S3 repository and connects it to your m356 account.`, Long: `Bootstraps a new S3 repository and connects it to your m356 account.`,
RunE: initS3Cmd, RunE: initS3Cmd,
Args: cobra.NoArgs, Args: cobra.NoArgs,
Example: utils.IndentExamples(s3ProviderCommandInitExamples), Example: s3ProviderCommandInitExamples,
} }
} }

View File

@ -164,7 +164,7 @@ func exchangeRestoreCmd() *cobra.Command {
Short: "Restore M365 Exchange service data", Short: "Restore M365 Exchange service data",
RunE: restoreExchangeCmd, RunE: restoreExchangeCmd,
Args: cobra.NoArgs, Args: cobra.NoArgs,
Example: utils.IndentExamples(exchangeServiceCommandRestoreExamples), Example: exchangeServiceCommandRestoreExamples,
} }
} }

View File

@ -111,7 +111,7 @@ func oneDriveRestoreCmd() *cobra.Command {
Short: "Restore M365 OneDrive service data", Short: "Restore M365 OneDrive service data",
RunE: restoreOneDriveCmd, RunE: restoreOneDriveCmd,
Args: cobra.NoArgs, Args: cobra.NoArgs,
Example: utils.IndentExamples(oneDriveServiceCommandRestoreExamples), Example: oneDriveServiceCommandRestoreExamples,
} }
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"regexp"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -58,12 +57,3 @@ func AddCommand(parent, c *cobra.Command) (*cobra.Command, *pflag.FlagSet) {
return c, c.Flags() return c, c.Flags()
} }
// Takes in a multi-line string and returns it indented by 2 spaces.
// This is only to be used with Examples strings which the default usage
// template does not properly indent to match other sections
func IndentExamples(examples string) string {
e := regexp.MustCompile(`(?m)^`)
return e.ReplaceAllString(examples, " ")
}