6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-04 11:01:46 +00:00

Fix issues found by goerrcheck linter

This commit is contained in:
Grzegorz Dlugoszewski
2025-08-24 16:33:15 +02:00
parent 599643892b
commit 1e1584ba33
6 changed files with 35 additions and 16 deletions

View File

@@ -21,6 +21,13 @@ linters:
- wrapcheck # Adds too much bloat, many of the errors are contextual enough and don't need wrapping
exclusions:
# Typical presets to exclude: https://golangci-lint.run/docs/linters/false-positives/#exclusion-presets
presets:
- comments
- common-false-positives
- legacy
- std-error-handling
rules:
- path: _test.go
linters:

View File

@@ -1,6 +1,7 @@
package main
import (
"fmt"
"git-get/pkg"
"git-get/pkg/cfg"
"git-get/pkg/git"
@@ -35,12 +36,9 @@ func newGetCommand() *cobra.Command {
cmd.PersistentFlags().BoolP("help", "h", false, "Print this help and exit.")
cmd.PersistentFlags().BoolP("version", "v", false, "Print version and exit.")
viper.BindPFlag(cfg.KeyBranch, cmd.PersistentFlags().Lookup(cfg.KeyBranch))
viper.BindPFlag(cfg.KeyDefaultHost, cmd.PersistentFlags().Lookup(cfg.KeyDefaultHost))
viper.BindPFlag(cfg.KeyDefaultScheme, cmd.PersistentFlags().Lookup(cfg.KeyDefaultScheme))
viper.BindPFlag(cfg.KeyDump, cmd.PersistentFlags().Lookup(cfg.KeyDump))
viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
viper.BindPFlag(cfg.KeySkipHost, cmd.PersistentFlags().Lookup(cfg.KeySkipHost))
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
panic(fmt.Sprintf("failed to bind flags: %v", err))
}
return cmd
}

View File

@@ -28,9 +28,9 @@ func newListCommand() *cobra.Command {
cmd.PersistentFlags().BoolP("help", "h", false, "Print this help and exit.")
cmd.PersistentFlags().BoolP("version", "v", false, "Print version and exit.")
viper.BindPFlag(cfg.KeyFetch, cmd.PersistentFlags().Lookup(cfg.KeyFetch))
viper.BindPFlag(cfg.KeyOutput, cmd.PersistentFlags().Lookup(cfg.KeyOutput))
viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
panic(fmt.Sprintf("failed to bind flags: %v", err))
}
return cmd
}

View File

@@ -92,7 +92,11 @@ func readGitconfig(cfg Gitconfig) {
}
viper.SetConfigType("env")
viper.ReadConfig(bytes.NewBuffer([]byte(strings.Join(lines, "\n"))))
if err := viper.ReadConfig(bytes.NewBuffer([]byte(strings.Join(lines, "\n")))); err != nil {
// Log error but don't fail - configuration is optional
fmt.Fprintf(os.Stderr, "Warning: failed to read git config: %v\n", err)
}
// TODO: A hacky way to read boolean flag from gitconfig. Find a cleaner way.
if val := cfg.Get(fmt.Sprintf("%s.%s", GitgetPrefix, KeySkipHost)); strings.ToLower(val) == "true" {

View File

@@ -95,22 +95,28 @@ func testConfigOnlyInGitconfig(t *testing.T) {
func testConfigOnlyInEnvVar(t *testing.T) {
Init(&gitconfigEmpty{})
os.Setenv(envVarName, fromEnv)
t.Setenv(envVarName, fromEnv)
}
func testConfigInGitconfigAndEnvVar(t *testing.T) {
Init(&gitconfigValid{})
os.Setenv(envVarName, fromEnv)
t.Setenv(envVarName, fromEnv)
}
func testConfigInFlag(t *testing.T) {
Init(&gitconfigValid{})
os.Setenv(envVarName, fromEnv)
t.Setenv(envVarName, fromEnv)
cmd := cobra.Command{}
cmd.PersistentFlags().String(KeyDefaultHost, Defaults[KeyDefaultHost], "")
viper.BindPFlag(KeyDefaultHost, cmd.PersistentFlags().Lookup(KeyDefaultHost))
if err := viper.BindPFlag(KeyDefaultHost, cmd.PersistentFlags().Lookup(KeyDefaultHost)); err != nil {
t.Fatalf("failed to bind flag: %v", err)
}
cmd.SetArgs([]string{"--" + KeyDefaultHost, fromFlag})
cmd.Execute()
if err := cmd.Execute(); err != nil {
t.Fatalf("failed to execute command: %v", err)
}
}

View File

@@ -39,7 +39,11 @@ func TestFinder(t *testing.T) {
root := test.reposMaker(t)
finder := NewRepoFinder(root)
finder.Find()
err := finder.Find()
if err != nil {
t.Fatalf("finder.Find() failed: %v", err)
}
assert.Len(t, finder.repos, test.want)
})