Skip to content
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

Merge atmos specific and terraform help documentation #857

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f0b7b98
Merge atmos specific and terraform help documentation
samtholiya Dec 14, 2024
922ba49
Merge branch 'main' into feature/dev-2821-atmos-terraform-help-should…
samtholiya Dec 16, 2024
437e1d4
Add help for terraform
samtholiya Dec 17, 2024
87fd2fd
Updated help content
samtholiya Dec 23, 2024
d7cabbd
Merge branch 'main' of https://github.com/cloudposse/atmos into featu…
samtholiya Dec 23, 2024
95304aa
test fix for auto approve
samtholiya Dec 23, 2024
0f3f84e
Update message for invalid command
samtholiya Dec 24, 2024
c719d86
remove old help code
samtholiya Dec 24, 2024
891e5d2
Merge branch 'main' into feature/dev-2821-atmos-terraform-help-should…
samtholiya Dec 24, 2024
fba6986
testing if auto-approve should be with two -
samtholiya Dec 24, 2024
48fee23
Merge branch 'feature/dev-2821-atmos-terraform-help-should-also-show-…
samtholiya Dec 24, 2024
636fb16
Merge branch 'main' into feature/dev-2821-atmos-terraform-help-should…
samtholiya Dec 26, 2024
51bbb79
check error after executing old usage func
samtholiya Dec 26, 2024
7765e3f
Merge branch 'feature/dev-2821-atmos-terraform-help-should-also-show-…
samtholiya Dec 26, 2024
d0351a8
Merge branch 'main' into feature/dev-2821-atmos-terraform-help-should…
samtholiya Dec 27, 2024
0491db4
Merge branch 'main' into feature/dev-2821-atmos-terraform-help-should…
samtholiya Dec 30, 2024
ce5bef1
rebase help.go
samtholiya Dec 31, 2024
1d8fe0e
Fix terraform subcommand help
samtholiya Dec 31, 2024
a8d6345
Remove unwanted setHelpFunc
samtholiya Dec 31, 2024
ed8b9f8
terraform,helmfile empty sub command should redirect to help
samtholiya Dec 31, 2024
18a6d96
check errors for usage and help
samtholiya Dec 31, 2024
40b4dd3
Added space in between Native commands
samtholiya Dec 31, 2024
80c4b8a
Removed unwanted code
samtholiya Jan 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,6 @@ func CheckForAtmosUpdateAndPrintMessage(atmosConfig schema.AtmosConfiguration) {
}
}

func customHelpMessageToUpgradeToAtmosLatestRelease(cmd *cobra.Command, args []string) {
originalHelpFunc(cmd, args)
CheckForAtmosUpdateAndPrintMessage(atmosConfig)
}

// Check Atmos is version command
func isVersionCommand() bool {
return len(os.Args) > 1 && os.Args[1] == "version"
Expand Down
6 changes: 4 additions & 2 deletions cmd/helmfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ var helmfileCmd = &cobra.Command{
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
}
// Exit on help
if info.NeedHelp {
if info.NeedHelp || (info.SubCommand == "" && info.SubCommand2 == "") {
// Check for the latest Atmos release on GitHub and print update message
CheckForAtmosUpdateAndPrintMessage(atmosConfig)
if err := cmd.Help(); err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
}
return
}
// Check Atmos configuration
Expand Down
49 changes: 23 additions & 26 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (

var atmosConfig schema.AtmosConfiguration

// originalHelpFunc holds Cobra's original help function to avoid recursion.
var originalHelpFunc func(*cobra.Command, []string)

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "atmos",
Expand Down Expand Up @@ -76,12 +73,8 @@ func Execute() error {

// Check if the `help` flag is passed and print a styled Atmos logo to the terminal before printing the help
err := RootCmd.ParseFlags(os.Args)
if err != nil && errors.Is(err, pflag.ErrHelp) {
fmt.Println()
err = tuiUtils.PrintStyledText("ATMOS")
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
}
if err != nil && !errors.Is(err, pflag.ErrHelp) {
u.LogErrorAndExit(atmosConfig, err)
}
// InitCliConfig finds and merges CLI configurations in the following order:
// system dir, home dir, current dir, ENV vars, command-line arguments
Expand All @@ -96,14 +89,6 @@ func Execute() error {
}
}

// Save the original help function to prevent infinite recursion when overriding it.
// This allows us to call the original help functionality within our custom help function.
originalHelpFunc = RootCmd.HelpFunc()

// Override the help function with a custom one that adds an upgrade message after displaying help.
// This custom help function will call the original help function and then display the bordered message.
RootCmd.SetHelpFunc(customHelpMessageToUpgradeToAtmosLatestRelease)

// If CLI configuration was found, process its custom commands and command aliases
if initErr == nil {
err = processCustomCommands(atmosConfig, atmosConfig.Commands, RootCmd, true)
Expand Down Expand Up @@ -132,25 +117,37 @@ func init() {

// Set custom usage template
templates.SetCustomUsageFunc(RootCmd)
cobra.OnInitialize(initConfig)
initCobraConfig()
}

func initConfig() {
func initCobraConfig() {
styles := boa.DefaultStyles()
b := boa.New(boa.WithStyles(styles))

oldUsageFunc := RootCmd.UsageFunc()
RootCmd.SetUsageFunc(b.UsageFunc)

RootCmd.SetHelpFunc(func(command *cobra.Command, strings []string) {
// Print a styled Atmos logo to the terminal
fmt.Println()
err := tuiUtils.PrintStyledText("ATMOS")
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
if command.Use != "atmos" {
err := tuiUtils.PrintStyledText("ATMOS")
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
}
if err := oldUsageFunc(command); err != nil {
u.LogErrorAndExit(atmosConfig, err)
}
} else {
err := tuiUtils.PrintStyledText("ATMOS")
if err != nil {
u.LogErrorAndExit(atmosConfig, err)
}
b.HelpFunc(command, strings)
if err := command.Usage(); err != nil {
u.LogErrorAndExit(atmosConfig, err)
}
}

b.HelpFunc(command, strings)
command.Usage()
CheckForAtmosUpdateAndPrintMessage(atmosConfig)
})
}

Expand Down
104 changes: 79 additions & 25 deletions cmd/terraform.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package cmd

import (
"fmt"

"github.com/samber/lo"
"github.com/spf13/cobra"

e "github.com/cloudposse/atmos/internal/exec"
"github.com/cloudposse/atmos/internal/tui/templates"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
cc "github.com/ivanpirog/coloredcobra"
)

// terraformCmd represents the base command for all terraform sub-commands
Expand All @@ -16,42 +20,92 @@ var terraformCmd = &cobra.Command{
Short: "Execute Terraform commands",
Long: `This command executes Terraform commands`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: true},
Run: func(cmd *cobra.Command, args []string) {
// Check Atmos configuration
//checkAtmosConfig()

var argsAfterDoubleDash []string
var finalArgs = args
RunE: func(cmd *cobra.Command, args []string) error {
return terraformRun(cmd, cmd, args)
},
}

doubleDashIndex := lo.IndexOf(args, "--")
if doubleDashIndex > 0 {
finalArgs = lo.Slice(args, 0, doubleDashIndex)
argsAfterDoubleDash = lo.Slice(args, doubleDashIndex+1, len(args))
}
info, err := e.ProcessCommandLineArgs("terraform", cmd, finalArgs, argsAfterDoubleDash)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
// Contains checks if a slice of strings contains an exact match for the target string.
func Contains(slice []string, target string) bool {
for _, item := range slice {
if item == target {
return true
}
}
return false
}

// Exit on help
if info.NeedHelp {
// Check for the latest Atmos release on GitHub and print update message
CheckForAtmosUpdateAndPrintMessage(atmosConfig)
return
func terraformRun(cmd *cobra.Command, actualCmd *cobra.Command, args []string) error {
var argsAfterDoubleDash []string
var finalArgs = args

doubleDashIndex := lo.IndexOf(args, "--")
if doubleDashIndex > 0 {
finalArgs = lo.Slice(args, 0, doubleDashIndex)
argsAfterDoubleDash = lo.Slice(args, doubleDashIndex+1, len(args))
}

info, _ := e.ProcessCommandLineArgs("terraform", cmd, finalArgs, argsAfterDoubleDash)
// Exit on help
if info.NeedHelp || (info.SubCommand == "" && info.SubCommand2 == "") {
if info.SubCommand != "" && info.SubCommand != "--help" && info.SubCommand != "help" {
suggestions := cmd.SuggestionsFor(args[0])
if !Contains(suggestions, args[0]) {
if len(suggestions) > 0 {
fmt.Printf("Unknown command: '%s'\n\nDid you mean this?\n", args[0])
for _, suggestion := range suggestions {
fmt.Printf(" %s\n", suggestion)
}
} else {
fmt.Printf(`Error: Unknkown command %q for %q`+"\n", args[0], cmd.CommandPath())
}
fmt.Printf(`Run '%s --help' for usage`+"\n", cmd.CommandPath())
return fmt.Errorf("unknown command %q for %q", args[0], cmd.CommandPath())
}
}
// Check Atmos configuration
checkAtmosConfig()
// check if this is terraform --help command. TODO: check if this is the best way to do
if cmd == actualCmd {
template := templates.GenerateFromBaseTemplate(actualCmd.Use, []templates.HelpTemplateSections{
templates.LongDescription,
templates.Usage,
templates.Aliases,
templates.Examples,
templates.AvailableCommands,
templates.Flags,
templates.GlobalFlags,
templates.NativeCommands,
templates.DoubleDashHelp,
templates.Footer,
})
actualCmd.SetUsageTemplate(template)
cc.Init(&cc.Config{
RootCmd: actualCmd,
Headings: cc.HiCyan + cc.Bold + cc.Underline,
Commands: cc.HiGreen + cc.Bold,
Example: cc.Italic,
ExecName: cc.Bold,
Flags: cc.Bold,
})

err = e.ExecuteTerraform(info)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
}
},

actualCmd.Help()
osterman marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
// Check Atmos configuration
checkAtmosConfig()

err := e.ExecuteTerraform(info)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
}
return nil
}

func init() {
// https://github.com/spf13/cobra/issues/739
terraformCmd.DisableFlagParsing = true
terraformCmd.PersistentFlags().StringP("stack", "s", "", "atmos terraform <terraform_command> <component> -s <stack>")
attachTerraformCommands(terraformCmd)
RootCmd.AddCommand(terraformCmd)
}
Loading
Loading