Skip to content

Commit 8d3e0c1

Browse files
authored
feat: Adding wsl lint (#3351)
* feat: Adding `wsl` lint * feat: Addressing `wsl` lint errors
1 parent 5867027 commit 8d3e0c1

File tree

133 files changed

+1512
-94
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+1512
-94
lines changed

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ linters:
6565
- unconvert
6666
- unused
6767
- unparam
68+
- wsl
6869
enable-all: false
6970
disable:
7071
- depguard

aws_helper/config.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ func (f tokenFetcher) FetchToken(ctx credentials.Context) ([]byte, error) {
127127
// TODO: See if this lint error should be ignored
128128
return []byte(f), nil //nolint: nilerr
129129
}
130+
130131
token, err := os.ReadFile(string(f))
131132
if err != nil {
132133
return nil, errors.WithStackTrace(err)
133134
}
135+
134136
return token, nil
135137
}
136138

@@ -140,13 +142,16 @@ func getWebIdentityCredentialsFromIAMRoleOptions(sess *session.Session, iamRoleO
140142
// Set a unique session name in the same way it is done in the SDK
141143
roleSessionName = strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
142144
}
145+
143146
svc := sts.New(sess)
144147
p := stscreds.NewWebIdentityRoleProviderWithOptions(svc, iamRoleOptions.RoleARN, roleSessionName, tokenFetcher(iamRoleOptions.WebIdentityToken))
148+
145149
if iamRoleOptions.AssumeRoleDuration > 0 {
146150
p.Duration = time.Second * time.Duration(iamRoleOptions.AssumeRoleDuration)
147151
} else {
148152
p.Duration = time.Second * time.Duration(options.DefaultIAMAssumeRoleDuration)
149153
}
154+
150155
return credentials.NewCredentials(p)
151156
}
152157

@@ -157,10 +162,12 @@ func getSTSCredentialsFromIAMRoleOptions(sess *session.Session, iamRoleOptions o
157162
} else {
158163
p.Duration = time.Second * time.Duration(options.DefaultIAMAssumeRoleDuration)
159164
}
165+
160166
if iamRoleOptions.AssumeRoleSessionName != "" {
161167
p.RoleSessionName = iamRoleOptions.AssumeRoleSessionName
162168
}
163169
})
170+
164171
return stscreds.NewCredentials(sess, iamRoleOptions.RoleARN, optFns...)
165172
}
166173

@@ -174,6 +181,7 @@ func getCredentialsFromEnvs(opts *options.TerragruntOptions) *credentials.Creden
174181
if accessKeyID == "" || secretAccessKey == "" {
175182
return nil
176183
}
184+
177185
return credentials.NewStaticCredentials(accessKeyID, secretAccessKey, sessionToken)
178186
}
179187

@@ -185,15 +193,21 @@ func getCredentialsFromEnvs(opts *options.TerragruntOptions) *credentials.Creden
185193
// Note that if the AwsSessionConfig object is null, this will return default session credentials using the default
186194
// credentials chain of the AWS SDK.
187195
func CreateAwsSession(config *AwsSessionConfig, terragruntOptions *options.TerragruntOptions) (*session.Session, error) {
188-
var sess *session.Session
189-
var err error
196+
var (
197+
sess *session.Session
198+
err error
199+
)
200+
190201
if config == nil {
191202
sessionOptions := session.Options{SharedConfigState: session.SharedConfigEnable}
203+
192204
sess, err = session.NewSessionWithOptions(sessionOptions)
193205
if err != nil {
194206
return nil, errors.WithStackTrace(err)
195207
}
208+
196209
sess.Handlers.Build.PushFrontNamed(addUserAgent)
210+
197211
if terragruntOptions.IAMRoleOptions.RoleARN != "" {
198212
if terragruntOptions.IAMRoleOptions.WebIdentityToken != "" {
199213
terragruntOptions.Logger.Debugf("Assuming role %s using WebIdentity token", terragruntOptions.IAMRoleOptions.RoleARN)
@@ -227,6 +241,7 @@ func CreateAwsSession(config *AwsSessionConfig, terragruntOptions *options.Terra
227241
// Make API calls to AWS to assume the IAM role specified and return the temporary AWS credentials to use that role
228242
func AssumeIamRole(iamRoleOpts options.IAMRoleOptions) (*sts.Credentials, error) {
229243
sessionOptions := session.Options{SharedConfigState: session.SharedConfigEnable}
244+
230245
sess, err := session.NewSessionWithOptions(sessionOptions)
231246
if err != nil {
232247
return nil, errors.WithStackTrace(err)
@@ -281,8 +296,10 @@ func AssumeIamRole(iamRoleOpts options.IAMRoleOptions) (*sts.Credentials, error)
281296
if err != nil {
282297
return nil, errors.WithStackTrace(err)
283298
}
299+
284300
token = string(tb)
285301
}
302+
286303
input := sts.AssumeRoleWithWebIdentityInput{
287304
RoleArn: aws.String(iamRoleOpts.RoleARN),
288305
RoleSessionName: aws.String(sessionName),
@@ -297,6 +314,7 @@ func AssumeIamRole(iamRoleOpts options.IAMRoleOptions) (*sts.Credentials, error)
297314
if err := req.Send(); err != nil {
298315
return nil, errors.WithStackTrace(err)
299316
}
317+
300318
return resp.Credentials, nil
301319
}
302320

@@ -333,6 +351,7 @@ func GetAWSPartition(config *AwsSessionConfig, terragruntOptions *options.Terrag
333351
if err != nil {
334352
return "", errors.WithStackTrace(err)
335353
}
354+
336355
return arn.Partition, nil
337356
}
338357

aws_helper/policy.go

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Statement struct {
2424

2525
func UnmarshalPolicy(policy string) (Policy, error) {
2626
var p Policy
27+
2728
err := json.Unmarshal([]byte(policy), &p)
2829
if err != nil {
2930
return p, err

cli/app.go

+14
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,15 @@ func NewApp(writer io.Writer, errWriter io.Writer) *App {
7575
app.Version = version.GetVersion()
7676
app.Writer = writer
7777
app.ErrWriter = errWriter
78+
7879
app.Flags = append(
7980
commands.NewGlobalFlags(opts),
8081
commands.NewHelpVersionFlags(opts)...)
82+
8183
app.Commands = append(
8284
DeprecatedCommands(opts),
8385
TerragruntCommands(opts)...).WrapAction(WrapWithTelemetry(opts))
86+
8487
app.Before = beforeAction(opts)
8588
app.DefaultCommand = terraformCmd.NewCommand(opts).WrapAction(WrapWithTelemetry(opts)) // by default, if no terragrunt command is specified, run the Terraform command
8689
app.OsExiter = OSExiter
@@ -133,6 +136,7 @@ func (app *App) RunContext(ctx context.Context, args []string) error {
133136
if engine.IsEngineEnabled() {
134137
ctx = engine.WithEngineValues(ctx)
135138
}
139+
136140
defer func(ctx context.Context) {
137141
if err := engine.Shutdown(ctx); err != nil {
138142
_, _ = app.ErrWriter.Write([]byte(err.Error()))
@@ -142,6 +146,7 @@ func (app *App) RunContext(ctx context.Context, args []string) error {
142146
if err := app.App.RunContext(ctx, args); err != nil && !goerrors.Is(err, context.Canceled) {
143147
return err
144148
}
149+
145150
return nil
146151
}
147152

@@ -198,6 +203,7 @@ func beforeAction(_ *options.TerragruntOptions) cli.ActionFunc {
198203
// exit the app
199204
return cli.NewExitError(err, 0)
200205
}
206+
201207
return nil
202208
}
203209
}
@@ -235,6 +241,7 @@ func runAction(cliCtx *cli.Context, opts *options.TerragruntOptions, action cli.
235241
if action != nil {
236242
return action(cliCtx)
237243
}
244+
238245
return nil
239246
})
240247

@@ -298,8 +305,10 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {
298305
if err != nil {
299306
return errors.WithStackTrace(err)
300307
}
308+
301309
opts.WorkingDir = currentDir
302310
}
311+
303312
opts.WorkingDir = filepath.ToSlash(opts.WorkingDir)
304313

305314
// --- Download Dir
@@ -311,6 +320,7 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {
311320
if err != nil {
312321
return errors.WithStackTrace(err)
313322
}
323+
314324
opts.DownloadDir = filepath.ToSlash(downloadDir)
315325

316326
// --- Terragrunt ConfigPath
@@ -346,6 +356,7 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {
346356
if err != nil {
347357
return err
348358
}
359+
349360
opts.ExcludeDirs = append(opts.ExcludeDirs, excludeDirs...)
350361

351362
// --- Terragrunt Version
@@ -356,18 +367,21 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {
356367
return errors.WithStackTrace(err)
357368
}
358369
}
370+
359371
opts.TerragruntVersion = terragruntVersion
360372
// Log the terragrunt version in debug mode. This helps with debugging issues and ensuring a specific version of terragrunt used.
361373
opts.Logger.Debugf("Terragrunt Version: %s", opts.TerragruntVersion)
362374

363375
// --- IncludeModulePrefix
364376
jsonOutput := false
377+
365378
for _, arg := range opts.TerraformCliArgs {
366379
if strings.EqualFold(arg, "-json") {
367380
jsonOutput = true
368381
break
369382
}
370383
}
384+
371385
if opts.IncludeModulePrefix && !jsonOutput {
372386
opts.OutputPrefix = fmt.Sprintf("[%s] ", opts.WorkingDir)
373387
} else {

cli/commands/aws-provider-patch/action.go

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func runAwsProviderPatch(ctx context.Context, opts *options.TerragruntOptions, c
6767

6868
for _, terraformFile := range terraformFilesInModules {
6969
opts.Logger.Debugf("Looking at file %s", terraformFile)
70+
7071
originalTerraformFileContents, err := util.ReadFileAsString(terraformFile)
7172
if err != nil {
7273
return err
@@ -79,6 +80,7 @@ func runAwsProviderPatch(ctx context.Context, opts *options.TerragruntOptions, c
7980

8081
if codeWasUpdated {
8182
opts.Logger.Debugf("Patching AWS provider in %s", terraformFile)
83+
8284
if err := util.WriteFileWithSamePermissions(terraformFile, terraformFile, []byte(updatedTerraformFileContents)); err != nil {
8385
return err
8486
}
@@ -191,6 +193,7 @@ func PatchAwsProviderInTerraformCode(terraformCode string, terraformFilePath str
191193
if err != nil {
192194
return string(hclFile.Bytes()), codeWasUpdated, err
193195
}
196+
194197
codeWasUpdated = codeWasUpdated || attributeOverridden
195198
}
196199
}
@@ -257,12 +260,14 @@ func overrideAttributeInBlock(block *hclwrite.Block, key string, value string) (
257260
// we maintain a mapping of all possible provider configurations (which is unmaintainable). To handle this, we
258261
// assume the user provided input is json, and convert to cty that way.
259262
valueBytes := []byte(value)
263+
260264
ctyType, err := ctyjson.ImpliedType(valueBytes)
261265
if err != nil {
262266
// Wrap error in a custom error type that has better error messaging to the user.
263267
returnErr := TypeInferenceError{value: value, underlyingErr: err}
264268
return false, errors.WithStackTrace(returnErr)
265269
}
270+
266271
ctyVal, err := ctyjson.Unmarshal(valueBytes, ctyType)
267272
if err != nil {
268273
// Wrap error in a custom error type that has better error messaging to the user.
@@ -271,6 +276,7 @@ func overrideAttributeInBlock(block *hclwrite.Block, key string, value string) (
271276
}
272277

273278
body.SetAttributeValue(attr, ctyVal)
279+
274280
return true, nil
275281
}
276282

@@ -317,5 +323,6 @@ func traverseBlock(block *hclwrite.Block, keyParts []string) (*hclwrite.Body, st
317323
}
318324

319325
blockName := keyParts[0]
326+
320327
return traverseBlock(block.Body().FirstMatchingBlock(blockName, nil), keyParts[1:])
321328
}

cli/commands/catalog/module/doc.go

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (regs DocRegs) Replace(str string) string {
4040
for _, reg := range regs {
4141
str = reg.ReplaceAllString(str, "$1")
4242
}
43+
4344
return str
4445
}
4546

@@ -149,6 +150,7 @@ func FindDoc(dir string) (*Doc, error) {
149150
if strings.EqualFold(readmeFile, file.Name()) {
150151
filePath = filepath.Join(dir, file.Name())
151152
fileExt = filepath.Ext(filePath)
153+
152154
break
153155
}
154156
}
@@ -167,6 +169,7 @@ func FindDoc(dir string) (*Doc, error) {
167169
if err != nil {
168170
return nil, errors.WithStackTrace(err)
169171
}
172+
170173
rawContent := string(contentByte)
171174

172175
return NewDoc(rawContent, fileExt), nil
@@ -207,6 +210,7 @@ func (doc *Doc) Description(maxLenght int) string {
207210
}
208211

209212
desc += "."
213+
210214
break
211215
}
212216
}
@@ -265,19 +269,22 @@ func (doc *Doc) parseTag(key docDataKey) string {
265269

266270
if doc.tagCache == nil {
267271
doc.tagCache = make(map[docDataKey]string)
272+
268273
var h1Body, h2Body string
269274

270275
for tagName, tagReg := range doc.tagRegs {
271276
match := tagReg.FindStringSubmatch(doc.rawContent)
272277
if len(match) == 0 {
273278
continue
274279
}
280+
275281
lines := strings.Split(match[1], "\n")
276282

277283
switch tagName {
278284
case tagH1Block:
279285
// header title
280286
doc.tagCache[docTitle] = lines[0]
287+
281288
if len(lines) > 1 {
282289
h1Body = strings.Join(lines[1:], "\n")
283290
}

cli/commands/catalog/module/module.go

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func NewModule(repo *Repo, moduleDir string) (*Module, error) {
4848
if err != nil {
4949
return nil, err
5050
}
51+
5152
module.url = moduleURL
5253

5354
modulePath := filepath.Join(module.repoPath, module.moduleDir)
@@ -56,6 +57,7 @@ func NewModule(repo *Repo, moduleDir string) (*Module, error) {
5657
if err != nil {
5758
return nil, err
5859
}
60+
5961
module.Doc = doc
6062

6163
return module, nil

cli/commands/catalog/module/repo.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func (repo *Repo) FindModules(ctx context.Context) (Modules, error) {
8383
if err != nil {
8484
return err
8585
}
86+
8687
if !remote.IsDir() {
8788
return nil
8889
}
@@ -103,7 +104,6 @@ func (repo *Repo) FindModules(ctx context.Context) (Modules, error) {
103104
if err != nil {
104105
return nil, err
105106
}
106-
107107
}
108108

109109
return modules, nil
@@ -154,6 +154,7 @@ func (repo *Repo) clone(ctx context.Context) error {
154154

155155
log.Debugf("Converting relative path %q to absolute %q", repoPath, absRepoPath)
156156
}
157+
157158
repo.path = repoPath
158159

159160
return nil
@@ -185,6 +186,7 @@ func (repo *Repo) clone(ctx context.Context) error {
185186
if err != nil {
186187
return err
187188
}
189+
188190
repo.cloneURL = sourceUrl.String()
189191

190192
log.Infof("Cloning repository %q to temporary directory %q", repo.cloneURL, repo.path)
@@ -212,10 +214,12 @@ func (repo *Repo) parseRemoteURL() error {
212214
}
213215

214216
var sectionName string
217+
215218
for _, name := range inidata.SectionStrings() {
216219
if !strings.HasPrefix(name, "remote") {
217220
continue
218221
}
222+
219223
sectionName = name
220224

221225
if sectionName == `remote "origin"` {

0 commit comments

Comments
 (0)