Skip to content

Commit

Permalink
use cobra.NoArgs instead
Browse files Browse the repository at this point in the history
  • Loading branch information
samtholiya committed Jan 13, 2025
1 parent 35c8182 commit 55c1362
Show file tree
Hide file tree
Showing 37 changed files with 43 additions and 194 deletions.
2 changes: 1 addition & 1 deletion cmd/atlantis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ var atlantisCmd = &cobra.Command{
Short: "Generate and manage Atlantis configurations",
Long: `Generate and manage Atlantis configurations that use Atmos under the hood to run Terraform workflows, bringing the power of Atmos to Atlantis for streamlined infrastructure automation.`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
}

func init() {
addUsageCommand(atlantisCmd, false)
RootCmd.AddCommand(atlantisCmd)
}
2 changes: 1 addition & 1 deletion cmd/atlantis_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ var atlantisGenerateCmd = &cobra.Command{
Short: "Generate Atlantis configuration files",
Long: "This command generates configuration files to automate and streamline Terraform workflows with Atlantis.",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
}

func init() {
addUsageCommand(atlantisGenerateCmd, false)
atlantisCmd.AddCommand(atlantisGenerateCmd)
}
6 changes: 1 addition & 5 deletions cmd/atlantis_generate_repo_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ var atlantisGenerateRepoConfigCmd = &cobra.Command{
Short: "Generate repository configuration for Atlantis",
Long: "Generate the repository configuration file required for Atlantis to manage Terraform repositories.",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
if hasPositionalArgs(args) {
showUsageAndExit(cmd, args)
}

// Check Atmos configuration
checkAtmosConfig()
err := e.ExecuteAtlantisGenerateRepoConfigCmd(cmd, args)
Expand Down
2 changes: 1 addition & 1 deletion cmd/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ var awsCmd = &cobra.Command{
Short: "Run AWS-specific commands for interacting with cloud resources",
Long: `This command allows interaction with AWS resources through various CLI commands.`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
}

func init() {
addUsageCommand(awsCmd, false)
RootCmd.AddCommand(awsCmd)
}
2 changes: 1 addition & 1 deletion cmd/aws_eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ You can use this command to interact with AWS EKS, including operations like con
For a list of available AWS EKS commands, refer to the Atmos documentation:
https://atmos.tools/cli/commands/aws/eks-update-kubeconfig`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
}

func init() {
addUsageCommand(awsEksCmd, false)
awsCmd.AddCommand(awsEksCmd)
}
1 change: 0 additions & 1 deletion cmd/aws_eks_update_kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ See https://docs.aws.amazon.com/cli/latest/reference/eks/update-kubeconfig.html

FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
err := e.ExecuteAwsEksUpdateKubeconfigCommand(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
10 changes: 1 addition & 9 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func checkAtmosConfig(opts ...AtmosValidateOption) {
atmosConfigExists, err := u.IsDirectory(atmosConfig.StacksBaseAbsolutePath)
if !atmosConfigExists || err != nil {
printMessageForMissingAtmosConfig(atmosConfig)
os.Exit(0)
os.Exit(1)
}
}
}
Expand Down Expand Up @@ -560,8 +560,6 @@ func showUsageAndExit(cmd *cobra.Command, args []string) {
unkonwnCommand := fmt.Sprintf("Unknown command: %q", cmd.CommandPath())

if len(args) > 0 {
// Show help if the first argument is "help"
handleHelpRequest(cmd, args)
suggestions = cmd.SuggestionsFor(args[0])
subCommand = args[0]
unkonwnCommand = fmt.Sprintf(`Error: Unkown command %q for %q`+"\n", subCommand, cmd.CommandPath())
Expand Down Expand Up @@ -591,12 +589,6 @@ func showUsageAndExit(cmd *cobra.Command, args []string) {
u.LogErrorAndExit(atmosConfig, errors.New(unkonwnCommand))
}

func addUsageCommand(cmd *cobra.Command, isNativeCommandsAvailable bool) {
cmd.Run = func(cmd *cobra.Command, args []string) {
showUsageAndExit(cmd, args)
}
}

// hasPositionalArgs checks if a slice of strings contains an exact match for the target string.
func hasPositionalArgs(args []string) bool {
for i := 0; i < len(args); i++ {
Expand Down
72 changes: 0 additions & 72 deletions cmd/cmd_utils_test.go

This file was deleted.

9 changes: 2 additions & 7 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ var completionCmd = &cobra.Command{
Short: "Generate autocompletion scripts for Bash, Zsh, Fish, and PowerShell",
Long: "This command generates completion scripts for Bash, Zsh, Fish and PowerShell",
DisableFlagsInUseLine: true,
// Why I am not using cobra inbuilt validation for Args:
// Because we have our own custom validation for Args
// Why we have our own custom validation for Args:
// Because we want to show custom error message when user provides invalid shell name
// ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
// Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Args: cobra.NoArgs,
}

func runCompletion(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -48,8 +43,8 @@ func init() {
Short: "Generate completion script for " + shellName,
Long: "This command generates completion scripts for " + shellName,
Run: runCompletion,
Args: cobra.NoArgs,
})
}
addUsageCommand(completionCmd, false)
RootCmd.AddCommand(completionCmd)
}
2 changes: 1 addition & 1 deletion cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ var describeCmd = &cobra.Command{
Short: "Show details about Atmos configurations and components",
Long: `Display configuration details for Atmos CLI, stacks, and components.`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
}

func init() {
addUsageCommand(describeCmd, false)
describeCmd.PersistentFlags().StringP("query", "q", "", "Query the results of an 'atmos describe' command using 'yq' expressions: atmos describe <subcommand> --query <yq-expression>")
RootCmd.AddCommand(describeCmd)
}
6 changes: 1 addition & 5 deletions cmd/describe_affected.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ var describeAffectedCmd = &cobra.Command{
Short: "List Atmos components and stacks affected by two Git commits",
Long: "Identify and list Atmos components and stacks impacted by changes between two Git commits.",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
if hasPositionalArgs(args) {
showUsageAndExit(cmd, args)
}

// Check Atmos configuration
checkAtmosConfig()

Expand Down
1 change: 0 additions & 1 deletion cmd/describe_component.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var describeComponentCmd = &cobra.Command{
Long: `Display the configuration details for a specific Atmos component within a designated Atmos stack, including its dependencies, settings, and overrides.`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
// Check Atmos configuration
checkAtmosConfig()

Expand Down
5 changes: 1 addition & 4 deletions cmd/describe_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ var describeConfigCmd = &cobra.Command{
Short: "Display the final merged CLI configuration",
Long: "This command displays the final, deep-merged CLI configuration after combining all relevant configuration files.",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
if hasPositionalArgs(args) {
showUsageAndExit(cmd, args)
}

err := e.ExecuteDescribeConfigCmd(cmd, args)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion cmd/describe_dependents.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var describeDependentsCmd = &cobra.Command{
Long: "This command generates a list of Atmos components within stacks that depend on the specified Atmos component.",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
// Check Atmos configuration
checkAtmosConfig()

Expand Down
5 changes: 1 addition & 4 deletions cmd/describe_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ var describeStacksCmd = &cobra.Command{
Short: "Display configuration for Atmos stacks and their components",
Long: "This command shows the configuration details for Atmos stacks and the components within those stacks.",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
if hasPositionalArgs(args) {
showUsageAndExit(cmd, args)
}

// Check Atmos configuration
checkAtmosConfig()
Expand Down
6 changes: 1 addition & 5 deletions cmd/describe_workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ var describeWorkflowsCmd = &cobra.Command{
"describe workflows -o map\n" +
"describe workflows -o all",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
if hasPositionalArgs(args) {
showUsageAndExit(cmd, args)
}

err := e.ExecuteDescribeWorkflowsCmd(cmd, args)
if err != nil {
u.LogErrorAndExit(schema.AtmosConfiguration{}, err)
Expand Down
1 change: 0 additions & 1 deletion cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var docsCmd = &cobra.Command{
Args: cobra.MaximumNArgs(1),
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
if len(args) == 1 {
info := schema.ConfigAndStacksInfo{
Component: args[0],
Expand Down
3 changes: 1 addition & 2 deletions cmd/helmfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ var helmfileCmd = &cobra.Command{
Short: "Manage Helmfile-based Kubernetes deployments",
Long: `This command runs Helmfile commands to manage Kubernetes deployments using Helmfile.`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: true},
Args: cobra.NoArgs,
}

func init() {
// https://github.com/spf13/cobra/issues/739
helmfileCmd.DisableFlagParsing = true
helmfileCmd.PersistentFlags().StringP("stack", "s", "", "atmos helmfile <helmfile_command> <component> -s <stack>")
addUsageCommand(helmfileCmd, false)
RootCmd.AddCommand(helmfileCmd)
}

func helmfileRun(cmd *cobra.Command, commandName string, args []string) {
handleHelpRequest(cmd, args)
diffArgs := []string{commandName}
diffArgs = append(diffArgs, args...)
info := getConfigAndStacksInfo("helmfile", cmd, diffArgs)
Expand Down
2 changes: 1 addition & 1 deletion cmd/helmfile_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ var helmfileGenerateCmd = &cobra.Command{
Short: "Generate configurations for Helmfile components",
Long: "This command generates various configuration files for Helmfile components in Atmos.",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
}

func init() {
addUsageCommand(helmfileGenerateCmd, false)
helmfileCmd.AddCommand(helmfileGenerateCmd)
}
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ var listCmd = &cobra.Command{
Short: "List available stacks and components",
Long: `Display a list of all available stacks and components defined in your project.`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
}

func init() {
addUsageCommand(listCmd, false)
RootCmd.AddCommand(listCmd)
}
5 changes: 1 addition & 4 deletions cmd/list_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ var listComponentsCmd = &cobra.Command{
Long: "List Atmos components, with options to filter results by specific stacks.",
Example: "atmos list components\n" +
"atmos list components -s <stack>",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
if hasPositionalArgs(args) {
showUsageAndExit(cmd, args)
}
// Check Atmos configuration
checkAtmosConfig()

Expand Down
5 changes: 1 addition & 4 deletions cmd/list_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ var listStacksCmd = &cobra.Command{
Example: "atmos list stacks\n" +
"atmos list stacks -c <component>",
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
if hasPositionalArgs(args) {
showUsageAndExit(cmd, args)
}
// Check Atmos configuration
checkAtmosConfig()

Expand Down
2 changes: 1 addition & 1 deletion cmd/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ var proCmd = &cobra.Command{
Short: "Access premium features integrated with app.cloudposse.com",
Long: `This command allows you to manage and configure premium features available through app.cloudposse.com.`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
}

func init() {
addUsageCommand(proCmd, false)
RootCmd.AddCommand(proCmd)
}
5 changes: 1 addition & 4 deletions cmd/pro_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ var proLockCmd = &cobra.Command{
Short: "Lock a stack",
Long: `This command calls the atmos pro API and locks a stack`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
if hasPositionalArgs(args) {
showUsageAndExit(cmd, args)
}
// Check Atmos configuration
checkAtmosConfig()

Expand Down
5 changes: 1 addition & 4 deletions cmd/pro_unlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ var proUnlockCmd = &cobra.Command{
Short: "Unlock a stack",
Long: `This command calls the atmos pro API and unlocks a stack`,
FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
handleHelpRequest(cmd, args)
if hasPositionalArgs(args) {
showUsageAndExit(cmd, args)
}
// Check Atmos configuration
checkAtmosConfig()

Expand Down
Loading

0 comments on commit 55c1362

Please sign in to comment.