From 6e3a24c20910c7220448f091a326f840f919e3b4 Mon Sep 17 00:00:00 2001 From: "Sean E. Russell" Date: Wed, 15 May 2024 09:13:50 -0500 Subject: [PATCH] Allows server connection to be supplied on command line --- stmps.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/stmps.go b/stmps.go index 3d63985..c9fbb15 100644 --- a/stmps.go +++ b/stmps.go @@ -6,6 +6,7 @@ package main import ( "flag" "fmt" + "net/url" "os" "runtime" @@ -37,17 +38,44 @@ func readConfig() { } } +// parseConfig takes the first non-flag arguments from flags and parses it +// into the viper config. +func parseConfig() { + if u, e := url.Parse(flag.Arg(0)); e == nil { + // If credentials were provided + if len(u.User.Username()) > 0 { + viper.Set("auth.username", u.User.Username()) + // If the password wasn't provided, the program will fail as normal + if p, s := u.User.Password(); s { + viper.Set("auth.password", p) + } + } + // Blank out the credentials so we can use the URL formatting + u.User = nil + viper.Set("server.host", u.String()) + } else { + fmt.Printf("Invalid server format; must be a valid URL: http[s]://[user:pass@]server:port") + fmt.Printf("USAGE: %s [http[s]://[user:pass@]server:port]\n", os.Args[0]) + flag.Usage() + os.Exit(1) + } +} + func main() { help := flag.Bool("help", false, "Print usage") enableMpris := flag.Bool("mpris", false, "Enable MPRIS2") flag.Parse() if *help { - fmt.Printf("USAGE: %s \n", os.Args[0]) + fmt.Printf("USAGE: %s [[user:pass@]server:port]\n", os.Args[0]) flag.Usage() os.Exit(0) } - readConfig() + if len(flag.Args()) > 0 { + parseConfig() + } else { + readConfig() + } logger := logger.Init()