Skip to content

Commit

Permalink
cleanup loading config from different locations (based on user env va…
Browse files Browse the repository at this point in the history
…riables) - using afero for mocking fs
  • Loading branch information
mfederowicz committed Oct 2, 2023
1 parent cdb8268 commit 0eb4c9f
Show file tree
Hide file tree
Showing 4 changed files with 526 additions and 7 deletions.
37 changes: 30 additions & 7 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
"github.com/mgechev/revive/config"
"github.com/mgechev/revive/revivelib"
"github.com/mitchellh/go-homedir"
"github.com/spf13/afero"
)

var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
AppFs = afero.NewOsFs()

Check warning on line 24 in cli/main.go

View workflow job for this annotation

GitHub Actions / Lint

exported var AppFs should have comment or be unexported
)

func fail(err string) {
Expand All @@ -29,7 +31,10 @@ func fail(err string) {

// RunRevive runs the CLI for revive.
func RunRevive(extraRules ...revivelib.ExtraRule) {
conf, err := config.GetConfig(configPath)
// move parsing flags outside of init() otherwise tests dont works properly
// more info: https://github.com/golang/go/issues/46869#issuecomment-865695953
initConfig()
conf, err := config.GetConfig(configPath)
if err != nil {
fail(err.Error())
}
Expand Down Expand Up @@ -105,17 +110,26 @@ Example:

func buildDefaultConfigPath() string {
var result string
var homeDirFile string
configFileName := "revive.toml"
configDirFile := filepath.Join(os.Getenv("XDG_CONFIG_HOME"), configFileName)

if homeDir, err := homedir.Dir(); err == nil {
result = filepath.Join(homeDir, "revive.toml")
if _, err := os.Stat(result); err != nil {
result = ""
}
homeDirFile = filepath.Join(homeDir, configFileName)
}

if fileExist(configDirFile) {
result = configDirFile
} else if fileExist(homeDirFile) {
result = homeDirFile
} else {
result = ""
}

return result
}

func init() {
func initConfig() {
// Force colorizing for no TTY environments
if os.Getenv("REVIVE_FORCE_COLOR") == "1" {
color.NoColor = false
Expand All @@ -128,7 +142,7 @@ func init() {

// command line help strings
const (
configUsage = "path to the configuration TOML file, defaults to $HOME/revive.toml, if present (i.e. -config myconf.toml)"
configUsage = "path to the configuration TOML file, defaults to $XDG_CONFIG_HOME/revive.toml or $HOME/revive.toml, if present (i.e. -config myconf.toml)"
excludeUsage = "list of globs which specify files to be excluded (i.e. -exclude foo/...)"
formatterUsage = "formatter to be used for the output (i.e. -formatter stylish)"
versionUsage = "get revive version"
Expand Down Expand Up @@ -175,3 +189,12 @@ func init() {
os.Exit(0)
}
}

func fileExist(path string) bool {
_, err := AppFs.Stat(path)
if err != nil {
return false
}

return true
}
60 changes: 60 additions & 0 deletions cli/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cli

import (
"os"
"testing"

"github.com/spf13/afero"
)

func init() {

AppFs = afero.NewMemMapFs()
}

func TestHomeConfigDir(t *testing.T) {

homeDirPath := "/tmp-iofs/home/tester"
AppFs.MkdirAll(homeDirPath, 0755)

afero.WriteFile(AppFs, homeDirPath+"/revive.toml", []byte("\n"), 0644)
os.Setenv("HOME", homeDirPath)

got := buildDefaultConfigPath()
want := homeDirPath + "/revive.toml"

if got != want {
t.Errorf("got %q, wanted %q", got, want)
}

}

func TestXDGConfigDir(t *testing.T) {

xdgDirPath := "/tmp-iofs/xdg/config"
AppFs.MkdirAll(xdgDirPath, 0755)

afero.WriteFile(AppFs, xdgDirPath+"/revive.toml", []byte("\n"), 0644)
os.Setenv("XDG_CONFIG_HOME", xdgDirPath)

got := buildDefaultConfigPath()
want := xdgDirPath + "/revive.toml"

if got != want {
t.Errorf("got %q, wanted %q", got, want)
}

}

func TestXDGConfigDirNoFile(t *testing.T) {

xdgDirPath := "/tmp-iofs/xdg/config"
os.Setenv("XDG_CONFIG_HOME", xdgDirPath)

got := buildDefaultConfigPath()
want := xdgDirPath + "/revive.toml"

if got != want {
t.Errorf("got %q, wanted %q", got, want)
}
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.10.0
golang.org/x/tools v0.13.0
)

replace github.com/spf13/afero => github.com/spf13/afero v1.10.0

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
)
Loading

0 comments on commit 0eb4c9f

Please sign in to comment.