Skip to content

Commit 45af459

Browse files
author
Abhijeet
authored
fix additional lint errors and increase linter scope
Also test creds parsing in S3Getter.
1 parent c8c6aba commit 45af459

15 files changed

+101
-97
lines changed

.github/workflows/go-getter.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,3 @@ jobs:
162162
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
163163
- name: Lint code
164164
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ func (cerr *ChecksumError) Error() string {
5656
func (c *FileChecksum) checksum(source string) error {
5757
f, err := os.Open(source)
5858
if err != nil {
59-
return fmt.Errorf("Failed to open file for checksum: %s", err)
59+
return fmt.Errorf("failed to open file for checksum: %s", err)
6060
}
6161
defer func() { _ = f.Close() }()
6262

6363
c.Hash.Reset()
6464
if _, err := io.Copy(c.Hash, f); err != nil {
65-
return fmt.Errorf("Failed to hash: %s", err)
65+
return fmt.Errorf("failed to hash: %s", err)
6666
}
6767

6868
if actual := c.Hash.Sum(nil); !bytes.Equal(actual, c.Value) {
@@ -184,7 +184,7 @@ func newChecksumFromValue(checksumValue, filename string) (*FileChecksum, error)
184184
c.Hash = sha512.New()
185185
c.Type = "sha512"
186186
default:
187-
return nil, fmt.Errorf("Unknown type for checksum %s", checksumValue)
187+
return nil, fmt.Errorf("unknown type for checksum %s", checksumValue)
188188
}
189189

190190
return c, nil
@@ -307,7 +307,7 @@ func parseChecksumLine(line string) (*FileChecksum, error) {
307307
if len(parts[1]) <= 2 ||
308308
parts[1][0] != '(' || parts[1][len(parts[1])-1] != ')' {
309309
return nil, fmt.Errorf(
310-
"Unexpected BSD-style-checksum filename format: %s", line)
310+
"unexpected BSD-style-checksum filename format: %s", line)
311311
}
312312
filename := parts[1][1 : len(parts[1])-1]
313313
return newChecksumFromType(parts[0], parts[3], filename)

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (c *Client) Get() error {
135135
if subDir != "" {
136136
// Check if the subdirectory is attempting to traverse updwards, outside of
137137
// the cloned repository path.
138-
subDir := filepath.Clean(subDir)
138+
subDir = filepath.Clean(subDir)
139139
if containsDotDot(subDir) {
140140
return fmt.Errorf("subdirectory component contain path traversal out of the repository")
141141
}

client_option_progress_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (p *MockProgressTracking) TrackProgress(src string,
2727
p.downloaded = map[string]int{}
2828
}
2929

30-
v, _ := p.downloaded[src]
30+
v := p.downloaded[src]
3131
p.downloaded[src] = v + 1
3232
return stream
3333
}

cmd/go-getter/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func main() {
7777
}
7878
}()
7979

80-
c := make(chan os.Signal)
80+
c := make(chan os.Signal, 1)
8181
signal.Notify(c, os.Interrupt)
8282

8383
select {

decompress_testing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestDecompressor(t testing.TB, d Decompressor, cases []TestDecompressCase)
9393
expected := tc.DirList
9494
if runtime.GOOS == "windows" {
9595
for i, v := range expected {
96-
expected[i] = strings.Replace(v, "/", "\\", -1)
96+
expected[i] = strings.ReplaceAll(v, "/", "\\")
9797
}
9898
}
9999

get_gcs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ func (g *GCSGetter) getObject(ctx context.Context, client *storage.Client, dst,
170170
var rc *storage.Reader
171171
var err error
172172
if fragment != "" {
173-
generation, err := strconv.ParseInt(fragment, 10, 64)
173+
var generation int64
174+
generation, err = strconv.ParseInt(fragment, 10, 64)
174175
if err != nil {
175176
return err
176177
}

get_git.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ type GitGetter struct {
3434
Timeout time.Duration
3535
}
3636

37-
var defaultBranchRegexp = regexp.MustCompile(`\s->\sorigin/(.*)`)
3837
var lsRemoteSymRefRegexp = regexp.MustCompile(`ref: refs/heads/([^\s]+).*`)
3938

4039
func (g *GitGetter) ClientMode(_ *url.URL) (ClientMode, error) {
@@ -83,7 +82,7 @@ func (g *GitGetter) Get(dst string, u *url.URL) error {
8382
q.Del("depth")
8483

8584
// Copy the URL
86-
var newU url.URL = *u
85+
newU := *u
8786
u = &newU
8887
u.RawQuery = q.Encode()
8988
}
@@ -313,22 +312,6 @@ func (g *GitGetter) fetchSubmodules(ctx context.Context, dst, sshKeyFile string,
313312
return getRunCommand(cmd)
314313
}
315314

316-
// findDefaultBranch checks the repo's origin remote for its default branch
317-
// (generally "master"). "master" is returned if an origin default branch
318-
// can't be determined.
319-
func findDefaultBranch(ctx context.Context, dst string) string {
320-
var stdoutbuf bytes.Buffer
321-
cmd := exec.CommandContext(ctx, "git", "branch", "-r", "--points-at", "refs/remotes/origin/HEAD")
322-
cmd.Dir = dst
323-
cmd.Stdout = &stdoutbuf
324-
err := cmd.Run()
325-
matches := defaultBranchRegexp.FindStringSubmatch(stdoutbuf.String())
326-
if err != nil || matches == nil {
327-
return "master"
328-
}
329-
return matches[len(matches)-1]
330-
}
331-
332315
// findRemoteDefaultBranch checks the remote repo's HEAD symref to return the remote repo's
333316
// default branch. "master" is returned if no HEAD symref exists.
334317
func findRemoteDefaultBranch(ctx context.Context, u *url.URL) string {
@@ -374,7 +357,7 @@ func setupGitEnv(cmd *exec.Cmd, sshKeyFile string) {
374357

375358
// We have an SSH key temp file configured, tell ssh about this.
376359
if runtime.GOOS == "windows" {
377-
sshKeyFile = strings.Replace(sshKeyFile, `\`, `/`, -1)
360+
sshKeyFile = strings.ReplaceAll(sshKeyFile, `\`, `/`)
378361
}
379362
sshCmd = append(sshCmd, "-i", sshKeyFile)
380363
env = append(env, strings.Join(sshCmd, " "))
@@ -398,7 +381,7 @@ func checkGitVersion(ctx context.Context, min string) error {
398381

399382
fields := strings.Fields(string(out))
400383
if len(fields) < 3 {
401-
return fmt.Errorf("Unexpected 'git version' output: %q", string(out))
384+
return fmt.Errorf("unexpected 'git version' output: %q", string(out))
402385
}
403386
v := fields[2]
404387
if runtime.GOOS == "windows" && strings.Contains(v, ".windows.") {
@@ -416,7 +399,7 @@ func checkGitVersion(ctx context.Context, min string) error {
416399
}
417400

418401
if have.LessThan(want) {
419-
return fmt.Errorf("Required git version = %s, have %s", want, have)
402+
return fmt.Errorf("required git version = %s, have %s", want, have)
420403
}
421404

422405
return nil
@@ -426,13 +409,13 @@ func checkGitVersion(ctx context.Context, min string) error {
426409
func removeCaseInsensitiveGitDirectory(dst string) error {
427410
files, err := os.ReadDir(dst)
428411
if err != nil {
429-
return fmt.Errorf("Failed to read the destination directory %s during git update", dst)
412+
return fmt.Errorf("failed to read the destination directory %s during git update", dst)
430413
}
431414
for _, f := range files {
432415
if strings.EqualFold(f.Name(), ".git") && f.IsDir() {
433416
err := os.RemoveAll(filepath.Join(dst, f.Name()))
434417
if err != nil {
435-
return fmt.Errorf("Failed to remove the .git directory in the destination directory %s during git update", dst)
418+
return fmt.Errorf("failed to remove the .git directory in the destination directory %s during git update", dst)
436419
}
437420
}
438421
}

get_git_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ func TestGitGetter_sshSCPStyleInvalidScheme(t *testing.T) {
603603

604604
got := err.Error()
605605
want1, want2 := `invalid source string`, `invalid port number "hashicorp"`
606-
if !(strings.Contains(got, want1) || strings.Contains(got, want2)) {
606+
if !strings.Contains(got, want1) && !strings.Contains(got, want2) {
607607
t.Fatalf("wrong error\ngot: %s\nwant: %q or %q", got, want1, want2)
608608
}
609609
}
@@ -621,7 +621,7 @@ func TestGitGetter_submodule(t *testing.T) {
621621
if err != nil {
622622
t.Fatal(err)
623623
}
624-
return strings.Replace(relpath, `\`, `/`, -1)
624+
return strings.ReplaceAll(relpath, `\`, `/`)
625625
// on windows git still prefers relatives paths
626626
// containing `/` for submodules
627627
}
@@ -887,7 +887,7 @@ func TestGitGetter_BadGitConfig(t *testing.T) {
887887

888888
_, err = os.Stat(dst)
889889
if err != nil && !os.IsNotExist(err) {
890-
t.Fatalf(err.Error())
890+
t.Fatal(err.Error())
891891
}
892892
if err == nil {
893893
// Update the repository containing the bad git config.
@@ -897,29 +897,29 @@ func TestGitGetter_BadGitConfig(t *testing.T) {
897897
// Clone a repository with a git config file
898898
err = g.clone(ctx, dst, testGitToken, url, "main", 1)
899899
if err != nil {
900-
t.Fatalf(err.Error())
900+
t.Fatal(err.Error())
901901
}
902902

903903
// Edit the git config file to simulate a bad git config
904904
gitConfigPath := filepath.Join(dst, ".git", "config")
905905
err = os.WriteFile(gitConfigPath, []byte("bad config"), 0600)
906906
if err != nil {
907-
t.Fatalf(err.Error())
907+
t.Fatal(err.Error())
908908
}
909909

910910
// Update the repository containing the bad git config.
911911
// This should remove the bad git config file and initialize a new one.
912912
err = g.update(ctx, dst, testGitToken, url, "main", 1)
913913
}
914914
if err != nil {
915-
t.Fatalf(err.Error())
915+
t.Fatal(err.Error())
916916
}
917917

918918
// Check if the .git/config file contains "bad config"
919919
gitConfigPath := filepath.Join(dst, ".git", "config")
920920
configBytes, err := os.ReadFile(gitConfigPath)
921921
if err != nil {
922-
t.Fatalf(err.Error())
922+
t.Fatal(err.Error())
923923
}
924924
if strings.Contains(string(configBytes), "bad config") {
925925
t.Fatalf("The .git/config file contains 'bad config'")
@@ -943,37 +943,37 @@ func TestGitGetter_BadGitDirName(t *testing.T) {
943943

944944
_, err = os.Stat(dst)
945945
if err != nil && !os.IsNotExist(err) {
946-
t.Fatalf(err.Error())
946+
t.Fatal(err.Error())
947947
}
948948
if err == nil {
949949
// Remove all variations of .git directories
950950
err = removeCaseInsensitiveGitDirectory(dst)
951951
if err != nil {
952-
t.Fatalf(err.Error())
952+
t.Fatal(err.Error())
953953
}
954954
} else {
955955
// Clone a repository with a git directory
956956
err = g.clone(ctx, dst, testGitToken, url, "main", 1)
957957
if err != nil {
958-
t.Fatalf(err.Error())
958+
t.Fatal(err.Error())
959959
}
960960

961961
// Rename the .git directory to .GIT
962962
oldPath := filepath.Join(dst, ".git")
963963
newPath := filepath.Join(dst, ".GIT")
964964
err = os.Rename(oldPath, newPath)
965965
if err != nil {
966-
t.Fatalf(err.Error())
966+
t.Fatal(err.Error())
967967
}
968968

969969
// Remove all variations of .git directories
970970
err = removeCaseInsensitiveGitDirectory(dst)
971971
if err != nil {
972-
t.Fatalf(err.Error())
972+
t.Fatal(err.Error())
973973
}
974974
}
975975
if err != nil {
976-
t.Fatalf(err.Error())
976+
t.Fatal(err.Error())
977977
}
978978

979979
// Check if the .GIT directory exists
@@ -1004,13 +1004,13 @@ func TestGitGetter_BadRef(t *testing.T) {
10041004

10051005
_, err = os.Stat(dst)
10061006
if err != nil && !os.IsNotExist(err) {
1007-
t.Fatalf(err.Error())
1007+
t.Fatal(err.Error())
10081008
}
10091009

10101010
// Clone a repository with non-existent ref
10111011
err = g.clone(ctx, dst, "", url, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0)
10121012
if err == nil {
1013-
t.Fatalf(err.Error())
1013+
t.Fatal(err.Error())
10141014
}
10151015

10161016
// Expect that the dst was cleaned up after failed ref checkout

get_http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error {
208208
}
209209

210210
// Copy the URL so we can modify it
211-
var newU url.URL = *u
211+
newU := *u
212212
u = &newU
213213

214214
if g.Netrc {

0 commit comments

Comments
 (0)