From 40564c5052ae707047735ce7c374ab9ee0ba9386 Mon Sep 17 00:00:00 2001 From: John Rinehart Date: Tue, 17 Sep 2019 08:38:25 -0700 Subject: [PATCH] support global config (e.g. for company-wide settings) (#233) * support global config (e.g. for company-wide settings) * documented global config behavior --- README.md | 4 +++- config.go | 22 ++++++++++++++++++++-- go.mod | 2 ++ go.sum | 5 +++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e96e042..df55bc6 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,8 @@ Here's how `revive` is different from `golint`: Since the default behavior of `revive` is compatible with `golint`, without providing any additional flags, the only difference you'd notice is faster execution. +`revive` supports a `-config` flag whose value should correspond to a TOML file describing which rules to use for `revive`'s linting. If not provided, `revive` will try to use a global config file (assumed to be located at `$HOME/revive.toml`). Otherwise, if no configuration TOML file is found then `revive` uses a built-in set of default linting rules. + ### Bazel If you want to use revive with Bazel, take a look at the [rules](https://github.com/atlassian/bazel-tools/tree/master/gorevive) that Atlassian maintains. @@ -128,7 +130,7 @@ go get -u github.com/mgechev/revive `revive` accepts three command line parameters: -- `-config [PATH]` - path to config file in TOML format. +- `-config [PATH]` - path to config file in TOML format, defaults to `$HOME/revive.toml` if present. - `-exclude [PATTERN]` - pattern for files/directories/packages to be excluded for linting. You can specify the files you want to exclude for linting either as package name (i.e. `github.com/mgechev/revive`), list them as individual files (i.e. `file.go`), directories (i.e. `./foo/...`), or any combination of the three. - `-formatter [NAME]` - formatter to be used for the output. The currently available formatters are: diff --git a/config.go b/config.go index e6bb1af..508ba6e 100644 --- a/config.go +++ b/config.go @@ -5,9 +5,11 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" "strings" "github.com/mgechev/dots" + "github.com/mitchellh/go-homedir" "github.com/mgechev/revive/formatter" @@ -173,6 +175,18 @@ func getFormatter() lint.Formatter { return formatter } +func buildDefaultConfigPath() string { + var result string + if homeDir, err := homedir.Dir(); err == nil { + result = filepath.Join(homeDir, "revive.toml") + if _, err := os.Stat(result); err != nil { + result = "" + } + } + + return result +} + func defaultConfig() *lint.Config { defaultConfig := lint.Config{ Confidence: 0.0, @@ -233,12 +247,16 @@ func init() { fmt.Println(banner) originalUsage() } + // command line help strings const ( - configUsage = "path to the configuration TOML file (i.e. -config myconf.toml)" + configUsage = "path to the configuration TOML file, defaults to $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)" ) - flag.StringVar(&configPath, "config", "", configUsage) + + defaultConfigPath := buildDefaultConfigPath() + + flag.StringVar(&configPath, "config", defaultConfigPath, configUsage) flag.Var(&excludePaths, "exclude", excludeUsage) flag.StringVar(&formatterName, "formatter", "", formatterUsage) flag.Parse() diff --git a/go.mod b/go.mod index b9ea41a..1576033 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,8 @@ require ( github.com/mattn/go-isatty v0.0.9 // indirect github.com/mattn/go-runewidth v0.0.4 // indirect github.com/mgechev/dots v0.0.0-20181228164730-18fa4c4b71cc + github.com/mitchellh/go-homedir v1.1.0 github.com/olekukonko/tablewriter v0.0.1 + github.com/pkg/errors v0.8.1 golang.org/x/tools v0.0.0-20190816200558-6889da9d5479 ) diff --git a/go.sum b/go.sum index c1d92be..dd56e66 100644 --- a/go.sum +++ b/go.sum @@ -13,13 +13,18 @@ github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/ github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mgechev/dots v0.0.0-20181228164730-18fa4c4b71cc h1:SQHr6jXnsY5YmRoO7RWDcZjmC3PgwPW/xQ9TYJ/SiRY= github.com/mgechev/dots v0.0.0-20181228164730-18fa4c4b71cc/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/olekukonko/tablewriter v0.0.1 h1:b3iUnf1v+ppJiOfNX4yxxqfWKMQPZR5yoh8urCTFX88= github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190816200558-6889da9d5479 h1:lfN2PY/jymfnxkNHlbBF5DwPsUvhqUnrdgfK01iH2s0=