Skip to content

Commit c8c6aba

Browse files
Abhijeet Vabhijeetviswa
authored andcommitted
fix errcheck lint errors and run it as part of pr checks
1 parent 5f1a809 commit c8c6aba

34 files changed

+162
-147
lines changed

.github/workflows/go-getter.yml

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ jobs:
2222
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
2323
with:
2424
go-version: ${{ matrix.go-version }}
25-
25+
2626
- name: Checkout code
2727
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
2828

2929
- name: Create test directory
3030
run: |
3131
mkdir -p ${{ env.TEST_RESULTS_PATH }}
32-
32+
3333
- name: Setup cache for go modules
3434
uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
3535
with:
@@ -39,10 +39,10 @@ jobs:
3939
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
4040
restore-keys: |
4141
${{ runner.os }}-go-
42-
42+
4343
- name: Download go modules
4444
run: go mod download
45-
45+
4646
# Check go fmt output because it does not report non-zero when there are fmt changes
4747
- name: Run gofmt
4848
run: |
@@ -53,7 +53,7 @@ jobs:
5353
echo "$files"
5454
exit 1
5555
fi
56-
56+
5757
- name: Install gotestsum
5858
run: go install gotest.tools/[email protected]
5959

@@ -71,14 +71,14 @@ jobs:
7171
workload_identity_provider: 'projects/328212837253/locations/global/workloadIdentityPools/hc-go-getter-test/providers/github-hc-go-getter-test'
7272
service_account: hc-go-getter-test@hc-e56c0f7c21c448d2be9e7696073.iam.gserviceaccount.com
7373
audience: https://github.com/hashicorp
74-
74+
7575
- name: Run go tests
7676
run: |
7777
PACKAGE_NAMES=$(go list ./...)
7878
echo "Running $(echo $PACKAGE_NAMES | wc -w) packages"
7979
echo $PACKAGE_NAMES
8080
gotestsum --format=short-verbose --junitfile $TEST_RESULTS_PATH/go-getter/gotestsum-report.xml -- -p 2 -cover -coverprofile=linux_cov.part $PACKAGE_NAMES
81-
81+
8282
# Save coverage report parts
8383
- name: Upload and save artifacts
8484
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808
@@ -104,7 +104,7 @@ jobs:
104104
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
105105
with:
106106
go-version: ${{ matrix.go-version }}
107-
107+
108108
- name: Checkout code
109109
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
110110

@@ -120,11 +120,11 @@ jobs:
120120
121121
- name: Download go modules
122122
run: go mod download
123-
123+
124124
- name: Install gotestsum
125125
shell: bash
126126
run: go install gotest.tools/[email protected]
127-
127+
128128
- name: Configure AWS Credentials
129129
uses: aws-actions/configure-aws-credentials@8c3f20df09ac63af7b3ae3d7c91f105f857d8497 # v4.0.0
130130
with:
@@ -139,18 +139,28 @@ jobs:
139139
workload_identity_provider: 'projects/328212837253/locations/global/workloadIdentityPools/hc-go-getter-test/providers/github-hc-go-getter-test'
140140
service_account: hc-go-getter-test@hc-e56c0f7c21c448d2be9e7696073.iam.gserviceaccount.com
141141
audience: https://github.com/hashicorp
142-
142+
143143
- name: Run go tests
144144
shell: bash
145145
run: |
146146
PACKAGE_NAMES=$(go list ./...)
147147
echo "Running $(echo $PACKAGE_NAMES | wc -w) packages"
148148
echo $PACKAGE_NAMES
149149
gotestsum --format=short-verbose --junitfile $TEST_RESULTS_PATH/go-getter/gotestsum-report.xml -- -p 2 -cover -race -coverprofile=win_cov.part $PACKAGE_NAMES
150-
150+
151151
# Save coverage report parts
152152
- name: Upload and save artifacts
153153
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808
154154
with:
155155
name: windows-test-results-${{ matrix.go-version }}
156-
path: win_cov.part
156+
path: win_cov.part
157+
158+
linter:
159+
runs-on: ubuntu-latest
160+
steps:
161+
- name: Checkout code
162+
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
163+
- name: Lint code
164+
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
165+
with:
166+
args: --enable-only errcheck # this is temporary until the other lint errors are fixed

checksum.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (c *FileChecksum) checksum(source string) error {
5858
if err != nil {
5959
return fmt.Errorf("Failed to open file for checksum: %s", err)
6060
}
61-
defer f.Close()
61+
defer func() { _ = f.Close() }()
6262

6363
c.Hash.Reset()
6464
if _, err := io.Copy(c.Hash, f); err != nil {
@@ -80,20 +80,24 @@ func (c *FileChecksum) checksum(source string) error {
8080
// extractChecksum will return a FileChecksum based on the 'checksum'
8181
// parameter of u.
8282
// ex:
83-
// http://hashicorp.com/terraform?checksum=<checksumValue>
84-
// http://hashicorp.com/terraform?checksum=<checksumType>:<checksumValue>
85-
// http://hashicorp.com/terraform?checksum=file:<checksum_url>
83+
//
84+
// http://hashicorp.com/terraform?checksum=<checksumValue>
85+
// http://hashicorp.com/terraform?checksum=<checksumType>:<checksumValue>
86+
// http://hashicorp.com/terraform?checksum=file:<checksum_url>
87+
//
8688
// when checksumming from a file, extractChecksum will go get checksum_url
8789
// in a temporary directory, parse the content of the file then delete it.
8890
// Content of files are expected to be BSD style or GNU style.
8991
//
9092
// BSD-style checksum:
91-
// MD5 (file1) = <checksum>
92-
// MD5 (file2) = <checksum>
93+
//
94+
// MD5 (file1) = <checksum>
95+
// MD5 (file2) = <checksum>
9396
//
9497
// GNU-style:
95-
// <checksum> file1
96-
// <checksum> *file2
98+
//
99+
// <checksum> file1
100+
// <checksum> *file2
97101
//
98102
// see parseChecksumLine for more detail on checksum file parsing
99103
func (c *Client) extractChecksum(u *url.URL) (*FileChecksum, error) {
@@ -203,7 +207,7 @@ func (c *Client) ChecksumFromFile(checksumFile string, src *url.URL) (*FileCheck
203207
if err != nil {
204208
return nil, err
205209
}
206-
defer os.Remove(tempfile)
210+
defer func() { _ = os.Remove(tempfile) }()
207211

208212
c2 := &Client{
209213
Ctx: c.Ctx,
@@ -253,7 +257,7 @@ func (c *Client) ChecksumFromFile(checksumFile string, src *url.URL) (*FileCheck
253257
return nil, fmt.Errorf(
254258
"Error opening downloaded file: %s", err)
255259
}
256-
defer f.Close()
260+
defer func() { _ = f.Close() }()
257261
rd := bufio.NewReader(f)
258262
for {
259263
line, err := rd.ReadString('\n')

client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (c *Client) Get() error {
148148
if err != nil {
149149
return err
150150
}
151-
defer tdcloser.Close()
151+
defer func() { _ = tdcloser.Close() }()
152152

153153
realDst = dst
154154
dst = td
@@ -210,7 +210,7 @@ func (c *Client) Get() error {
210210
return fmt.Errorf(
211211
"Error creating temporary directory for archive: %s", err)
212212
}
213-
defer os.RemoveAll(td)
213+
defer func() { _ = os.RemoveAll(td) }()
214214

215215
// Swap the download directory to be our temporary path and
216216
// store the old values.

client_option_progress_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestGet_progress(t *testing.T) {
4141

4242
{ // dl without tracking
4343
dst := tempTestFile(t)
44-
defer os.RemoveAll(filepath.Dir(dst))
44+
defer func() { _ = os.RemoveAll(filepath.Dir(dst)) }()
4545
if err := GetFile(dst, s.URL+"/file?thig=this&that"); err != nil {
4646
t.Fatalf("download failed: %v", err)
4747
}
@@ -50,7 +50,7 @@ func TestGet_progress(t *testing.T) {
5050
{ // tracking
5151
p := &MockProgressTracking{}
5252
dst := tempTestFile(t)
53-
defer os.RemoveAll(filepath.Dir(dst))
53+
defer func() { _ = os.RemoveAll(filepath.Dir(dst)) }()
5454
if err := GetFile(dst, s.URL+"/file?thig=this&that", WithProgress(p)); err != nil {
5555
t.Fatalf("download failed: %v", err)
5656
}

cmd/go-getter/progress_tracking.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (cpb *ProgressBar) TrackProgress(src string, currentSize, totalSize int64,
4949
ProgressBarConfig(newPb, filepath.Base(src))
5050
if cpb.pool == nil {
5151
cpb.pool = pb.NewPool()
52-
cpb.pool.Start()
52+
_ = cpb.pool.Start()
5353
}
5454
cpb.pool.Add(newPb)
5555
reader := newPb.NewProxyReader(stream)
@@ -64,7 +64,7 @@ func (cpb *ProgressBar) TrackProgress(src string, currentSize, totalSize int64,
6464
newPb.Finish()
6565
cpb.pbs--
6666
if cpb.pbs <= 0 {
67-
cpb.pool.Stop()
67+
_ = cpb.pool.Stop()
6868
cpb.pool = nil
6969
}
7070
return nil

common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ func tmpFile(dir, pattern string) (string, error) {
1212
if err != nil {
1313
return "", err
1414
}
15-
f.Close()
15+
_ = f.Close()
1616
return f.Name(), nil
1717
}

decompress_bzip2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (d *Bzip2Decompressor) Decompress(dst, src string, dir bool, umask os.FileM
3535
if err != nil {
3636
return err
3737
}
38-
defer f.Close()
38+
defer func() { _ = f.Close() }()
3939

4040
// Bzip2 compression is second
4141
bzipR := bzip2.NewReader(f)

decompress_gzip.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ func (d *GzipDecompressor) Decompress(dst, src string, dir bool, umask os.FileMo
3535
if err != nil {
3636
return err
3737
}
38-
defer f.Close()
38+
defer func() { _ = f.Close() }()
3939

4040
// gzip compression is second
4141
gzipR, err := gzip.NewReader(f)
4242
if err != nil {
4343
return err
4444
}
45-
defer gzipR.Close()
45+
defer func() { _ = gzipR.Close() }()
4646

4747
// Copy it out
4848
return copyReader(dst, gzipR, 0622, umask, d.FileSizeLimit)

decompress_tar.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (d *TarDecompressor) Decompress(dst, src string, dir bool, umask os.FileMod
180180
if err != nil {
181181
return err
182182
}
183-
defer f.Close()
183+
defer func() { _ = f.Close() }()
184184

185185
return untar(f, dst, src, dir, umask, d.FileSizeLimit, d.FilesLimit)
186186
}

decompress_tar_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func testDecompressorPermissions(t *testing.T, d Decompressor, input string, exp
146146
t.Fatalf("err: %s", err)
147147
}
148148

149-
defer os.RemoveAll(dst)
149+
defer func() { _ = os.RemoveAll(dst) }()
150150

151151
for name, mode := range expected {
152152
fi, err := os.Stat(filepath.Join(dst, name))

0 commit comments

Comments
 (0)