Skip to content

Commit

Permalink
fix: Fixing command not found error (#3330)
Browse files Browse the repository at this point in the history
* fix: Fixing command not found error

* fix: Fixing integration test for command not found error
  • Loading branch information
yhakbar authored Aug 9, 2024
1 parent e79d9f4 commit e354194
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
9 changes: 8 additions & 1 deletion cli/commands/terraform/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package terraform

import (
"strings"

"github.com/gruntwork-io/go-commons/errors"
"github.com/gruntwork-io/gruntwork-cli/collections"
"github.com/gruntwork-io/terragrunt/options"
Expand Down Expand Up @@ -33,7 +35,12 @@ func action(opts *options.TerragruntOptions) cli.ActionFunc {
}

if !opts.DisableCommandValidation && !collections.ListContainsElement(nativeTerraformCommands, opts.TerraformCommand) {
return errors.WithStackTrace(WrongTerraformCommand(opts.TerraformCommand))
if strings.HasSuffix(opts.TerraformPath, "terraform") {
return errors.WithStackTrace(WrongTerraformCommand(opts.TerraformCommand))
} else {
// We default to tofu if the terraform path does not end in Terraform
return errors.WithStackTrace(WrongTofuCommand(opts.TerraformCommand))
}
}

return Run(ctx.Context, opts.OptionsFromContext(ctx))
Expand Down
53 changes: 53 additions & 0 deletions cli/commands/terraform/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package terraform

import (
"context"
"testing"

"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/pkg/cli"
"github.com/stretchr/testify/require"
)

func TestAction(t *testing.T) {
t.Parallel()

tt := []struct {
name string
opts *options.TerragruntOptions
expectedErr error
}{
{
name: "wrong tofu command",
opts: &options.TerragruntOptions{
TerraformCommand: "foo",
TerraformPath: "tofu",
},
expectedErr: WrongTofuCommand("foo"),
},
{
name: "wrong terraform command",
opts: &options.TerragruntOptions{
TerraformCommand: "foo",
TerraformPath: "terraform",
},
expectedErr: WrongTerraformCommand("foo"),
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
fn := action(tc.opts)

ctx := cli.Context{
Context: context.Background(),
}
err := fn(&ctx)
if tc.expectedErr != nil {
require.ErrorIs(t, err, tc.expectedErr)
} else {
require.NoError(t, err)
}
})
}
}
6 changes: 6 additions & 0 deletions cli/commands/terraform/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func (name WrongTerraformCommand) Error() string {
return fmt.Sprintf("Terraform has no command named %q. To see all of Terraform's top-level commands, run: terraform -help", string(name))
}

type WrongTofuCommand string

func (name WrongTofuCommand) Error() string {
return fmt.Sprintf("OpenTofu has no command named %q. To see all of OpenTofu's top-level commands, run: tofu -help", string(name))
}

type BackendNotDefined struct {
Opts *options.TerragruntOptions
BackendType string
Expand Down
10 changes: 9 additions & 1 deletion test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ func TestTerraformCommandCliArgs(t *testing.T) {
{
[]string{"paln"},
"",
terraform.WrongTerraformCommand("paln"),
expectedWrongCommandErr("paln"),
},
{
[]string{"paln", "--terragrunt-disable-command-validation"},
Expand Down Expand Up @@ -7433,6 +7433,14 @@ func wrappedBinary() string {
return filepath.Base(value)
}

// expectedWrongCommandErr - return expected error message for wrong command
func expectedWrongCommandErr(command string) error {
if wrappedBinary() == TOFU_BINARY {
return terraform.WrongTofuCommand(command)
}
return terraform.WrongTerraformCommand(command)
}

func isTerraform() bool {
return wrappedBinary() == TERRAFORM_BINARY
}
Expand Down

0 comments on commit e354194

Please sign in to comment.