Skip to content

Commit

Permalink
Load config from different locations prefer XDG_CONFIG_HOME before HO…
Browse files Browse the repository at this point in the history
…ME (#912)
  • Loading branch information
mfederowicz authored Oct 3, 2023
1 parent d631844 commit cd2737a
Show file tree
Hide file tree
Showing 5 changed files with 545 additions and 8 deletions.
32 changes: 26 additions & 6 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ 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 is used to operations related with user config files
AppFs = afero.NewOsFs()
)

func fail(err string) {
Expand All @@ -29,6 +32,9 @@ func fail(err string) {

// RunRevive runs the CLI for revive.
func RunRevive(extraRules ...revivelib.ExtraRule) {
// 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 +111,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 +143,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 +190,8 @@ func init() {
os.Exit(0)
}
}

func fileExist(path string) bool {
_, err := AppFs.Stat(path)
return err == nil
}
81 changes: 81 additions & 0 deletions cli/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cli

import (
"os"
"testing"

"github.com/spf13/afero"
)

func init() {
os.Unsetenv("HOME")
os.Unsetenv("XDG_CONFIG_HOME")
AppFs = afero.NewMemMapFs()
}

func TestXDGConfigDirIsPrefferedFirst(t *testing.T) {

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

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

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

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

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

//reset fs after test
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 cd2737a

Please sign in to comment.