Skip to content

Commit

Permalink
feat: Adding stylecheck lint (#3354)
Browse files Browse the repository at this point in the history
* fix: Adding workaround for DDB eventual consistency of tags

* fix: Fixing dynamodb check

* fix: Removing unnecessary extra wait

* feat: Merging in `.golangci.yml`

* feat: Addressing `stylecheck` lint errors

* fix: Fixing tflint tests

* fix: Fixing lints post rebase

* fix: Resolving merge conflicts from `main`

* fix: Privatizing fixture variable names

* fix: Privatizing more test constants

* fix: Resolving some merge conflicts

* fix: Adjusting method receiver renames so that they aren't all single characters

* fix: Fixing merge conflicts

* fix: Setting refs to commits instead of tags

* fix: Reseting fixtures to track `main`

* fix: Fixing more tests

* fix: Linting

* fix: Trying to fix test by not using tenv

* fix: Fixed grammer mistake
  • Loading branch information
yhakbar authored Sep 16, 2024
1 parent 64baa28 commit 979efc8
Show file tree
Hide file tree
Showing 147 changed files with 1,539 additions and 1,340 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ linters:
- wsl
- thelper
- wastedassign
- stylecheck

enable-all: false
disable:
- depguard
- gosec
- gocyclo
- exhaustruct
- nolintlint
- wrapcheck
- varnamelen

fast: false
mnd:
Expand Down
21 changes: 11 additions & 10 deletions aws_helper/config.go → awshelper/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package aws_helper
// Package awshelper provides helper functions for working with AWS services.
package awshelper

import (
"fmt"
Expand All @@ -20,7 +21,7 @@ import (
"github.com/gruntwork-io/terragrunt/options"
)

// A representation of the configuration options for an AWS Session
// AwsSessionConfig is a representation of the configuration options for an AWS Session
type AwsSessionConfig struct {
Region string
CustomS3Endpoint string
Expand All @@ -41,7 +42,7 @@ var addUserAgent = request.NamedHandler{
"terragrunt", version.GetVersion()),
}

// Returns an AWS session object for the given config region (required), profile name (optional), and IAM role to assume
// CreateAwsSessionFromConfig returns an AWS session object for the given config region (required), profile name (optional), and IAM role to assume
// (optional), ensuring that the credentials are available.
func CreateAwsSessionFromConfig(config *AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (*session.Session, error) {
defaultResolver := endpoints.DefaultResolver()
Expand Down Expand Up @@ -185,7 +186,7 @@ func getCredentialsFromEnvs(opts *options.TerragruntOptions) *credentials.Creden
return credentials.NewStaticCredentials(accessKeyID, secretAccessKey, sessionToken)
}

// Returns an AWS session object. The session is configured by either:
// CreateAwsSession returns an AWS session object. The session is configured by either:
// - The provided AwsSessionConfig struct, which specifies region (required), profile name (optional), and IAM role to
// assume (optional).
// - The provided TerragruntOptions struct, which specifies any IAM role to assume (optional).
Expand Down Expand Up @@ -238,7 +239,7 @@ func CreateAwsSession(config *AwsSessionConfig, terragruntOptions *options.Terra
return sess, nil
}

// Make API calls to AWS to assume the IAM role specified and return the temporary AWS credentials to use that role
// AssumeIamRole makes API calls to AWS to assume the IAM role specified and return the temporary AWS credentials to use that role.
func AssumeIamRole(iamRoleOpts options.IAMRoleOptions) (*sts.Credentials, error) {
sessionOptions := session.Options{SharedConfigState: session.SharedConfigEnable}

Expand Down Expand Up @@ -318,7 +319,7 @@ func AssumeIamRole(iamRoleOpts options.IAMRoleOptions) (*sts.Credentials, error)
return resp.Credentials, nil
}

// Return the AWS caller identity associated with the current set of credentials
// GetAWSCallerIdentity returns the AWS caller identity associated with the current set of credentials
func GetAWSCallerIdentity(config *AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (sts.GetCallerIdentityOutput, error) {
sess, err := CreateAwsSession(config, terragruntOptions)
if err != nil {
Expand All @@ -340,7 +341,7 @@ func ValidateAwsSession(config *AwsSessionConfig, terragruntOptions *options.Ter
return err
}

// Get the AWS Partition of the current session configuration
// GetAWSPartition gets the AWS Partition of the current session configuration
func GetAWSPartition(config *AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (string, error) {
identity, err := GetAWSCallerIdentity(config, terragruntOptions)
if err != nil {
Expand All @@ -355,7 +356,7 @@ func GetAWSPartition(config *AwsSessionConfig, terragruntOptions *options.Terrag
return arn.Partition, nil
}

// Get the AWS account ID of the current session configuration
// GetAWSAccountID gets the AWS account ID of the current session configuration.
func GetAWSAccountID(config *AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (string, error) {
identity, err := GetAWSCallerIdentity(config, terragruntOptions)
if err != nil {
Expand All @@ -365,7 +366,7 @@ func GetAWSAccountID(config *AwsSessionConfig, terragruntOptions *options.Terrag
return *identity.Account, nil
}

// Get the ARN of the AWS identity associated with the current set of credentials
// GetAWSIdentityArn gets the ARN of the AWS identity associated with the current set of credentials.
func GetAWSIdentityArn(config *AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (string, error) {
identity, err := GetAWSCallerIdentity(config, terragruntOptions)
if err != nil {
Expand All @@ -375,7 +376,7 @@ func GetAWSIdentityArn(config *AwsSessionConfig, terragruntOptions *options.Terr
return *identity.Arn, nil
}

// Get the AWS user ID of the current session configuration
// GetAWSUserID gets the AWS user ID of the current session configuration.
func GetAWSUserID(config *AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (string, error) {
identity, err := GetAWSCallerIdentity(config, terragruntOptions)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions aws_helper/config_test.go → awshelper/config_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//go:build aws

package aws_helper_test
package awshelper_test

import (
"testing"

"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/gruntwork-io/terragrunt/aws_helper"
"github.com/gruntwork-io/terragrunt/awshelper"
"github.com/gruntwork-io/terragrunt/options"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -16,7 +16,7 @@ import (
func TestAwsIsAddedInUserAgent(t *testing.T) {
t.Parallel()

sess, err := aws_helper.CreateAwsSession(nil, options.NewTerragruntOptions())
sess, err := awshelper.CreateAwsSession(nil, options.NewTerragruntOptions())
require.NoError(t, err)

op := &request.Operation{
Expand All @@ -36,7 +36,7 @@ func TestAwsIsAddedInUserAgent(t *testing.T) {
func TestAwsSessionValidationFail(t *testing.T) {
t.Parallel()

err := aws_helper.ValidateAwsSession(&aws_helper.AwsSessionConfig{
err := awshelper.ValidateAwsSession(&awshelper.AwsSessionConfig{
Region: "not-existing-region",
CredsFilename: "/tmp/not-existing-file",
}, options.NewTerragruntOptions())
Expand Down
6 changes: 3 additions & 3 deletions aws_helper/policy.go → awshelper/policy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package aws_helper
package awshelper

import "encoding/json"

Expand Down Expand Up @@ -34,10 +34,10 @@ func UnmarshalPolicy(policy string) (Policy, error) {
}

func MarshalPolicy(policy Policy) ([]byte, error) {
policyJson, err := json.Marshal(policy)
policyJSON, err := json.Marshal(policy)
if err != nil {
return nil, err
}

return policyJson, nil
return policyJSON, nil
}
12 changes: 6 additions & 6 deletions aws_helper/policy_test.go → awshelper/policy_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//go:build aws

package aws_helper_test
package awshelper_test

import (
"testing"

"github.com/gruntwork-io/terragrunt/aws_helper"
"github.com/gruntwork-io/terragrunt/awshelper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -56,7 +56,7 @@ const arraysPolicy = `
func TestAwsUnmarshalStringActionResource(t *testing.T) {
t.Parallel()

bucketPolicy, err := aws_helper.UnmarshalPolicy(simplePolicy)
bucketPolicy, err := awshelper.UnmarshalPolicy(simplePolicy)
require.NoError(t, err)
assert.NotNil(t, bucketPolicy)
assert.Len(t, bucketPolicy.Statement, 1)
Expand All @@ -77,14 +77,14 @@ func TestAwsUnmarshalStringActionResource(t *testing.T) {
assert.Fail(t, "Expected string type for Resource")
}

out, err := aws_helper.MarshalPolicy(bucketPolicy)
out, err := awshelper.MarshalPolicy(bucketPolicy)
require.NoError(t, err)
assert.NotContains(t, string(out), "null")
}

func TestAwsUnmarshalActionResourceList(t *testing.T) {
t.Parallel()
bucketPolicy, err := aws_helper.UnmarshalPolicy(arraysPolicy)
bucketPolicy, err := awshelper.UnmarshalPolicy(arraysPolicy)
require.NoError(t, err)
assert.NotNil(t, bucketPolicy)
assert.Len(t, bucketPolicy.Statement, 1)
Expand All @@ -107,7 +107,7 @@ func TestAwsUnmarshalActionResourceList(t *testing.T) {
assert.Fail(t, "Expected []string type for Resource")
}

out, err := aws_helper.MarshalPolicy(bucketPolicy)
out, err := awshelper.MarshalPolicy(bucketPolicy)
require.NoError(t, err)
assert.NotContains(t, string(out), "null")
}
5 changes: 3 additions & 2 deletions cli/app.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package cli configures the Terragrunt CLI app and its commands.
package cli

import (
Expand Down Expand Up @@ -146,7 +147,7 @@ func (app *App) RunContext(ctx context.Context, args []string) error {
return nil
}

// This set of commands is also used in unit tests
// TerragruntCommands returns the set of Terragrunt commands.
func TerragruntCommands(opts *options.TerragruntOptions) cli.Commands {
cmds := cli.Commands{
runall.NewCommand(opts), // runAction-all
Expand All @@ -171,7 +172,7 @@ func TerragruntCommands(opts *options.TerragruntOptions) cli.Commands {
return cmds
}

// Wrap CLI command execution with setting of telemetry context and labels, if telemetry is disabled, just runAction the command.
// WrapWithTelemetry wraps CLI command execution with setting of telemetry context and labels, if telemetry is disabled, just runAction the command.
func WrapWithTelemetry(opts *options.TerragruntOptions) func(ctx *cli.Context, action cli.ActionFunc) error {
return func(ctx *cli.Context, action cli.ActionFunc) error {
return telemetry.Telemetry(ctx.Context, opts, fmt.Sprintf("%s %s", ctx.Command.Name, opts.TerraformCommand), map[string]interface{}{
Expand Down
44 changes: 8 additions & 36 deletions cli/commands/aws-provider-patch/action.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
// `aws-provider-patch` command finds all Terraform modules nested in the current code (i.e., in the .terraform/modules
// folder), looks for provider "aws" { ... } blocks in those modules, and overwrites the attributes in those provider
// blocks with the attributes specified in terragrntOptions.
//
// For example, if were running Terragrunt against code that contained a module:
//
// module "example" {
// source = "<URL>"
// }
//
// When you run 'init', Terraform would download the code for that module into .terraform/modules. This function would
// scan that module code for provider blocks:
//
// provider "aws" {
// region = var.aws_region
// }
//
// And if AwsProviderPatchOverrides in opts was set to map[string]string{"region": "us-east-1"}, then this
// method would update the module code to:
//
// provider "aws" {
// region = "us-east-1"
// }
//
// This is a temporary workaround for a Terraform bug (https://github.com/hashicorp/terraform/issues/13018) where
// any dynamic values in nested provider blocks are not handled correctly when you call 'terraform import', so by
// temporarily hard-coding them, we can allow 'import' to work.

package awsproviderpatch

import (
Expand Down Expand Up @@ -90,8 +62,8 @@ func runAwsProviderPatch(ctx context.Context, opts *options.TerragruntOptions, c
return nil
}

// The format we expect in the .terraform/modules/modules.json file
type TerraformModulesJson struct {
// TerraformModulesJSON is the format we expect in the .terraform/modules/modules.json file
type TerraformModulesJSON struct {
Modules []TerraformModule `json:"Modules"`
}

Expand All @@ -115,25 +87,25 @@ func findAllTerraformFilesInModules(opts *options.TerragruntOptions) ([]string,
// API, so the way we parse/read this modules.json file may break in future Terraform versions. Note that we
// can't use the official HashiCorp code to parse this file, as it's marked internal:
// https://github.com/hashicorp/terraform/blob/master/internal/modsdir/manifest.go
modulesJsonPath := util.JoinPath(opts.DataDir(), "modules", "modules.json")
modulesJSONPath := util.JoinPath(opts.DataDir(), "modules", "modules.json")

if !util.FileExists(modulesJsonPath) {
if !util.FileExists(modulesJSONPath) {
return nil, nil
}

modulesJsonContents, err := os.ReadFile(modulesJsonPath)
modulesJSONContents, err := os.ReadFile(modulesJSONPath)
if err != nil {
return nil, errors.WithStackTrace(err)
}

var terraformModulesJson TerraformModulesJson
if err := json.Unmarshal(modulesJsonContents, &terraformModulesJson); err != nil {
var terraformModulesJSON TerraformModulesJSON
if err := json.Unmarshal(modulesJSONContents, &terraformModulesJSON); err != nil {
return nil, errors.WithStackTrace(err)
}

var terraformFiles []string

for _, module := range terraformModulesJson.Modules {
for _, module := range terraformModulesJSON.Modules {
if module.Key != "" && module.Dir != "" {
moduleAbsPath := module.Dir
if !filepath.IsAbs(moduleAbsPath) {
Expand Down
29 changes: 29 additions & 0 deletions cli/commands/aws-provider-patch/command.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
// Package awsproviderpatch provides the `aws-provider-patch` command.
//
// The `aws-provider-patch` command finds all Terraform modules nested in the current code (i.e., in the .terraform/modules
// folder), looks for provider "aws" { ... } blocks in those modules, and overwrites the attributes in those provider
// blocks with the attributes specified in terragrntOptions.
//
// For example, if were running Terragrunt against code that contained a module:
//
// module "example" {
// source = "<URL>"
// }
//
// When you run 'init', Terraform would download the code for that module into .terraform/modules. This function would
// scan that module code for provider blocks:
//
// provider "aws" {
// region = var.aws_region
// }
//
// And if AwsProviderPatchOverrides in opts was set to map[string]string{"region": "us-east-1"}, then this
// method would update the module code to:
//
// provider "aws" {
// region = "us-east-1"
// }
//
// This is a temporary workaround for a Terraform bug (https://github.com/hashicorp/terraform/issues/13018) where
// any dynamic values in nested provider blocks are not handled correctly when you call 'terraform import', so by
// temporarily hard-coding them, we can allow 'import' to work.
package awsproviderpatch

import (
Expand Down
2 changes: 2 additions & 0 deletions cli/commands/catalog/command.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package catalog provides the ability to interact with a catalog of OpenTofu/Terraform modules
// via the `terragrunt catalog` command.
package catalog

import (
Expand Down
1 change: 1 addition & 0 deletions cli/commands/catalog/module/module.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package module provides a struct to represent an OpenTofu/Terraform module.
package module

import (
Expand Down
6 changes: 3 additions & 3 deletions cli/commands/catalog/module/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,16 @@ func (repo *Repo) clone(ctx context.Context) error {
}
}

sourceUrl, err := terraform.ToSourceUrl(repo.cloneURL, "")
sourceURL, err := terraform.ToSourceURL(repo.cloneURL, "")
if err != nil {
return err
}

repo.cloneURL = sourceUrl.String()
repo.cloneURL = sourceURL.String()

repo.logger.Infof("Cloning repository %q to temporary directory %q", repo.cloneURL, repo.path)

if err := getter.Get(repo.path, strings.Trim(sourceUrl.String(), "/"), getter.WithContext(ctx)); err != nil {
if err := getter.Get(repo.path, strings.Trim(sourceURL.String(), "/"), getter.WithContext(ctx)); err != nil {
return errors.WithStackTrace(err)
}

Expand Down
2 changes: 2 additions & 0 deletions cli/commands/catalog/tui/command/scaffold.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package command provides the implementation of the terragrunt scaffold command
// This command is used to scaffold a new Terragrunt unit in the current directory.
package command

import (
Expand Down
1 change: 1 addition & 0 deletions cli/commands/catalog/tui/components/buttonbar/buttonbar.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package buttonbar provides a bubbletea component that displays an inline list of buttons.
package buttonbar

import (
Expand Down
1 change: 1 addition & 0 deletions cli/commands/catalog/tui/tui.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package tui provides a text-based user interface for the Terragrunt catalog command.
package tui

import (
Expand Down
Loading

0 comments on commit 979efc8

Please sign in to comment.