From 6901ffccf2eb4d24d209dd030e3e5a4209ed2413 Mon Sep 17 00:00:00 2001 From: Hector Valcarcel Date: Sat, 2 Nov 2024 00:22:21 +0100 Subject: [PATCH] Chore: Apply fixes from MegaLinter reports --- .linters/.checkov.yml | 3 + .linters/.cspell.json | 24 ++++++++ .linters/.golangci.yml | 31 +++++++++++ .linters/.jscpd.json | 20 +++++++ .linters/.markdown-link-check.json | 8 +++ .linters/.markdownlint.yml | 30 ++++++++++ .linters/.revive.toml | 31 +++++++++++ .linters/.yamllint.yml | 18 ++++++ .linters/project-dict.txt | 43 +++++++++++++++ .mega-linter.yml | 55 +++++++++++++++++++ README.md | 13 ++--- .../provider/object_set_value_function.go | 4 +- .../object_set_value_function_test.go | 13 +++-- internal/provider/provider.go | 6 +- internal/provider/provider_test.go | 16 ------ main.go | 2 +- 16 files changed, 282 insertions(+), 35 deletions(-) create mode 100644 .linters/.checkov.yml create mode 100644 .linters/.cspell.json create mode 100644 .linters/.golangci.yml create mode 100644 .linters/.jscpd.json create mode 100644 .linters/.markdown-link-check.json create mode 100644 .linters/.markdownlint.yml create mode 100644 .linters/.revive.toml create mode 100644 .linters/.yamllint.yml create mode 100644 .linters/project-dict.txt create mode 100644 .mega-linter.yml diff --git a/.linters/.checkov.yml b/.linters/.checkov.yml new file mode 100644 index 0000000..b889819 --- /dev/null +++ b/.linters/.checkov.yml @@ -0,0 +1,3 @@ +compact: true # For the CLI output, do not display code blocks +quiet: true # For the CLI output, display only failed checks. Also disables progress bars +evaluate-variables: true diff --git a/.linters/.cspell.json b/.linters/.cspell.json new file mode 100644 index 0000000..bbec04f --- /dev/null +++ b/.linters/.cspell.json @@ -0,0 +1,24 @@ +{ + "language": "en", + "noConfigSearch": true, + "words": ["megalinter", "oxsecurity"], + "dictionaries": ["project-dict", "go", "filetypes", "softwareTerms", "misc"], + "dictionaryDefinitions": [ + { + "name": "project-dict", + "path": "./project-dict.txt", + "useCompounds": true + } + ], + "ignorePaths": [ + "**/node_modules/**", + "**/vscode-extension/**", + "**/.git/**", + "**/.pnpm-lock.json", + ".vscode", + "megalinter", + "package-lock.json", + "report", + ".ml-reports/**" + ] +} diff --git a/.linters/.golangci.yml b/.linters/.golangci.yml new file mode 100644 index 0000000..d5e4906 --- /dev/null +++ b/.linters/.golangci.yml @@ -0,0 +1,31 @@ +# Visit https://golangci-lint.run/ for usage documentation +# and information on other useful linters +issues: + max-issues-per-linter: 30 + max-same-issues: 0 + exclude-rules: + - path: '(.+)_test\.go' + linters: + - unparam + - forcetypeassert + +linters: + disable-all: true + enable: + - durationcheck + - errcheck + - forcetypeassert + - godot + - gofmt + - gosimple + - govet + - ineffassign + - makezero + - misspell + - nilerr + - predeclared + - staticcheck + - tenv + - unconvert + - unparam + - unused diff --git a/.linters/.jscpd.json b/.linters/.jscpd.json new file mode 100644 index 0000000..b3308b2 --- /dev/null +++ b/.linters/.jscpd.json @@ -0,0 +1,20 @@ +{ + "threshold": 20, + "minLines": 15, + "reporters": ["html", "markdown"], + "ignore": [ + "**/node_modules/**", + "**/.git/**", + "**/.rbenv/**", + "**/.venv/**", + "**/*cache*/**", + "**/.github/**", + "**/.idea/**", + "**/report/**", + "**/.ml-reports/**", + "**/*.svg", + "**/*_test.go", + "./examples/**", + "./docs/**" + ] +} diff --git a/.linters/.markdown-link-check.json b/.linters/.markdown-link-check.json new file mode 100644 index 0000000..2cdbe3f --- /dev/null +++ b/.linters/.markdown-link-check.json @@ -0,0 +1,8 @@ +{ + "ignorePatterns": [ + { "pattern": "^#.+" } + ], + "retryOn429": true, + "retryCount": 3, + "aliveStatusCodes": [200, 203] +} diff --git a/.linters/.markdownlint.yml b/.linters/.markdownlint.yml new file mode 100644 index 0000000..5c3a08c --- /dev/null +++ b/.linters/.markdownlint.yml @@ -0,0 +1,30 @@ +# Default state for all rules +default: true + +# MD007/ul-indent - Unordered list indentation +MD007: + # Spaces for indent + indent: 2 + +# MD013/line-length - Line length +MD013: + line_length: 120 + tables: false + code_blocks: false + +# MD026/no-trailing-punctuation - Trailing punctuation in heading +MD026: + # Punctuation characters + punctuation: ".,;:!。,;:!" + +# MD029/ol-prefix - Ordered list item prefix +MD029: false + +# MD033/no-inline-html - Inline HTML +MD033: false + +# MD036/no-emphasis-as-heading/no-emphasis-as-header - Emphasis used instead of a heading +MD036: false + +# MD041/first-line-heading/first-line-h1 - First line in a file should be a top-level heading +MD041: false diff --git a/.linters/.revive.toml b/.linters/.revive.toml new file mode 100644 index 0000000..804e32e --- /dev/null +++ b/.linters/.revive.toml @@ -0,0 +1,31 @@ +ignoreGeneratedHeader = false +severity = "warning" +confidence = 0.8 +errorCode = 0 +warningCode = 0 + +[rule.blank-imports] +[rule.context-as-argument] +[rule.context-keys-type] +[rule.dot-imports] +[rule.error-return] +[rule.error-strings] +[rule.error-naming] +[rule.increment-decrement] +[rule.var-naming] +[rule.var-declaration] +[rule.range] +[rule.receiver-naming] +[rule.time-naming] +[rule.unexported-return] +[rule.indent-error-flow] +[rule.errorf] +[rule.empty-block] +[rule.superfluous-else] +[rule.unused-parameter] +[rule.unreachable-code] +[rule.redefines-builtin-id] + +# disabled from the recommended rules +# [rule.package-comments] +# [rule.exported] diff --git a/.linters/.yamllint.yml b/.linters/.yamllint.yml new file mode 100644 index 0000000..0163cae --- /dev/null +++ b/.linters/.yamllint.yml @@ -0,0 +1,18 @@ +extends: default +rules: + empty-lines: + max: 1 + max-start: 0 + max-end: 0 + new-lines: + level: warning + type: unix + line-length: + max: 150 + comments: + min-spaces-from-content: 1 + document-start: false + document-end: false + new-line-at-end-of-file: enable + truthy: + check-keys: false diff --git a/.linters/project-dict.txt b/.linters/project-dict.txt new file mode 100644 index 0000000..9d73001 --- /dev/null +++ b/.linters/project-dict.txt @@ -0,0 +1,43 @@ +basetypes +coverprofile +datasource +durationcheck +errcheck +exportloopref +forcetypeassert +goarch +gofmt +GOLANGCI +golangci +gomod +goreleaser +gosimple +govet +ineffassign +inventium +knownvalue +ldflags +makezero +MARKDOWNLINT +markdownlint +megalinter +nilerr +oxsecurity +predeclared +providerserver +refucktor +sonarcloud +sonarsource +statecheck +staticcheck +stringvalidator +tenv +terraformterraform +tflog +tfplugindocs +tfprotov +tfversion +trimpath +unconvert +unparam +venv diff --git a/.mega-linter.yml b/.mega-linter.yml new file mode 100644 index 0000000..3b103bc --- /dev/null +++ b/.mega-linter.yml @@ -0,0 +1,55 @@ +# Configuration file for MegaLinter +# +# See all available variables at https://megalinter.io/latest/config-file/ and in linters documentation + +# all, none, or list of linter keys +APPLY_FIXES: # all, none, or list of linter keys + - JSON_PRETTIER + - YAML_YAMLLINT +ENABLE_LINTERS: # If you use ENABLE_LINTERS variable, all other linters will be disabled by default + - COPYPASTE_JSCPD # https://megalinter.io/latest/descriptors/copypaste_jscpd/ + # - GO_GOLANGCI_LINT # https://megalinter.io/latest/descriptors/go_golangci_lint/ # missing support for Go 1.23 + - GO_REVIVE # https://megalinter.io/latest/descriptors/go_revive/ + - JSON_PRETTIER # https://megalinter.io/latest/descriptors/json_prettier/ + - MARKDOWN_MARKDOWN_LINK_CHECK # https://megalinter.io/latest/descriptors/markdown_markdown_link_check/ + - MARKDOWN_MARKDOWNLINT # https://megalinter.io/latest/descriptors/markdown_markdownlint/ + - REPOSITORY_CHECKOV # https://megalinter.io/latest/descriptors/repository_checkov/ + - SPELL_CSPELL # https://megalinter.io/latest/descriptors/spell_cspell/ + - YAML_YAMLLINT # https://megalinter.io/latest/descriptors/yaml_yamllint/ + +CLEAR_REPORT_FOLDER: true +CONFIG_PROPERTIES_TO_APPEND: + - ADDITIONAL_EXCLUDED_DIRECTORIES +CONFIG_REPORTER: false +FAIL_IF_MISSING_LINTER_IN_FLAVOR: true +FORMATTERS_DISABLE_ERRORS: false +IGNORE_GENERATED_FILES: true +LINTER_RULES_PATH: .linters +PRINT_ALPACA: false +REPORT_OUTPUT_FOLDER: /tmp/lint/.ml-reports +SHOW_ELAPSED_TIME: true +UPDATED_SOURCES_REPORTER: false + +ADDITIONAL_EXCLUDED_DIRECTORIES: + - '.archived' + - 'docs' + - 'examples' + - '.git' + - '.linters' + - '.vscode' + - '.idea' + +# MARKDOWN_MARKDOWNLINT variables +MARKDOWN_MARKDOWNLINT_CONFIG_FILE: .markdownlint.yml + +# YAML_YAMLLINT variables +YAML_YAMLLINT_CONFIG_FILE: .yamllint.yml + +# JSON_PRETTIER variables +JSON_PRETTIER_CONFIG_FILE: .prettierrc.yml + +# GO_REVIVE +GO_REVIVE_CONFIG_FILE: .revive.toml + +# GO_GOLANGCI_LINT +GO_GOLANGCI_LINT_CONFIG_FILE: .golangci.yml diff --git a/README.md b/README.md index 5039413..8aab3c7 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,9 @@ ![Golang][shield-golang] ![Terraform][shield-terraform] -![🛠️ Build Workflow][badge-gh-action-build][link-gh-action-build] -![🔎 MegaLinter][badge-gh-action-megalinter][link-gh-action-megalinter] -![❇️ CodeQL][badge-gh-action-codeql][link-gh-action-codeql] +[![🛠️ Build Workflow][badge-gh-action-build]][link-gh-action-build] +[![🔎 MegaLinter][badge-gh-action-megalinter]][link-gh-action-megalinter] +[![❇️ CodeQL][badge-gh-action-codeql]][link-gh-action-codeql] ![GitHub language count][shield-lang-count] ![GitHub Actions Workflow Status][shield-gh-action-status] @@ -25,15 +25,15 @@ # 🧰 Terraform Provider: Helpers Functions -This is a Terraform Provider only for Helper functions. The main idea is to extend the built-in functionalities of +This is a Terraform Provider only for Helper functions. The main idea is to extend the built-in functionalities of Terraform with some functions that are not available by default and can be useful in some scenarios. -For a detailed examples and documentation check inside the [docs](./docs/index.md) folder or directly in the +For a detailed examples and documentation check inside the [docs](./docs/index.md) folder or directly in the Terraform Registry. ## Available Functions -- [object_set_value](./docs/functions/object_set_value.md) +* [object_set_value](./docs/functions/object_set_value.md) ## Example Usage @@ -68,7 +68,6 @@ output "write_all_operation" { style="height: 60px !important;width: 217px !important;"> - [shield-golang]: [shield-terraform]: diff --git a/internal/provider/object_set_value_function.go b/internal/provider/object_set_value_function.go index 4c20b69..f437c04 100644 --- a/internal/provider/object_set_value_function.go +++ b/internal/provider/object_set_value_function.go @@ -17,11 +17,11 @@ func NewObjectSetValueFunction() function.Function { return &ObjectSetValueFunction{} } -func (c ObjectSetValueFunction) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { +func (c ObjectSetValueFunction) Metadata(_ context.Context, _ function.MetadataRequest, resp *function.MetadataResponse) { resp.Name = "object_set_value" } -func (c ObjectSetValueFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) { +func (c ObjectSetValueFunction) Definition(_ context.Context, _ function.DefinitionRequest, resp *function.DefinitionResponse) { resp.Definition = function.Definition{ Summary: "Sets a value in an Object or creates a new key with the value", Parameters: []function.Parameter{ diff --git a/internal/provider/object_set_value_function_test.go b/internal/provider/object_set_value_function_test.go index 7bb70d0..51bf451 100644 --- a/internal/provider/object_set_value_function_test.go +++ b/internal/provider/object_set_value_function_test.go @@ -1,7 +1,6 @@ package provider import ( - "fmt" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/knownvalue" "github.com/hashicorp/terraform-plugin-testing/statecheck" @@ -21,7 +20,7 @@ func (m mockTFObjectPartial) modify(t *testing.T, key string, value knownvalue.C return result } -func TestObjectSetValueFunction_Run(t *testing.T) { +func TestObjectSetValueFunction(t *testing.T) { t.Parallel() mockObject := mockTFObjectPartial{ @@ -31,7 +30,9 @@ func TestObjectSetValueFunction_Run(t *testing.T) { "key4": knownvalue.StringExact(""), "key5": knownvalue.Null(), } - mockObjectString := `{ key1 = "value1", key2 = true, key3 = 3, key4 = "", key5 = null }` + mockTerraformLocalsTestObject := `locals { + test_object = { key1 = "value1", key2 = true, key3 = 3, key4 = "", key5 = null } + ` resource.UnitTest(t, resource.TestCase{ TerraformVersionChecks: []tfversion.TerraformVersionCheck{ @@ -42,7 +43,7 @@ func TestObjectSetValueFunction_Run(t *testing.T) { { // test "write_all" operation mode Config: `` + - fmt.Sprintf("locals { test_object = %s }\n", mockObjectString) + ` + mockTerraformLocalsTestObject + ` output "expect_value_change" { value = provider::helpers::object_set_value(local.test_object, "key1", "new_value", "write_all") } @@ -58,7 +59,7 @@ func TestObjectSetValueFunction_Run(t *testing.T) { { // test "write_value" operation mode Config: `` + - fmt.Sprintf("locals { test_object = %s }\n", mockObjectString) + ` + mockTerraformLocalsTestObject + ` output "expect_value_change" { value = provider::helpers::object_set_value(local.test_object, "key1", "new_value", "write_value") } @@ -74,7 +75,7 @@ func TestObjectSetValueFunction_Run(t *testing.T) { { // test "write_safe" operation mode Config: `` + - fmt.Sprintf("locals { test_object = %s }\n", mockObjectString) + ` + mockTerraformLocalsTestObject + ` output "expect_value_change_1" { value = provider::helpers::object_set_value(local.test_object, "key4", "new_value", "write_safe") } diff --git a/internal/provider/provider.go b/internal/provider/provider.go index ed39dd1..734a643 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -20,12 +20,12 @@ type HelpersProvider struct { version string } -func (h *HelpersProvider) Metadata(ctx context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) { +func (h *HelpersProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) { resp.TypeName = "helpers" resp.Version = h.version } -func (h *HelpersProvider) Schema(ctx context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) { +func (h *HelpersProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) { resp.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{}, Description: "The Helpers Provides offers a set of functions to help with common tasks.", @@ -46,7 +46,7 @@ func (h *HelpersProvider) Resources(ctx context.Context) []func() resource.Resou return nil } -func (h *HelpersProvider) Functions(ctx context.Context) []func() function.Function { +func (h *HelpersProvider) Functions(_ context.Context) []func() function.Function { return []func() function.Function{ NewObjectSetValueFunction, } diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index 03dd60b..b265fef 100644 --- a/internal/provider/provider_test.go +++ b/internal/provider/provider_test.go @@ -1,7 +1,6 @@ package provider import ( - "fmt" "github.com/hashicorp/terraform-plugin-framework/providerserver" "github.com/hashicorp/terraform-plugin-go/tfprotov6" ) @@ -13,18 +12,3 @@ import ( var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){ "helpers": providerserver.NewProtocol6WithError(NewProvider("test")()), } - -func testWrap() { - x := map[string]interface{}{ - "keyOne": "valueOne", - "keyTwo": 2, - "keyThree": true, - } - var qwe []interface{} - - for key, value := range x { - qwe = append(qwe, fmt.Sprintf("%v = %v", key, value)) - } - str := fmt.Sprintf("%v", x) - fmt.Println(str) -} diff --git a/main.go b/main.go index 3db79cb..f0b1305 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ import ( var ( // these will be set by the goreleaser configuration // to appropriate values for the compiled binary. - version string = "dev" + version = "dev" // goreleaser can pass other information to the main package, such as the specific commit // https://goreleaser.com/cookbooks/using-main.version/