-
-
Notifications
You must be signed in to change notification settings - Fork 118
Standardize Help and Usage content for all commands and sub commands #914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aknysh
merged 78 commits into
main
from
feature/dev-2896-incorrect-output-for-atmos-validate
Jan 18, 2025
Merged
Changes from all commits
Commits
Show all changes
78 commits
Select commit
Hold shift + click to select a range
f0b7b98
Merge atmos specific and terraform help documentation
samtholiya 922ba49
Merge branch 'main' into feature/dev-2821-atmos-terraform-help-should…
samtholiya 437e1d4
Add help for terraform
samtholiya 87fd2fd
Updated help content
samtholiya d7cabbd
Merge branch 'main' of https://github.com/cloudposse/atmos into featu…
samtholiya 95304aa
test fix for auto approve
samtholiya 0f3f84e
Update message for invalid command
samtholiya c719d86
remove old help code
samtholiya 891e5d2
Merge branch 'main' into feature/dev-2821-atmos-terraform-help-should…
samtholiya fba6986
testing if auto-approve should be with two -
samtholiya 48fee23
Merge branch 'feature/dev-2821-atmos-terraform-help-should-also-show-…
samtholiya 636fb16
Merge branch 'main' into feature/dev-2821-atmos-terraform-help-should…
samtholiya 51bbb79
check error after executing old usage func
samtholiya 7765e3f
Merge branch 'feature/dev-2821-atmos-terraform-help-should-also-show-…
samtholiya d0351a8
Merge branch 'main' into feature/dev-2821-atmos-terraform-help-should…
samtholiya 0491db4
Merge branch 'main' into feature/dev-2821-atmos-terraform-help-should…
samtholiya ce5bef1
rebase help.go
samtholiya 1d8fe0e
Fix terraform subcommand help
samtholiya a8d6345
Remove unwanted setHelpFunc
samtholiya ed8b9f8
terraform,helmfile empty sub command should redirect to help
samtholiya 18a6d96
check errors for usage and help
samtholiya 40b4dd3
Added space in between Native commands
samtholiya 80c4b8a
Removed unwanted code
samtholiya 6823379
Add consistent usage and help for all commands
samtholiya ea2732f
Removed unwanted help check
samtholiya 9184a73
Merge branch 'main' into feature/dev-2821-atmos-terraform-help-should…
samtholiya 5766d45
Fix atmos help
samtholiya 18aaa77
Help should be in stdout terminal
samtholiya 58cbac8
Merge branch 'main' into feature/dev-2821-atmos-terraform-help-should…
samtholiya b920974
fix terraform args
samtholiya 22923fc
Merge branch 'feature/dev-2821-atmos-terraform-help-should-also-show-…
samtholiya 7b56200
fixed terraform command
samtholiya c20c5a2
Merge branch 'main' of https://github.com/cloudposse/atmos into featu…
samtholiya 33bd593
fix helm command
samtholiya 08ab3de
Merge branch 'main' of https://github.com/cloudposse/atmos into featu…
samtholiya 969d988
rearrange the help strings
samtholiya 1d88c3b
Merge branch 'main' into feature/dev-2896-incorrect-output-for-atmos-…
samtholiya e22e144
simplified the code
samtholiya a52e95a
Merge branch 'feature/dev-2896-incorrect-output-for-atmos-validate' o…
samtholiya 9814e03
Updated version with the help and usage as per expectations
samtholiya 94d2057
Merge branch 'main' into feature/dev-2896-incorrect-output-for-atmos-…
samtholiya d1dc47c
fix atmos command usage
samtholiya 2bf3eec
Merge branch 'feature/dev-2896-incorrect-output-for-atmos-validate' o…
samtholiya 70a92c8
Merge branch 'main' into feature/dev-2896-incorrect-output-for-atmos-…
samtholiya c1153f1
Fix atmos usage error assertion
samtholiya db89d5d
Merge branch 'feature/dev-2896-incorrect-output-for-atmos-validate' o…
samtholiya e6f9ba8
Add SubCommand Aliases:
samtholiya 35c8182
Updated template to be more dynamic
samtholiya 55c1362
use cobra.NoArgs instead
samtholiya 6a893e9
fix atmos version command
samtholiya 24e9ac4
Merge branch 'main' into feature/dev-2896-incorrect-output-for-atmos-…
samtholiya 73de411
update atmos test case
samtholiya d9c550b
Merge branch 'feature/dev-2896-incorrect-output-for-atmos-validate' o…
samtholiya e51577f
Fix help and add test cases
samtholiya e88e989
added more test cases
samtholiya b1e0b4b
filter commands added
samtholiya c74a2d4
fixed typo
samtholiya f4fe81c
removed unwanted function
samtholiya be6b4b5
remove duplicated test
samtholiya db28636
use existing theme color package
samtholiya 3886d68
Merge branch 'main' into feature/dev-2896-incorrect-output-for-atmos-…
samtholiya 27d6ae0
use existing theme color package
samtholiya 424f6c6
Merge branch 'feature/dev-2896-incorrect-output-for-atmos-validate' o…
samtholiya 4780d79
Update cmd/cmd_utils.go
samtholiya bd91bef
Fix error message ux
samtholiya e7a8434
Merge branch 'feature/dev-2896-incorrect-output-for-atmos-validate' o…
samtholiya f1e2b9b
fix template
samtholiya 36357af
fix usage test case for atmos terraform
samtholiya 00c4357
fix atlantis help
samtholiya 8fded4b
Add more test cases for atlantis
samtholiya c3a6b6e
fix atlantis command help and usage
samtholiya f504b75
Merge branch 'main' into feature/dev-2896-incorrect-output-for-atmos-…
samtholiya 33bdded
fix atlantis command help and usage
samtholiya c7ee7e7
Merge branch 'feature/dev-2896-incorrect-output-for-atmos-validate' o…
samtholiya c0a0891
Merge branch 'main' of https://github.com/cloudposse/atmos into featu…
samtholiya ac22b72
fix test and update golden screenshots
samtholiya 9d5d407
fix tests for atmos help
samtholiya 1858b86
Merge branch 'main' into feature/dev-2896-incorrect-output-for-atmos-…
samtholiya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
// ColoredCobra allows you to colorize Cobra's text output, | ||
// making it look better using simple settings to customize | ||
// individual parts of console output. | ||
// | ||
// Usage example: | ||
// | ||
// 1. Insert in cmd/root.go file of your project : | ||
// | ||
// import cc "github.com/ivanpirog/coloredcobra" | ||
// | ||
// 2. Put the following code to the beginning of the Execute() function: | ||
// | ||
// cc.Init(&cc.Config{ | ||
// RootCmd: rootCmd, | ||
// Headings: cc.Bold + cc.Underline, | ||
// Commands: cc.Yellow + cc.Bold, | ||
// ExecName: cc.Bold, | ||
// Flags: cc.Bold, | ||
// }) | ||
// | ||
// 3. Build & execute your code. | ||
// | ||
// Copyright © 2022 Ivan Pirog <[email protected]>. | ||
// Released under the MIT license. | ||
// Project home: https://github.com/ivanpirog/coloredcobra | ||
package colored | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/cloudposse/atmos/pkg/ui/theme" | ||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Config is a settings structure which sets styles for individual parts of Cobra text output. | ||
// | ||
// Note that RootCmd is required. | ||
// | ||
// Example: | ||
// | ||
// c := &cc.Config{ | ||
// RootCmd: rootCmd, | ||
// } | ||
type Config struct { | ||
RootCmd *cobra.Command | ||
NoExtraNewlines bool | ||
NoBottomNewline bool | ||
} | ||
|
||
// Init patches Cobra's usage template with configuration provided. | ||
func Init(cfg *Config) { | ||
|
||
if cfg.RootCmd == nil { | ||
panic("coloredcobra: Root command pointer is missing.") | ||
} | ||
|
||
// Get usage template | ||
tpl := cfg.RootCmd.UsageTemplate() | ||
|
||
// | ||
// Add extra line breaks for headings | ||
// | ||
if !cfg.NoExtraNewlines { | ||
tpl = strings.NewReplacer( | ||
"Use \"", "\nUse \"", | ||
).Replace(tpl) | ||
} | ||
|
||
// | ||
// Styling headers | ||
// | ||
if theme.Styles.Help.Headings != nil { | ||
ch := theme.Styles.Help.Headings | ||
// Add template function to style the headers | ||
cobra.AddTemplateFunc("HeadingStyle", ch.SprintFunc()) | ||
} | ||
|
||
// | ||
// Styling commands | ||
// | ||
if theme.Styles.Help.Commands != nil { | ||
cc := theme.Styles.Help.Commands | ||
|
||
// Add template function to style commands | ||
cobra.AddTemplateFunc("CommandStyle", cc.SprintFunc()) | ||
cobra.AddTemplateFunc("sum", func(a, b int) int { | ||
return a + b | ||
}) | ||
|
||
// Patch usage template | ||
re := regexp.MustCompile(`(?i){{\s*rpad\s+.Name\s+.NamePadding\s*}}`) | ||
tpl = re.ReplaceAllLiteralString(tpl, "{{rpad (CommandStyle .Name) (sum .NamePadding 12)}}") | ||
|
||
re = regexp.MustCompile(`(?i){{\s*rpad\s+.CommandPath\s+.CommandPathPadding\s*}}`) | ||
tpl = re.ReplaceAllLiteralString(tpl, "{{rpad (CommandStyle .CommandPath) (sum .CommandPathPadding 12)}}") | ||
} | ||
|
||
// | ||
// Styling a short desription of commands | ||
// | ||
if theme.Styles.Help.CmdShortDescr != nil { | ||
csd := theme.Styles.Help.CmdShortDescr | ||
|
||
cobra.AddTemplateFunc("CmdShortStyle", csd.SprintFunc()) | ||
|
||
re := regexp.MustCompile(`(?ism)({{\s*range\s+.Commands\s*}}.*?){{\s*.Short\s*}}`) | ||
tpl = re.ReplaceAllString(tpl, `$1{{CmdShortStyle .Short}}`) | ||
} | ||
|
||
// | ||
// Styling executable file name | ||
// | ||
if theme.Styles.Help.ExecName != nil { | ||
cen := theme.Styles.Help.ExecName | ||
|
||
// Add template functions | ||
cobra.AddTemplateFunc("ExecStyle", cen.SprintFunc()) | ||
cobra.AddTemplateFunc("UseLineStyle", func(s string) string { | ||
spl := strings.Split(s, " ") | ||
spl[0] = cen.Sprint(spl[0]) | ||
return strings.Join(spl, " ") | ||
}) | ||
|
||
// Patch usage template | ||
re := regexp.MustCompile(`(?i){{\s*.CommandPath\s*}}`) | ||
tpl = re.ReplaceAllLiteralString(tpl, "{{ExecStyle .CommandPath}}") | ||
|
||
re = regexp.MustCompile(`(?i){{\s*.UseLine\s*}}`) | ||
tpl = re.ReplaceAllLiteralString(tpl, "{{UseLineStyle .UseLine}}") | ||
} | ||
|
||
// | ||
// Styling flags | ||
// | ||
var cf, cfd, cfdt *color.Color | ||
if theme.Styles.Help.Flags != nil { | ||
cf = theme.Styles.Help.Flags | ||
} | ||
if theme.Styles.Help.FlagsDescr != nil { | ||
cfd = theme.Styles.Help.FlagsDescr | ||
} | ||
if theme.Styles.Help.FlagsDataType != nil { | ||
cfdt = theme.Styles.Help.FlagsDataType | ||
} | ||
if cf != nil || cfd != nil || cfdt != nil { | ||
|
||
cobra.AddTemplateFunc("FlagStyle", func(s string) string { | ||
|
||
// Flags info section is multi-line. | ||
// Let's split these lines and iterate them. | ||
lines := strings.Split(s, "\n") | ||
for k := range lines { | ||
|
||
// Styling short and full flags (-f, --flag) | ||
if cf != nil { | ||
re := regexp.MustCompile(`(--?\S+)`) | ||
for _, flag := range re.FindAllString(lines[k], 2) { | ||
lines[k] = strings.Replace(lines[k], flag, cf.Sprint(flag), 1) | ||
} | ||
} | ||
|
||
// If no styles for flag data types and description - continue | ||
if cfd == nil && cfdt == nil { | ||
continue | ||
} | ||
|
||
// Split line into two parts: flag data type and description | ||
// Tip: Use debugger to understand the logic | ||
re := regexp.MustCompile(`\s{2,}`) | ||
spl := re.Split(lines[k], -1) | ||
if len(spl) != 3 { | ||
continue | ||
} | ||
|
||
// Styling the flag description | ||
if cfd != nil { | ||
lines[k] = strings.Replace(lines[k], spl[2], cfd.Sprint(spl[2]), 1) | ||
} | ||
|
||
// Styling flag data type | ||
// Tip: Use debugger to understand the logic | ||
if cfdt != nil { | ||
re = regexp.MustCompile(`\s+(\w+)$`) // the last word after spaces is the flag data type | ||
m := re.FindAllStringSubmatch(spl[1], -1) | ||
if len(m) == 1 && len(m[0]) == 2 { | ||
lines[k] = strings.Replace(lines[k], m[0][1], cfdt.Sprint(m[0][1]), 1) | ||
} | ||
} | ||
|
||
} | ||
s = strings.Join(lines, "\n") | ||
|
||
return s | ||
|
||
}) | ||
|
||
// Patch usage template | ||
re := regexp.MustCompile(`(?i)(\.(InheritedFlags|LocalFlags)\.FlagUsages)`) | ||
tpl = re.ReplaceAllString(tpl, "FlagStyle $1") | ||
} | ||
|
||
// | ||
// Styling aliases | ||
// | ||
if theme.Styles.Help.Aliases != nil { | ||
ca := theme.Styles.Help.Aliases | ||
cobra.AddTemplateFunc("AliasStyle", ca.SprintFunc()) | ||
|
||
re := regexp.MustCompile(`(?i){{\s*.NameAndAliases\s*}}`) | ||
tpl = re.ReplaceAllLiteralString(tpl, "{{AliasStyle .NameAndAliases}}") | ||
} | ||
|
||
// | ||
// Styling the example text | ||
// | ||
if theme.Styles.Help.Example != nil { | ||
ce := theme.Styles.Help.Example | ||
cobra.AddTemplateFunc("ExampleStyle", ce.SprintFunc()) | ||
|
||
re := regexp.MustCompile(`(?i){{\s*.Example\s*}}`) | ||
tpl = re.ReplaceAllLiteralString(tpl, "{{ExampleStyle .Example}}") | ||
} | ||
|
||
// Adding a new line to the end | ||
if !cfg.NoBottomNewline { | ||
tpl += "\n" | ||
} | ||
// Apply patched template | ||
cfg.RootCmd.SetUsageTemplate(tpl) | ||
// Debug line, uncomment when needed | ||
// fmt.Println(tpl) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.