diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e333873..62c5487 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,7 +27,7 @@ jobs: - run: go mod download - run: go build -v . - name: Run linters - uses: golangci/golangci-lint-action@971e284b6050e8a5849b72094c50ab08da042db8 # v6.1.1 + uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0 with: version: latest diff --git a/.golangci.yml b/.golangci.yml index 6301ca6..93ba58d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,26 +1,22 @@ # Visit https://golangci-lint.run/ for usage documentation # and information on other useful linters +version: "2" issues: - max-per-linter: 0 max-same-issues: 0 linters: - disable-all: true enable: - durationcheck - errcheck - copyloopvar - forcetypeassert - godot - - gofmt - - gosimple - ineffassign - makezero - misspell - nilerr - predeclared - staticcheck - - tenv - unconvert - unparam - unused diff --git a/internal/provider/hypercore_vm_resource.go b/internal/provider/hypercore_vm_resource.go index 6752229..e9d580f 100644 --- a/internal/provider/hypercore_vm_resource.go +++ b/internal/provider/hypercore_vm_resource.go @@ -640,7 +640,7 @@ func ShutdownVM(ctx context.Context, vmUUID string, restClient *utils.RestClient return err } } - var maxWaitTime int = 300 + var maxWaitTime = 300 envMaxWaitTime := os.Getenv("HC_VM_SHUTDOWN_TIMEOUT") if envMaxWaitTime != "" { val, err := strconv.Atoi(envMaxWaitTime) diff --git a/internal/provider/tests/acceptance/setup/acceptance_test_env_prepare.go b/internal/provider/tests/acceptance/setup/acceptance_test_env_prepare.go index 482a082..7861273 100644 --- a/internal/provider/tests/acceptance/setup/acceptance_test_env_prepare.go +++ b/internal/provider/tests/acceptance/setup/acceptance_test_env_prepare.go @@ -80,7 +80,12 @@ func SendHTTPRequest(client *http.Client, method string, url string, data []byte if err != nil { log.Fatalf("Sending request failed with %v", err) } - defer resp.Body.Close() + + defer func() { + if cerr := resp.Body.Close(); cerr != nil { + err = fmt.Errorf("there was an issue closing response body with: %w", cerr) + } + }() // Read and print the response body, err := io.ReadAll(resp.Body) diff --git a/internal/utils/helper.go b/internal/utils/helper.go index 25057b1..3beea19 100644 --- a/internal/utils/helper.go +++ b/internal/utils/helper.go @@ -238,7 +238,11 @@ func ReadLocalFileBinary(filePath string) ([]byte, error) { if err != nil { return nil, fmt.Errorf("error opening file '%s': %s", filePath, err) } - defer file.Close() + defer func() { + if cerr := file.Close(); cerr != nil && err == nil { + err = fmt.Errorf("error closing file '%s': %w", filePath, cerr) + } + }() reader := bufio.NewReader(file) buffer := make([]byte, 4096) // 4KiB buffer @@ -262,7 +266,11 @@ func FetchFileBinaryFromURL(url string) ([]byte, error) { if err != nil { return nil, err } - defer resp.Body.Close() + defer func() { + if cerr := resp.Body.Close(); cerr != nil { + err = fmt.Errorf("there was an issue closing response body with: %w", cerr) + } + }() var binaryData []byte buffer := make([]byte, 4096) // 4 KiB buffer @@ -334,7 +342,7 @@ func ValidateHTTP(httpUri string, path string) diag.Diagnostic { func RecoverDiagnostics(ctx context.Context, diags *diag.Diagnostics) { if r := recover(); r != nil { - err := fmt.Errorf("Terraform provider got an unexpected error during execution: %v", r) + err := fmt.Errorf("terraform provider got an unexpected error during execution: %v", r) *diags = append(*diags, diag.NewErrorDiagnostic("Unexpected error", err.Error())) } } diff --git a/internal/utils/rest_client.go b/internal/utils/rest_client.go index 7049d57..1395037 100644 --- a/internal/utils/rest_client.go +++ b/internal/utils/rest_client.go @@ -201,7 +201,11 @@ func (rc *RestClient) Login() { if err != nil { panic(fmt.Errorf("couldn't authenticate: %s", err.Error())) } - defer resp.Body.Close() + defer func() { + if cerr := resp.Body.Close(); cerr != nil { + panic(fmt.Errorf("couldn't close response body: %s", cerr.Error())) + } + }() if resp.StatusCode != http.StatusOK { respBytes, _ := io.ReadAll(resp.Body) @@ -237,9 +241,13 @@ func (rc *RestClient) ListRecords(endpoint string, query map[string]any, timeout if err != nil { panic(fmt.Errorf("error making a request: %s", err.Error())) } - defer resp.Body.Close() + defer func() { + if cerr := resp.Body.Close(); cerr != nil { + panic(fmt.Errorf("couldn't close response body: %s", cerr.Error())) + } + }() - if !(resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNoContent) { + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent { panic(fmt.Errorf("unexpected response: %d - %v", resp.StatusCode, rc.ToString(resp))) } @@ -289,7 +297,12 @@ func (rc *RestClient) CreateRecord(endpoint string, payload map[string]any, time if err != nil { return nil, resp.StatusCode, err } - defer resp.Body.Close() + + defer func() { + if cerr := resp.Body.Close(); cerr != nil { + panic(fmt.Errorf("couldn't close response body: %s", cerr.Error())) + } + }() respJson := rc.ToJson(resp) if resp.StatusCode == 400 { @@ -336,7 +349,12 @@ func (rc *RestClient) CreateRecordWithList(endpoint string, payload []map[string if err != nil { return nil, resp.StatusCode, err } - defer resp.Body.Close() + + defer func() { + if cerr := resp.Body.Close(); cerr != nil { + panic(fmt.Errorf("couldn't close response body: %s", cerr.Error())) + } + }() respJson := rc.ToJson(resp) if resp.StatusCode == 400 { @@ -374,7 +392,12 @@ func (rc *RestClient) UpdateRecord(endpoint string, payload map[string]any, time if err != nil { panic(fmt.Errorf("error making a request: %s", err.Error())) } - defer resp.Body.Close() + + defer func() { + if cerr := resp.Body.Close(); cerr != nil { + panic(fmt.Errorf("couldn't close response body: %s", cerr.Error())) + } + }() respJson := rc.ToJson(resp) if resp.StatusCode == 400 { @@ -412,7 +435,12 @@ func (rc *RestClient) PutRecord(endpoint string, payload map[string]any, timeout if err != nil { panic(fmt.Errorf("error making a request: %s", err.Error())) } - defer resp.Body.Close() + + defer func() { + if cerr := resp.Body.Close(); cerr != nil { + panic(fmt.Errorf("couldn't close response body: %s", cerr.Error())) + } + }() respJson := rc.ToJson(resp) if resp.StatusCode == 400 { @@ -451,7 +479,12 @@ func (rc *RestClient) PutBinaryRecord(endpoint string, binaryData []byte, conten if err != nil { panic(fmt.Errorf("error making a request: %s", err.Error())) } - defer resp.Body.Close() + + defer func() { + if cerr := resp.Body.Close(); cerr != nil { + panic(fmt.Errorf("couldn't close response body: %s", cerr.Error())) + } + }() respJson := rc.ToJson(resp) if resp.StatusCode == 400 { @@ -490,7 +523,12 @@ func (rc *RestClient) PutBinaryRecordWithoutTaskTag(endpoint string, binaryData if err != nil { panic(fmt.Errorf("error making a request: %s", err.Error())) } - defer resp.Body.Close() + + defer func() { + if cerr := resp.Body.Close(); cerr != nil { + panic(fmt.Errorf("couldn't close response body: %s", cerr.Error())) + } + }() if resp.StatusCode != 200 { panic(fmt.Errorf("error making a request: got response status code %v", resp.StatusCode)) @@ -518,7 +556,12 @@ func (rc *RestClient) DeleteRecord(endpoint string, timeout float64, ctx context if err != nil { panic(fmt.Errorf("error making a request: %s", err.Error())) } - defer resp.Body.Close() + + defer func() { + if cerr := resp.Body.Close(); cerr != nil { + panic(fmt.Errorf("couldn't close response body: %s", cerr.Error())) + } + }() respJson := rc.ToJson(resp) diff --git a/internal/utils/task_tag.go b/internal/utils/task_tag.go index bd7ac22..ebfef2c 100644 --- a/internal/utils/task_tag.go +++ b/internal/utils/task_tag.go @@ -60,7 +60,7 @@ func (tt *TaskTag) WaitTask(restClient RestClient, ctx context.Context) { panic(fmt.Sprintf("Error executing task: %s, %s", state, taskStatus)) } - if !(state == "RUNNING" || state == "QUEUED") { // TaskTag has finished + if state != "RUNNING" && state != "QUEUED" { // TaskTag has finished return } } diff --git a/internal/utils/vm.go b/internal/utils/vm.go index 292cb5f..6715a87 100644 --- a/internal/utils/vm.go +++ b/internal/utils/vm.go @@ -626,7 +626,7 @@ func GetOneVM(uuid string, restClient RestClient) map[string]any { } if len(records) > 1 { // uuid == "" - panic(fmt.Errorf("Multiple VMs found: uuid=%v", uuid)) + panic(fmt.Errorf("multiple VMs found: uuid=%v", uuid)) } return records[0]