XDG_CONFIG_HOME support
5 files changed, 46 insertions(+), 11 deletions(-)

M CHANGELOG.md
M README.md
M go.mod
M go.sum
M main.go
M CHANGELOG.md +6 -0
@@ 13,6 13,12 @@ and this project adheres to [Semantic Ve
 > - **Fixed**: for any bug fixes.
 > - **Security**: in case of vulnerabilities.
 
+## [1.1.0] - 2020-04-04 
+
+### Added
+
+- Supports XDG_CONFIG_DIR
+
 ## [1.0.0] - 2020-04-03
 
 First release.

          
M README.md +6 -0
@@ 39,6 39,12 @@ You can specify as many rules for as man
 
 The `-d` debug flag can be useful for testing rules without executing the commands. It can be tricky finding a single event (and, usually, you want the *last* event) in the series of events udev will report for a given device; generally, you want to try to find a rule that will execute exactly once per plug event.  For example, when I connect that Goldtouch keyboard above, udev spews out a dozen `add` events that seem to correspond with each function the keyboard supports, so it took a bit to find a rule that triggered only once.
 
+Put the rules file in `$XDG_CONFIG_HOME/kbplug/rules.json` or specify the file path on the command line, like this:
+
+```
+kbplug myrules.json
+```
+
 ## Installation
 
 **kbplug** is `go get`table:

          
M go.mod +4 -1
@@ 2,4 2,7 @@ module code.ser1.net/kbplug
 
 go 1.14
 
-require github.com/pilebones/go-udev v0.0.0-20180820235104-043677e09b13
+require (
+	github.com/pilebones/go-udev v0.0.0-20180820235104-043677e09b13
+	github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0
+)

          
M go.sum +2 -0
@@ 1,2 1,4 @@ 
 github.com/pilebones/go-udev v0.0.0-20180820235104-043677e09b13 h1:Y+ynP+0QIjUejN2tsuIlWOJG1CThJy6amRuWlBL94Vg=
 github.com/pilebones/go-udev v0.0.0-20180820235104-043677e09b13/go.mod h1:MXAPLpvZeTqLpU1eO6kFXzU0uBMooSGc1MPXAcBoy1M=
+github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0 h1:Xuk8ma/ibJ1fOy4Ee11vHhUFHQNpHhrBneOCNHVXS5w=
+github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0/go.mod h1:7AwjWCpdPhkSmNAgUv5C7EJ4AbmjEB3r047r3DXWu3Y=

          
M main.go +28 -10
@@ 9,13 9,15 @@ import (
 	"os"
 	"os/exec"
 	"os/signal"
+	"path/filepath"
 	"regexp"
 	"syscall"
 
 	"github.com/pilebones/go-udev/netlink"
+	"github.com/shibukawa/configdir"
 )
 
-var Version string
+var Version, BuildDate string = "dev", "N/A"
 
 func main() {
 	help := flag.Bool("h", false, "Print help and exit")

          
@@ 24,7 26,7 @@ func main() {
 	flag.Parse()
 	usage := fmt.Sprintf("USAGE: %s <rules-file>\n", os.Args[0])
 	if *version {
-		fmt.Println(Version)
+		fmt.Printf("%s (built %s)\n", Version, BuildDate)
 		os.Exit(0)
 	}
 	if *help {

          
@@ 33,11 35,31 @@ func main() {
 		fmt.Printf("\nThe only *required* argument is the path to the rules file.\n")
 		os.Exit(0)
 	}
+	var rules []byte
+	var err error
+	rulesFN := "rules.json"
 	if flag.NArg() < 1 {
-		fmt.Printf("The only *required* argument is the path to the rules file; this was missing.\n\n")
-		fmt.Printf(usage)
-		flag.PrintDefaults()
-		os.Exit(1)
+		cd := configdir.New("", "kbplug")
+		cd.LocalPath, _ = filepath.Abs(".")
+		cfg := cd.QueryFolderContainsFile(rulesFN)
+		if cfg == nil {
+			ds := cd.QueryFolders(configdir.All)
+			dirs := make([]string, len(ds))
+			for i, d := range ds {
+				dirs[i] = d.Path
+			}
+			fmt.Printf("No config file found in %v, and none supplied on the command line.\n\n", dirs)
+			fmt.Printf(usage)
+			flag.PrintDefaults()
+			os.Exit(1)
+		} else {
+			rules, err = cfg.ReadFile(rulesFN)
+		}
+	} else {
+		rules, err = ioutil.ReadFile(flag.Arg(0))
+		if err != nil {
+			panic(err)
+		}
 	}
 	conn := new(netlink.UEventConn)
 	if err := conn.Connect(netlink.UdevEvent); err != nil {

          
@@ 48,10 70,6 @@ func main() {
 	queue := make(chan netlink.UEvent)
 	errors := make(chan error)
 	matcher := new(netlink.RuleDefinitions)
-	rules, err := ioutil.ReadFile(flag.Arg(0))
-	if err != nil {
-		panic(err)
-	}
 	if err := json.Unmarshal(rules, &matcher); err != nil {
 		log.Fatalf("Wrong rule syntax in \"%s\", err: %s", flag.Arg(0), err.Error())
 	}