Skip to content

Commit

Permalink
Merge pull request #19 from dm3ch/fix-linting-issues
Browse files Browse the repository at this point in the history
Fixing lint errors
  • Loading branch information
dm3ch authored Jan 30, 2022
2 parents b096b8f + 90896b6 commit 0311868
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 39 deletions.
6 changes: 4 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ linters-settings:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
golint:
min-confidence: 0
gocyclo:
min-complexity: 10
maligned:
Expand All @@ -36,6 +34,10 @@ linters:
- maligned
- prealloc
- depguard
- wrapcheck # Requires wrapping external Errors to new ones
- forbidigo # Restricts using of fmt.Println
- goerr113 # Requires rewriting of most of errors to static ones in cmd/usage.go
- exhaustivestruct # Reports errors in Cobra structs

issues:
exclude-rules:
Expand Down
1 change: 0 additions & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"

"github.com/dm3ch/git-profile-manager/profile"

"github.com/spf13/cobra"
)

Expand Down
28 changes: 17 additions & 11 deletions cmd/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,51 @@ func getConfigValue(key string) string {
if err != nil {
return ""
}

return out[:len(out)-1]
}

//nolint:gocyclo
//nolint: gocyclo, gocognit, cyclop, gocritic
func templateRender(tpl string) string {
phStartPos := -1
phEndPos := 0
keyStartPos := -1
keyEndPos := -1
result := ""

for i := 0; i < len(tpl); i++ {
if i != 0 && tpl[i] == '{' && tpl[i-1] == '{' {
phStartPos = i - 1
for index := 0; index < len(tpl); index++ {
if index != 0 && tpl[index] == '{' && tpl[index-1] == '{' {
phStartPos = index - 1

continue
}

if phStartPos != -1 && keyStartPos == -1 && tpl[i] != ' ' {
keyStartPos = i
if phStartPos != -1 && keyStartPos == -1 && tpl[index] != ' ' {
keyStartPos = index
}

if keyStartPos != -1 && keyEndPos == -1 && tpl[i] == ' ' {
keyEndPos = i
if keyStartPos != -1 && keyEndPos == -1 && tpl[index] == ' ' {
keyEndPos = index
}

if i != 0 && tpl[i] == '}' && tpl[i-1] == '}' {
if index != 0 && tpl[index] == '}' && tpl[index-1] == '}' {
if phStartPos != -1 {
result += tpl[phEndPos:phStartPos]

if keyEndPos == -1 {
keyEndPos = i - 1
keyEndPos = index - 1
}

result += getConfigValue(tpl[keyStartPos:keyEndPos])
phEndPos = i + 1
phEndPos = index + 1
phStartPos = -1
keyStartPos = -1
keyEndPos = -1
}
}
}

result += tpl[phEndPos:]

return result
}
24 changes: 15 additions & 9 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ const (
profileExtention = "profile"
)

// Create directory if it doesn't exists
// Create directory if it doesn't exists.
func createDirIfNotExist(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
err = os.MkdirAll(dir, 0o755) //nolint:gomnd //Rights used only there so it doesn't make sense to move it to const

return err
}

return nil
}

// Get configuration directory relative path
// Get configuration directory relative path.
func getConfigDirRelativePath() string {
return viper.GetString("configDir")
}

// Get configuration directory absolute path
// Get configuration directory absolute path.
func getConfigDirAbsolutePath() (string, error) {
configDir, err := homedir.Expand(getConfigDirRelativePath())
if err != nil {
Expand All @@ -40,40 +41,45 @@ func getConfigDirAbsolutePath() (string, error) {
return configDir, nil
}

// Prompts string with label
// Prompts string with label.
func prompt(label string) string {
reader := bufio.NewReader(os.Stdin)

fmt.Printf("%s: ", label)

str, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Prompt failed:")
fmt.Println(err)
os.Exit(1)
}

return str[:len(str)-1]
}

// Prompts empty GitUser fields
// Prompts empty GitUser fields.
func promptGitUser(user *profile.GitUser) {
user.Name = prompt("Name")
user.Email = prompt("Email")
user.SigningKey = prompt("Signing Key")
}

// Prompts yes or now answer
// Prompts yes or now answer.
func promptYesNo(label string) bool {
answer := prompt(label + " [y/N]")

return (answer == "y" || answer == "Y")
}

// Get profile file path
// Get profile file path.
func getProfilePath(configDir, profileName string) string {
return filepath.Join(configDir, profileName+"."+profileExtention)
}

// Check if file exists
// Check if file exists.
func isFileExist(path string) bool {
absolutePath, _ := homedir.Expand(path)
_, err := os.Stat(absolutePath)

return !os.IsNotExist(err)
}
1 change: 1 addition & 0 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var removeCmd = &cobra.Command{
profileExists := isFileExist(path)
if !profileExists {
fmt.Printf("Profile %s does not exists\n", profileName)

return
}

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func Execute() {
func init() {
rootCmd.PersistentFlags().StringP("configDir", "C", defaultConfigDir,
fmt.Sprintf("Configuration directory (Could be also set via %s environment variable)", envConfigDir))

_ = viper.BindPFlag("configDir", rootCmd.PersistentFlags().Lookup("configDir"))
_ = viper.BindEnv("configDir", envConfigDir)
viper.SetDefault("configDir", defaultConfigDir)
Expand Down
4 changes: 2 additions & 2 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"fmt"
"io/ioutil"
"io"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -37,7 +37,7 @@ var showCmd = &cobra.Command{
}
defer file.Close()

buffer, err := ioutil.ReadAll(file)
buffer, err := io.ReadAll(file)
if err != nil {
file.Close()
fmt.Printf("Can't read profile %s file:\n", profileName)
Expand Down
26 changes: 17 additions & 9 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ func unsetProfile(configType gitconfig.ConfigType, unsetProfilePath bool, key, v
}

func unsetProfileWrapper(incType includeType, dir string) error {
var key, value string
var unsetProfilePath bool
var err error
var configType gitconfig.ConfigType
var (
key, value string
unsetProfilePath bool
err error
configType gitconfig.ConfigType
)

switch incType {
case dirInclude:
Expand All @@ -125,18 +127,22 @@ func unsetProfileWrapper(incType includeType, dir string) error {
unsetProfilePath = true
key = gitIncludePath
value, err = gitconfig.Get(configType, gitProfilePath)

if err != nil || value == "" {
return nil
return fmt.Errorf("failed to detect profile value during unset")
}

value = value[:len(value)-1]
case localInclude:
configType = gitconfig.LocalConfig
unsetProfilePath = true
key = gitIncludePath
value, err = gitconfig.Get(configType, gitProfilePath)

if err != nil || value == "" {
return nil
return fmt.Errorf("failed to detect profile value during unset")
}

value = value[:len(value)-1]
}

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

func setProfileWrapper(incType includeType, dir, profilePath string) error {
var key string
var setProfilePath bool
var configType gitconfig.ConfigType
var (
key string
setProfilePath bool
configType gitconfig.ConfigType
)

switch incType {
case dirInclude:
Expand Down
3 changes: 3 additions & 0 deletions gitconfig/gitconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ const (

func GitExec(command ...string) (string, error) {
out, err := exec.Command("git", command...).CombinedOutput()

return string(out), err
}

func Exec(configType ConfigType, command ...string) (string, error) {
var args []string

switch configType {
case LocalConfig:
args = append([]string{"config", "--local"}, command...)
Expand Down Expand Up @@ -49,6 +51,7 @@ func UnsetAll(configType ConfigType, key, value string) (string, error) {
if value != "" {
return Exec(configType, "--unset-all", key, value)
}

return Exec(configType, "--unset-all", key)
}

Expand Down
9 changes: 4 additions & 5 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package version

//nolint:gochecknoglobals // These variables should be set by the linker during build.
var (
// These variables should be set by the linker during build

VersionNumber = "Unknown" //nolint:gochecknoglobals
VersionCommitHash = "Unknown" //nolint:gochecknoglobals
VersionBuildDate = "Unknown" //nolint:gochecknoglobals
VersionNumber = "Unknown"
VersionCommitHash = "Unknown"
VersionBuildDate = "Unknown"
)

0 comments on commit 0311868

Please sign in to comment.