add env file resolver

This commit is contained in:
onyx-and-iris 2025-05-02 12:24:46 +01:00
parent 8cf969c906
commit 30fabe8cfc
5 changed files with 39 additions and 11 deletions

11
.gitignore vendored
View File

@ -26,6 +26,15 @@ go.work
# End of gignore: github.com/onyx-and-iris/gignore
# Environment
.env
.envrc
*_test.go
# Man pages
gobs-cli.1
# Config files
config.yaml
# Test files
*_test.go

View File

@ -22,17 +22,19 @@ gobs-cli --host=localhost --port=4455 --password=<websocket 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=<websocket password>
export OBS_TIMEOUT=5
```env
OBS_HOST=localhost
OBS_PORT=4455
OBS_PASSWORD=<websocket password>
OBS_TIMEOUT=5
```
## Commands
### VersionCmd
@ -434,3 +436,5 @@ gobs-cli virtualcam toggle
```console
gobs-cli virtualcam status
```
[userconfigdir]: https://pkg.go.dev/os#UserConfigDir

2
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

11
main.go
View File

@ -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)