From b966bcfb044444790710aff7a570644d59080f2a Mon Sep 17 00:00:00 2001 From: Moritz Poldrack Date: Tue, 18 Apr 2023 19:03:09 +0200 Subject: [PATCH] use XDG_CONFIG_HOME by default To prevent having users always specify the configuration file, the path now defaults to $XDG_CONFIG_HOME/neosay/config.json. This way it is possible to send messages from a "default identity" like this: echo "look mom, I'm in someone's commit history!" | neosay To make the code more succinct, flag parsing is no longer done manually, but instead using the flag package from stdlib. Fixes: https://github.com/donuts-are-good/neosay/issues/2 Signed-off-by: Moritz Poldrack --- main.go | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index c4ad5f4..a358d92 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,11 @@ package main import ( "bufio" "encoding/json" + "flag" "fmt" "os" + "path" + "path/filepath" "strings" "time" @@ -22,27 +25,26 @@ type Config struct { const maxMessageSize = 4000 const messageDelay = 2 * time.Second -var codeFlag bool +var ( + configFile = flag.String("config", "$XDG_CONFIG_HOME/neosay/config.json", "path to the configuration file") + codeFlag = flag.Bool("code", false, "is this code?") +) func main() { + flag.Parse() - // name the argset - args := os.Args[1:] - - // if we get too few, supply the usage - if len(args) < 1 { - fmt.Fprintln(os.Stderr, colors.BrightRed+"Usage: neosay [-c | --code]"+colors.Nc) + home, err := os.UserHomeDir() + if err != nil { + fmt.Fprintf(os.Stderr, "%sfailed to get user's home directory. Well, shit.%s\n", colors.BrightRed, colors.Nc) os.Exit(1) } + xdgConfigHome := path.Join(home, ".config") + if env, _ := os.LookupEnv("XDG_CONFIG_HOME"); env != "" { + xdgConfigHome = env + } - // name the config file - configFile := args[0] - - // check for the args, which can be anywhere - for _, arg := range args[1:] { - if arg == "--code" || arg == "-c" { - codeFlag = true - } + if strings.Contains(*configFile, "$XDG_CONFIG_HOME") { + *configFile = filepath.Join(xdgConfigHome, "neosay", "config.json") } // open the config @@ -131,7 +133,7 @@ func main() { func sendMessage(client *gomatrix.Client, roomID, message string) { // if we use -c or --code, make it a ```this thing``` - if codeFlag { + if *codeFlag { message = "
" + message + "
" }