Skip to content

Commit 0311868

Browse files
authored
Merge pull request #19 from dm3ch/fix-linting-issues
Fixing lint errors
2 parents b096b8f + 90896b6 commit 0311868

File tree

10 files changed

+64
-39
lines changed

10 files changed

+64
-39
lines changed

.golangci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ linters-settings:
1212
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
1313
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
1414
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
15-
golint:
16-
min-confidence: 0
1715
gocyclo:
1816
min-complexity: 10
1917
maligned:
@@ -36,6 +34,10 @@ linters:
3634
- maligned
3735
- prealloc
3836
- depguard
37+
- wrapcheck # Requires wrapping external Errors to new ones
38+
- forbidigo # Restricts using of fmt.Println
39+
- goerr113 # Requires rewriting of most of errors to static ones in cmd/usage.go
40+
- exhaustivestruct # Reports errors in Cobra structs
3941

4042
issues:
4143
exclude-rules:

cmd/add.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66

77
"github.com/dm3ch/git-profile-manager/profile"
8-
98
"github.com/spf13/cobra"
109
)
1110

cmd/current.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,45 +34,51 @@ func getConfigValue(key string) string {
3434
if err != nil {
3535
return ""
3636
}
37+
3738
return out[:len(out)-1]
3839
}
3940

40-
//nolint:gocyclo
41+
//nolint: gocyclo, gocognit, cyclop, gocritic
4142
func templateRender(tpl string) string {
4243
phStartPos := -1
4344
phEndPos := 0
4445
keyStartPos := -1
4546
keyEndPos := -1
4647
result := ""
4748

48-
for i := 0; i < len(tpl); i++ {
49-
if i != 0 && tpl[i] == '{' && tpl[i-1] == '{' {
50-
phStartPos = i - 1
49+
for index := 0; index < len(tpl); index++ {
50+
if index != 0 && tpl[index] == '{' && tpl[index-1] == '{' {
51+
phStartPos = index - 1
52+
5153
continue
5254
}
5355

54-
if phStartPos != -1 && keyStartPos == -1 && tpl[i] != ' ' {
55-
keyStartPos = i
56+
if phStartPos != -1 && keyStartPos == -1 && tpl[index] != ' ' {
57+
keyStartPos = index
5658
}
5759

58-
if keyStartPos != -1 && keyEndPos == -1 && tpl[i] == ' ' {
59-
keyEndPos = i
60+
if keyStartPos != -1 && keyEndPos == -1 && tpl[index] == ' ' {
61+
keyEndPos = index
6062
}
6163

62-
if i != 0 && tpl[i] == '}' && tpl[i-1] == '}' {
64+
if index != 0 && tpl[index] == '}' && tpl[index-1] == '}' {
6365
if phStartPos != -1 {
6466
result += tpl[phEndPos:phStartPos]
67+
6568
if keyEndPos == -1 {
66-
keyEndPos = i - 1
69+
keyEndPos = index - 1
6770
}
71+
6872
result += getConfigValue(tpl[keyStartPos:keyEndPos])
69-
phEndPos = i + 1
73+
phEndPos = index + 1
7074
phStartPos = -1
7175
keyStartPos = -1
7276
keyEndPos = -1
7377
}
7478
}
7579
}
80+
7681
result += tpl[phEndPos:]
82+
7783
return result
7884
}

cmd/helpers.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,23 @@ const (
1515
profileExtention = "profile"
1616
)
1717

18-
// Create directory if it doesn't exists
18+
// Create directory if it doesn't exists.
1919
func createDirIfNotExist(dir string) error {
2020
if _, err := os.Stat(dir); os.IsNotExist(err) {
21-
err = os.MkdirAll(dir, 0755)
21+
err = os.MkdirAll(dir, 0o755) //nolint:gomnd //Rights used only there so it doesn't make sense to move it to const
22+
2223
return err
2324
}
2425

2526
return nil
2627
}
2728

28-
// Get configuration directory relative path
29+
// Get configuration directory relative path.
2930
func getConfigDirRelativePath() string {
3031
return viper.GetString("configDir")
3132
}
3233

33-
// Get configuration directory absolute path
34+
// Get configuration directory absolute path.
3435
func getConfigDirAbsolutePath() (string, error) {
3536
configDir, err := homedir.Expand(getConfigDirRelativePath())
3637
if err != nil {
@@ -40,40 +41,45 @@ func getConfigDirAbsolutePath() (string, error) {
4041
return configDir, nil
4142
}
4243

43-
// Prompts string with label
44+
// Prompts string with label.
4445
func prompt(label string) string {
4546
reader := bufio.NewReader(os.Stdin)
47+
4648
fmt.Printf("%s: ", label)
49+
4750
str, err := reader.ReadString('\n')
4851
if err != nil {
4952
fmt.Println("Prompt failed:")
5053
fmt.Println(err)
5154
os.Exit(1)
5255
}
56+
5357
return str[:len(str)-1]
5458
}
5559

56-
// Prompts empty GitUser fields
60+
// Prompts empty GitUser fields.
5761
func promptGitUser(user *profile.GitUser) {
5862
user.Name = prompt("Name")
5963
user.Email = prompt("Email")
6064
user.SigningKey = prompt("Signing Key")
6165
}
6266

63-
// Prompts yes or now answer
67+
// Prompts yes or now answer.
6468
func promptYesNo(label string) bool {
6569
answer := prompt(label + " [y/N]")
70+
6671
return (answer == "y" || answer == "Y")
6772
}
6873

69-
// Get profile file path
74+
// Get profile file path.
7075
func getProfilePath(configDir, profileName string) string {
7176
return filepath.Join(configDir, profileName+"."+profileExtention)
7277
}
7378

74-
// Check if file exists
79+
// Check if file exists.
7580
func isFileExist(path string) bool {
7681
absolutePath, _ := homedir.Expand(path)
7782
_, err := os.Stat(absolutePath)
83+
7884
return !os.IsNotExist(err)
7985
}

cmd/remove.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var removeCmd = &cobra.Command{
2929
profileExists := isFileExist(path)
3030
if !profileExists {
3131
fmt.Printf("Profile %s does not exists\n", profileName)
32+
3233
return
3334
}
3435

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func Execute() {
4444
func init() {
4545
rootCmd.PersistentFlags().StringP("configDir", "C", defaultConfigDir,
4646
fmt.Sprintf("Configuration directory (Could be also set via %s environment variable)", envConfigDir))
47+
4748
_ = viper.BindPFlag("configDir", rootCmd.PersistentFlags().Lookup("configDir"))
4849
_ = viper.BindEnv("configDir", envConfigDir)
4950
viper.SetDefault("configDir", defaultConfigDir)

cmd/show.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"os"
77

88
"github.com/spf13/cobra"
@@ -37,7 +37,7 @@ var showCmd = &cobra.Command{
3737
}
3838
defer file.Close()
3939

40-
buffer, err := ioutil.ReadAll(file)
40+
buffer, err := io.ReadAll(file)
4141
if err != nil {
4242
file.Close()
4343
fmt.Printf("Can't read profile %s file:\n", profileName)

cmd/use.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,12 @@ func unsetProfile(configType gitconfig.ConfigType, unsetProfilePath bool, key, v
109109
}
110110

111111
func unsetProfileWrapper(incType includeType, dir string) error {
112-
var key, value string
113-
var unsetProfilePath bool
114-
var err error
115-
var configType gitconfig.ConfigType
112+
var (
113+
key, value string
114+
unsetProfilePath bool
115+
err error
116+
configType gitconfig.ConfigType
117+
)
116118

117119
switch incType {
118120
case dirInclude:
@@ -125,18 +127,22 @@ func unsetProfileWrapper(incType includeType, dir string) error {
125127
unsetProfilePath = true
126128
key = gitIncludePath
127129
value, err = gitconfig.Get(configType, gitProfilePath)
130+
128131
if err != nil || value == "" {
129-
return nil
132+
return fmt.Errorf("failed to detect profile value during unset")
130133
}
134+
131135
value = value[:len(value)-1]
132136
case localInclude:
133137
configType = gitconfig.LocalConfig
134138
unsetProfilePath = true
135139
key = gitIncludePath
136140
value, err = gitconfig.Get(configType, gitProfilePath)
141+
137142
if err != nil || value == "" {
138-
return nil
143+
return fmt.Errorf("failed to detect profile value during unset")
139144
}
145+
140146
value = value[:len(value)-1]
141147
}
142148

@@ -160,9 +166,11 @@ func setProfile(configType gitconfig.ConfigType, setProfilePath bool, key, value
160166
}
161167

162168
func setProfileWrapper(incType includeType, dir, profilePath string) error {
163-
var key string
164-
var setProfilePath bool
165-
var configType gitconfig.ConfigType
169+
var (
170+
key string
171+
setProfilePath bool
172+
configType gitconfig.ConfigType
173+
)
166174

167175
switch incType {
168176
case dirInclude:

gitconfig/gitconfig.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ const (
1616

1717
func GitExec(command ...string) (string, error) {
1818
out, err := exec.Command("git", command...).CombinedOutput()
19+
1920
return string(out), err
2021
}
2122

2223
func Exec(configType ConfigType, command ...string) (string, error) {
2324
var args []string
25+
2426
switch configType {
2527
case LocalConfig:
2628
args = append([]string{"config", "--local"}, command...)
@@ -49,6 +51,7 @@ func UnsetAll(configType ConfigType, key, value string) (string, error) {
4951
if value != "" {
5052
return Exec(configType, "--unset-all", key, value)
5153
}
54+
5255
return Exec(configType, "--unset-all", key)
5356
}
5457

version/version.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package version
22

3+
//nolint:gochecknoglobals // These variables should be set by the linker during build.
34
var (
4-
// These variables should be set by the linker during build
5-
6-
VersionNumber = "Unknown" //nolint:gochecknoglobals
7-
VersionCommitHash = "Unknown" //nolint:gochecknoglobals
8-
VersionBuildDate = "Unknown" //nolint:gochecknoglobals
5+
VersionNumber = "Unknown"
6+
VersionCommitHash = "Unknown"
7+
VersionBuildDate = "Unknown"
98
)

0 commit comments

Comments
 (0)