diff --git a/.gitignore b/.gitignore index 6098107..9d4f24b 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,15 @@ go.work # End of gignore: github.com/onyx-and-iris/gignore +# Environment +.env .envrc -*_test.go -gobs-cli.1 \ No newline at end of file + +# Man pages +gobs-cli.1 + +# Config files +config.yaml + +# Test files +*_test.go \ No newline at end of file diff --git a/README.md b/README.md index 63e0727..6993ee6 100644 --- a/README.md +++ b/README.md @@ -22,17 +22,19 @@ gobs-cli --host=localhost --port=4455 --password= --help #### Environment Variables -Load connection details from your environment: +Store and load environment variables from: -```bash -#!/usr/bin/env bash +- A `.env` file in the cwd +- $XDG_CONFIG_HOME / gobs-cli / config.env (see [os.UserConfigDir][userconfigdir]) -export OBS_HOST=localhost -export OBS_PORT=4455 -export OBS_PASSWORD= -export OBS_TIMEOUT=5 +```env +OBS_HOST=localhost +OBS_PORT=4455 +OBS_PASSWORD= +OBS_TIMEOUT=5 ``` + ## Commands ### VersionCmd @@ -433,4 +435,6 @@ gobs-cli virtualcam toggle ```console gobs-cli virtualcam status -``` \ No newline at end of file +``` + +[userconfigdir]: https://pkg.go.dev/os#UserConfigDir \ No newline at end of file diff --git a/go.mod b/go.mod index 43e9918..a2f96b4 100644 --- a/go.mod +++ b/go.mod @@ -6,12 +6,14 @@ require ( github.com/alecthomas/kong v1.10.0 github.com/alecthomas/mango-kong v0.1.0 github.com/andreykaipov/goobs v1.5.6 + github.com/titusjaka/kong-dotenv-go v0.1.0 ) require ( github.com/buger/jsonparser v1.1.1 // indirect github.com/gorilla/websocket v1.5.3 // indirect github.com/hashicorp/logutils v1.0.0 // indirect + github.com/joho/godotenv v1.5.1 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mmcloughlin/profile v0.1.1 // indirect github.com/muesli/mango v0.1.1-0.20220205060214-77e2058169ab // indirect diff --git a/go.sum b/go.sum index 0e107c5..150d5db 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mmcloughlin/profile v0.1.1 h1:jhDmAqPyebOsVDOCICJoINoLb/AnLBaUw58nFzxWS2w= @@ -32,5 +34,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/titusjaka/kong-dotenv-go v0.1.0 h1:TmUjP/sXoNiKLr6oR7n9xrB5XyXi/Ssuebzfz5nxZj4= +github.com/titusjaka/kong-dotenv-go v0.1.0/go.mod h1:pBgLjcu82oqUgb7+bngK9+Ch7jg49E0YADP8Wnj2MXU= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index d991b99..8521451 100644 --- a/main.go +++ b/main.go @@ -7,11 +7,13 @@ import ( "fmt" "io" "os" + "path/filepath" "time" "github.com/alecthomas/kong" mangokong "github.com/alecthomas/mango-kong" "github.com/andreykaipov/goobs" + kongdotenv "github.com/titusjaka/kong-dotenv-go" ) // ObsConfig holds the configuration for connecting to the OBS WebSocket server. @@ -49,11 +51,18 @@ type context struct { } func main() { - cli := cli{} + userConfigDir, err := os.UserConfigDir() + if err != nil { + fmt.Fprintf(os.Stderr, "Error getting user config directory: %v\n", err) + os.Exit(1) + } + + var cli cli ctx := kong.Parse( &cli, kong.Name("GOBS-CLI"), kong.Description("A command line tool to interact with OBS Websocket."), + kong.Configuration(kongdotenv.ENVFileReader, ".env", filepath.Join(userConfigDir, "gobs-cli", "config.env")), ) client, err := connectObs(cli.ObsConfig)