Skip to content

Commit

Permalink
Add support key=value pair for cli config
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 13, 2024
1 parent e4ff6d2 commit 8f2bb3f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
54 changes: 54 additions & 0 deletions internal/app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
- By default go2rtc will search config file `go2rtc.yaml` in current work directory
- go2rtc support multiple config files
- go2rtc support inline config as `YAML`, `JSON` or `key=value` format from command line
- Every next config will overwrite previous (but only defined params)

```
go2rtc -config "{log: {format: text}}" -config /config/go2rtc.yaml -config "{rtsp: {listen: ''}}" -config /usr/local/go2rtc/go2rtc.yaml
```

or simple version

```
go2rtc -c log.format=text -c /config/go2rtc.yaml -c rtsp.listen='' -c /usr/local/go2rtc/go2rtc.yaml
```

## Environment variables

Also go2rtc support templates for using environment variables in any part of config:

```yaml
streams:
camera1: rtsp://rtsp:${CAMERA_PASSWORD}@192.168.1.123/av_stream/ch0

${LOGS:} # empty default value

rtsp:
username: ${RTSP_USER:admin} # "admin" if env "RTSP_USER" not set
password: ${RTSP_PASS:secret} # "secret" if env "RTSP_PASS" not set
```
## Defaults
```yaml
api:
listen: ":1984"

ffmpeg:
bin: "ffmpeg"

log:
level: "info"

rtsp:
listen: ":8554"
default_query: "video&audio"

srtp:
listen: ":8443"

webrtc:
listen: ":8555/tcp"
ice_servers:
- urls: [ "stun:stun.l.google.com:19302" ]
```
38 changes: 32 additions & 6 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,26 @@ func Init() {
}

for _, conf := range confs {
if conf[0] != '{' {
if len(conf) == 0 {
continue
}
if conf[0] == '{' {
// config as raw YAML or JSON
configs = append(configs, []byte(conf))
} else if data := parseConfString(conf); data != nil {
configs = append(configs, data)
} else {
// config as file
if ConfigPath == "" {
ConfigPath = conf
}

data, _ := os.ReadFile(conf)
if data == nil {
if data, _ = os.ReadFile(conf); data == nil {
continue
}

data = []byte(shell.ReplaceEnvVars(string(data)))
configs = append(configs, data)
} else {
// config as raw YAML
configs = append(configs, []byte(conf))
}
}

Expand Down Expand Up @@ -190,3 +194,25 @@ func readRevisionTime() (revision, vcsTime string) {
}
return
}

func parseConfString(s string) []byte {
i := strings.IndexByte(s, '=')
if i < 0 {
return nil
}

items := strings.Split(s[:i], ".")
if len(items) < 2 {
return nil
}

// `log.level=trace` => `{log: {level: trace}}`
var pre string
var suf = s[i+1:]
for _, item := range items {
pre += "{" + item + ": "
suf += "}"
}

return []byte(pre + suf)
}

0 comments on commit 8f2bb3f

Please sign in to comment.