Skip to content

Commit 963a859

Browse files
authored
chore: Upgrades golangci-lint to v2 (#41)
* update version to 2.0.2 * golangci-lint migrate * keep lll with default line length * simplify config * getSpec
1 parent 4dbdcf8 commit 963a859

File tree

5 files changed

+110
-113
lines changed

5 files changed

+110
-113
lines changed

.github/workflows/code-health.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ jobs:
4646
go-version-file: 'go.mod'
4747
cache: false # see https://github.com/golangci/golangci-lint-action/issues/807
4848
- name: golangci-lint
49-
uses: golangci/golangci-lint-action@55c2c1448f86e01eaae002a5a3a9624417608d84
49+
uses: golangci/golangci-lint-action@1481404843c368bc19ca9406f87d6e0fc97bdcfd
5050
with:
51-
version: v1.64.7 # Also update GOLANGCI_VERSION variable in GNUmakefile when updating this version
51+
version: v2.0.2 # Also update GOLANGCI_VERSION variable in GNUmakefile when updating this version
5252
- name: actionlint
5353
run: |
5454
make tools

.golangci.yml

Lines changed: 77 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,96 @@
1-
linters-settings:
2-
gocritic:
3-
enabled-tags:
4-
- diagnostic
5-
- experimental
6-
- opinionated
7-
- performance
8-
- style
9-
govet:
10-
enable-all: true
11-
12-
revive:
13-
# see https://github.com/mgechev/revive#available-rules for details.
14-
ignore-generated-header: true
15-
severity: warning
16-
rules:
17-
- name: blank-imports
18-
- name: context-as-argument
19-
- name: context-keys-type
20-
- name: dot-imports
21-
- name: error-return
22-
- name: error-strings
23-
- name: error-naming
24-
- name: errorf
25-
- name: exported
26-
- name: indent-error-flow
27-
- name: if-return
28-
- name: increment-decrement
29-
- name: var-naming
30-
- name: var-declaration
31-
- name: package-comments
32-
- name: range
33-
- name: receiver-naming
34-
- name: time-naming
35-
- name: unexported-return
36-
- name: indent-error-flow
37-
- name: errorf
38-
- name: empty-block
39-
- name: superfluous-else
40-
- name: struct-tag
41-
# Too many unusued parameters, skipping this check for now
42-
#- name: unused-parameter
43-
- name: unreachable-code
44-
- name: redefines-builtin-id
45-
misspell:
46-
locale: US
47-
ignore-words:
48-
- cancelled
49-
lll:
50-
# Default is 120. '\t' is counted as 1 character.
51-
# set our project to 500, as we are adding open api field description in the schema.
52-
# also, for anyone using vscode, use the following configs:
53-
# "rewrap.wrappingColumn": 500 ... requires the rewrap plugin
54-
# "editor.rulers": [500]
55-
line-length: 500
56-
nestif:
57-
# minimal complexity of if statements to report, 5 by default
58-
min-complexity: 7
59-
mnd:
60-
checks:
61-
- case
62-
- operation
63-
- return
64-
funlen:
65-
lines: 360
66-
statements: 120
1+
version: "2"
2+
run:
3+
modules-download-mode: readonly
4+
tests: true
675
linters:
68-
disable-all: true
6+
default: none
697
enable:
8+
- copyloopvar
709
- dogsled
7110
- errcheck
11+
- exhaustive
7212
- funlen
7313
- gocritic
74-
- gofmt
75-
- goimports
76-
- revive
77-
- mnd
7814
- goprintffuncname
7915
- gosec
80-
- gosimple
8116
- govet
8217
- ineffassign
8318
- lll
19+
- makezero
8420
- misspell
21+
- mnd
8522
- nakedret
23+
- noctx
8624
- nolintlint
25+
- revive
8726
- rowserrcheck
88-
- copyloopvar
8927
- staticcheck
90-
- stylecheck
91-
- typecheck
92-
- unconvert
93-
- unused
94-
- whitespace
95-
- thelper
9628
- testifylint
97-
- exhaustive
98-
- makezero
99-
- noctx
10029
- testpackage
30+
- thelper
31+
- unconvert
32+
- unused
10133
- usetesting
102-
run:
103-
timeout: 10m
104-
tests: true
105-
build-tags:
106-
- integration
107-
modules-download-mode: readonly
34+
- whitespace
35+
settings:
36+
funlen:
37+
lines: 360
38+
statements: 120
39+
gocritic:
40+
enabled-tags:
41+
- diagnostic
42+
- experimental
43+
- opinionated
44+
- performance
45+
- style
46+
govet:
47+
enable-all: true
48+
misspell:
49+
locale: US
50+
mnd:
51+
checks:
52+
- case
53+
- operation
54+
- return
55+
nestif:
56+
min-complexity: 7
57+
revive:
58+
severity: warning
59+
rules:
60+
- name: blank-imports
61+
- name: context-as-argument
62+
- name: context-keys-type
63+
- name: dot-imports
64+
- name: error-return
65+
- name: error-strings
66+
- name: error-naming
67+
- name: errorf
68+
- name: exported
69+
- name: indent-error-flow
70+
- name: if-return
71+
- name: increment-decrement
72+
- name: var-naming
73+
- name: var-declaration
74+
- name: package-comments
75+
- name: range
76+
- name: receiver-naming
77+
- name: time-naming
78+
- name: unexported-return
79+
- name: indent-error-flow
80+
- name: errorf
81+
- name: empty-block
82+
- name: superfluous-else
83+
- name: struct-tag
84+
- name: unreachable-code
85+
- name: redefines-builtin-id
86+
exclusions:
87+
generated: lax
88+
presets:
89+
- comments
90+
- common-false-positives
91+
- legacy
92+
- std-error-handling
93+
formatters:
94+
enable:
95+
- gofmt
96+
- goimports

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CLI_DESTINATION=./bin/$(CLI_BINARY_NAME)
44
MANIFEST_FILE?=./bin/manifest.yml
55
WIN_MANIFEST_FILE?=./bin/manifest.windows.yml
66

7-
GOLANGCI_VERSION=v1.64.7 # Also update golangci-lint GH action in code-health.yml when updating this version
7+
GOLANGCI_VERSION=v2.0.2 # Also update golangci-lint GH action in code-health.yml when updating this version
88

99
.PHONY: build
1010
build: ## Generate the binary in ./bin

internal/cli/clu2adv/clu2adv.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
func Builder() *cobra.Command {
1010
o := &opts{fs: afero.NewOsFs()}
1111
cmd := &cobra.Command{
12-
Use: "clusterToAdvancedCluster",
13-
Short: "Convert cluster to advanced_cluster preview provider 2.0.0",
14-
Long: "Convert a Terraform configuration from mongodbatlas_cluster to mongodbatlas_advanced_cluster preview provider 2.0.0",
12+
Use: "clusterToAdvancedCluster",
13+
Short: "Convert cluster to advanced_cluster preview provider 2.0.0",
14+
Long: "Convert a Terraform configuration from mongodbatlas_cluster to " +
15+
"mongodbatlas_advanced_cluster preview provider 2.0.0",
1516
Aliases: []string{"clu2adv"},
1617
RunE: func(_ *cobra.Command, _ []string) error {
1718
if err := o.PreRun(); err != nil {
@@ -24,8 +25,11 @@ func Builder() *cobra.Command {
2425
_ = cmd.MarkFlagRequired(flag.File)
2526
cmd.Flags().StringVarP(&o.output, flag.Output, flag.OutputShort, "", "output file")
2627
_ = cmd.MarkFlagRequired(flag.Output)
27-
cmd.Flags().BoolVarP(&o.replaceOutput, flag.ReplaceOutput, flag.ReplaceOutputShort, false, "replace output file if exists")
28-
cmd.Flags().BoolVarP(&o.watch, flag.Watch, flag.WatchShort, false, "keeps the plugin running and watches the input file for changes")
29-
cmd.Flags().BoolVarP(&o.includeMoved, flag.IncludeMoved, flag.IncludeMovedShort, false, "include moved blocks in the output file")
28+
cmd.Flags().BoolVarP(&o.replaceOutput, flag.ReplaceOutput, flag.ReplaceOutputShort, false,
29+
"replace output file if exists")
30+
cmd.Flags().BoolVarP(&o.watch, flag.Watch, flag.WatchShort, false,
31+
"keeps the plugin running and watches the input file for changes")
32+
cmd.Flags().BoolVarP(&o.includeMoved, flag.IncludeMoved, flag.IncludeMovedShort, false,
33+
"include moved blocks in the output file")
3034
return cmd
3135
}

internal/convert/convert.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ type attrVals struct {
4848
// ClusterToAdvancedCluster transforms all mongodbatlas_cluster definitions in a
4949
// Terraform configuration file into mongodbatlas_advanced_cluster schema 2.0.0.
5050
// All other resources and data sources are left untouched.
51-
// Note: hclwrite.Tokens are used instead of cty.Value so expressions with interpolations like var.region can be preserved.
51+
// Note: hclwrite.Tokens are used instead of cty.Value so expressions with
52+
// interpolations like var.region can be preserved.
5253
// cty.Value only supports literal expressions.
5354
func ClusterToAdvancedCluster(config []byte, includeMoved bool) ([]byte, error) {
5455
var moveLabels []string
@@ -216,7 +217,7 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
216217
break
217218
}
218219
specbSrc := specSrc.Body()
219-
d, err := fillReplicationSpecsWithDynamicRegionConfigs(specbSrc, root, false)
220+
d, err := fillWithDynamicRegionConfigs(specbSrc, root, false)
220221
if err != nil {
221222
return err
222223
}
@@ -333,7 +334,7 @@ func fillReplicationSpecsWithDynamicBlock(resourceb *hclwrite.Body, root attrVal
333334
return dynamicBlock{}, err
334335
}
335336
transformDynamicBlockReferences(dSpec.content.Body(), nRepSpecs, nSpec)
336-
dConfig, err := fillReplicationSpecsWithDynamicRegionConfigs(dSpec.content.Body(), root, true)
337+
dConfig, err := fillWithDynamicRegionConfigs(dSpec.content.Body(), root, true)
337338
if err != nil {
338339
return dynamicBlock{}, err
339340
}
@@ -344,8 +345,8 @@ func fillReplicationSpecsWithDynamicBlock(resourceb *hclwrite.Body, root attrVal
344345
return dSpec, nil
345346
}
346347

347-
// fillReplicationSpecsWithDynamicRegionConfigs is used for dynamic blocks in region_configs
348-
func fillReplicationSpecsWithDynamicRegionConfigs(specbSrc *hclwrite.Body, root attrVals, transformRegionReferences bool) (dynamicBlock, error) {
348+
// fillWithDynamicRegionConfigs is used for dynamic blocks in region_configs
349+
func fillWithDynamicRegionConfigs(specbSrc *hclwrite.Body, root attrVals, changeReferences bool) (dynamicBlock, error) {
349350
d, err := getDynamicBlock(specbSrc, nConfigSrc)
350351
if err != nil || !d.IsPresent() {
351352
return dynamicBlock{}, err
@@ -356,15 +357,16 @@ func fillReplicationSpecsWithDynamicRegionConfigs(specbSrc *hclwrite.Body, root
356357
repSpecb.SetAttributeRaw(nZoneName, hcl.TokensFromExpr(zoneName))
357358
}
358359
forEach := hcl.GetAttrExpr(d.forEach)
359-
if transformRegionReferences {
360+
if changeReferences {
360361
forEach = replaceDynamicBlockReferences(forEach, nRepSpecs, nSpec)
361362
}
362-
regionFor, err := getDynamicBlockRegionConfigsRegionArray(forEach, d.content, root)
363+
regionFor, err := getDynamicBlockRegionArray(forEach, d.content, root)
363364
if err != nil {
364365
return dynamicBlock{}, err
365366
}
367+
priorityForStr := fmt.Sprintf("for %s in range(%d, %d, -1) : ", nPriority, valMaxPriority, valMinPriority)
366368
priorityFor := hcl.TokensComment(commentPriorityFor)
367-
priorityFor = append(priorityFor, hcl.TokensFromExpr(fmt.Sprintf("for %s in range(%d, %d, -1) : ", nPriority, valMaxPriority, valMinPriority))...)
369+
priorityFor = append(priorityFor, hcl.TokensFromExpr(priorityForStr)...)
368370
priorityFor = append(priorityFor, regionFor...)
369371
repSpecb.SetAttributeRaw(nConfig, hcl.TokensFuncFlatten(priorityFor))
370372

@@ -410,13 +412,13 @@ func getRegionConfig(configSrc *hclwrite.Block, root attrVals, isDynamicBlock bo
410412
if err := hcl.MoveAttr(configSrc.Body(), fileb, nPriority, nPriority, errRepSpecs); err != nil {
411413
return nil, err
412414
}
413-
if electable, _ := getSpecs(configSrc, nElectableNodes, root, isDynamicBlock); electable != nil {
415+
if electable, _ := getSpec(configSrc, nElectableNodes, root, isDynamicBlock); electable != nil {
414416
fileb.SetAttributeRaw(nElectableSpecs, electable)
415417
}
416-
if readOnly, _ := getSpecs(configSrc, nReadOnlyNodes, root, isDynamicBlock); readOnly != nil {
418+
if readOnly, _ := getSpec(configSrc, nReadOnlyNodes, root, isDynamicBlock); readOnly != nil {
417419
fileb.SetAttributeRaw(nReadOnlySpecs, readOnly)
418420
}
419-
if analytics, _ := getSpecs(configSrc, nAnalyticsNodes, root, isDynamicBlock); analytics != nil {
421+
if analytics, _ := getSpec(configSrc, nAnalyticsNodes, root, isDynamicBlock); analytics != nil {
420422
fileb.SetAttributeRaw(nAnalyticsSpecs, analytics)
421423
}
422424
if autoScaling := getAutoScalingOpt(root.opt); autoScaling != nil {
@@ -425,7 +427,7 @@ func getRegionConfig(configSrc *hclwrite.Block, root attrVals, isDynamicBlock bo
425427
return file, nil
426428
}
427429

428-
func getSpecs(configSrc *hclwrite.Block, countName string, root attrVals, isDynamicBlock bool) (hclwrite.Tokens, error) {
430+
func getSpec(configSrc *hclwrite.Block, countName string, root attrVals, isDynamicBlock bool) (hclwrite.Tokens, error) {
429431
var (
430432
file = hclwrite.NewEmptyFile()
431433
fileb = file.Body()
@@ -556,9 +558,9 @@ func replaceDynamicBlockExpr(attr *hclwrite.Attribute, blockName, attrName strin
556558
return strings.ReplaceAll(expr, fmt.Sprintf("%s.%s", blockName, attrName), attrName)
557559
}
558560

559-
// getDynamicBlockRegionConfigsRegionArray returns the region array for a dynamic block in replication_specs.
561+
// getDynamicBlockRegionArray returns the region array for a dynamic block in replication_specs.
560562
// e.g. [ for region in var.replication_specs.regions_config : { ... } if priority == region.priority ]
561-
func getDynamicBlockRegionConfigsRegionArray(forEach string, configSrc *hclwrite.Block, root attrVals) (hclwrite.Tokens, error) {
563+
func getDynamicBlockRegionArray(forEach string, configSrc *hclwrite.Block, root attrVals) (hclwrite.Tokens, error) {
562564
transformDynamicBlockReferences(configSrc.Body(), nConfigSrc, nRegion)
563565
priorityStr := hcl.GetAttrExpr(configSrc.Body().GetAttribute(nPriority))
564566
if priorityStr == "" {
@@ -581,7 +583,8 @@ func transformDynamicBlockReferences(configSrcb *hclwrite.Body, blockName, varNa
581583
}
582584
}
583585

584-
// replaceDynamicBlockReferences changes value references, e.g. regions_config.value.electable_nodes to region.electable_nodes
586+
// replaceDynamicBlockReferences changes value references,
587+
// e.g. regions_config.value.electable_nodes to region.electable_nodes
585588
func replaceDynamicBlockReferences(expr, blockName, varName string) string {
586589
return strings.ReplaceAll(expr,
587590
fmt.Sprintf("%s.%s.", blockName, nValue),
@@ -606,7 +609,8 @@ func setKeyValue(body *hclwrite.Body, key, value *hclwrite.Attribute) {
606609
keyStr, err := hcl.GetAttrString(key, "")
607610
if err == nil {
608611
if !hclsyntax.ValidIdentifier(keyStr) {
609-
keyStr = strconv.Quote(keyStr) // wrap in quotes so invalid identifiers (e.g. with blanks) can be used as attribute names
612+
// wrap in quotes so invalid identifiers (e.g. with blanks) can be used as attribute names
613+
keyStr = strconv.Quote(keyStr)
610614
}
611615
} else {
612616
keyStr = strings.TrimSpace(string(key.Expr().BuildTokens(nil).Bytes()))

0 commit comments

Comments
 (0)