Skip to content

Commit

Permalink
linter: enable noctx and unused
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer [email protected]
  • Loading branch information
sbueringer committed Jan 20, 2022
1 parent 07e31b7 commit b1cd72e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ linters:
- misspell
- nakedret
- nilerr
- noctx
- nolintlint
- prealloc
- predeclared
Expand All @@ -37,6 +38,7 @@ linters:
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace

Expand Down Expand Up @@ -149,6 +151,8 @@ linters-settings:
- unnecessaryDefer
- whyNoLint
- wrapperFunc
unused:
go: "1.17"
issues:
max-same-issues: 0
max-issues-per-linter: 0
Expand Down
10 changes: 9 additions & 1 deletion cmd/clusterctl/client/config/reader_viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package config

import (
"context"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -133,6 +134,8 @@ func (v *viperReader) Init(path string) error {
}

func downloadFile(url string, filepath string) error {
ctx := context.TODO()

// Create the file
out, err := os.Create(filepath)
if err != nil {
Expand All @@ -144,7 +147,12 @@ func downloadFile(url string, filepath string) error {
Timeout: 30 * time.Second,
}
// Get the data
resp, err := client.Get(url)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return errors.Wrapf(err, "failed to download the clusterctl config file from %s: failed to create request", url)
}

resp, err := client.Do(req)
if err != nil {
return errors.Wrapf(err, "failed to download the clusterctl config file from %s", url)
}
Expand Down
13 changes: 10 additions & 3 deletions cmd/clusterctl/client/repository/repository_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,14 @@ func (g *gitHubRepository) getReleaseByTag(tag string) (*github.RepositoryReleas

// downloadFilesFromRelease download a file from release.
func (g *gitHubRepository) downloadFilesFromRelease(release *github.RepositoryRelease, fileName string) ([]byte, error) {
ctx := context.TODO()

cacheID := fmt.Sprintf("%s/%s:%s:%s", g.owner, g.repository, *release.TagName, fileName)
if content, ok := cacheFiles[cacheID]; ok {
return content, nil
}

client := g.getClient()
c := g.getClient()
absoluteFileName := filepath.Join(g.rootPath, fileName)

// search for the file into the release assets, retrieving the asset id
Expand All @@ -282,12 +284,17 @@ func (g *gitHubRepository) downloadFilesFromRelease(release *github.RepositoryRe
return nil, errors.Errorf("failed to get file %q from %q release", fileName, *release.TagName)
}

reader, redirect, err := client.Repositories.DownloadReleaseAsset(context.TODO(), g.owner, g.repository, *assetID, http.DefaultClient)
reader, redirect, err := c.Repositories.DownloadReleaseAsset(context.TODO(), g.owner, g.repository, *assetID, http.DefaultClient)
if err != nil {
return nil, g.handleGithubErr(err, "failed to download file %q from %q release", *release.TagName, fileName)
}
if redirect != "" {
response, err := http.Get(redirect) //nolint:bodyclose,gosec // (NB: The reader is actually closed in a defer)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, redirect, http.NoBody)
if err != nil {
return nil, errors.Wrapf(err, "failed to download file %q from %q release via redirect location %q: failed to create request", *release.TagName, fileName, redirect)
}

response, err := http.DefaultClient.Do(req) //nolint:bodyclose // (NB: The reader is actually closed in a defer)
if err != nil {
return nil, errors.Wrapf(err, "failed to download file %q from %q release via redirect location %q", *release.TagName, fileName, redirect)
}
Expand Down

0 comments on commit b1cd72e

Please sign in to comment.