chore: config fixes
- remove example config file - don't set default values on read - omit empty fields - indent yaml 2 spaces
This commit is contained in:
parent
ba356137c3
commit
6952516204
@ -9,13 +9,13 @@ type Destination struct {
|
|||||||
// LogFile holds the configuration for the log file.
|
// LogFile holds the configuration for the log file.
|
||||||
type LogFile struct {
|
type LogFile struct {
|
||||||
Enabled bool `yaml:"enabled"`
|
Enabled bool `yaml:"enabled"`
|
||||||
Path string `yaml:"path"`
|
Path string `yaml:"path,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// RTMPSource holds the configuration for the RTMP source.
|
// RTMPSource holds the configuration for the RTMP source.
|
||||||
type RTMPSource struct {
|
type RTMPSource struct {
|
||||||
Enabled bool `yaml:"enabled"`
|
Enabled bool `yaml:"enabled"`
|
||||||
StreamKey string `yaml:"streamkey"`
|
StreamKey string `yaml:"streamkey,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sources holds the configuration for the sources.
|
// Sources holds the configuration for the sources.
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
# Octoplex is a live stream multiplexer.
|
|
||||||
---
|
|
||||||
#
|
|
||||||
sources:
|
|
||||||
# Currently the only source type is RTMP server.
|
|
||||||
rtmp:
|
|
||||||
enabled: yes
|
|
||||||
# Your local stream key. Defaults to "live".
|
|
||||||
#
|
|
||||||
# rtmp://localhost:1935/live
|
|
||||||
streamkey: live
|
|
||||||
#
|
|
||||||
logfile:
|
|
||||||
# Change to yes to log to system location.
|
|
||||||
enabled: no
|
|
||||||
# Or, log to this absolute path:
|
|
||||||
# path: octoplex.log
|
|
||||||
#
|
|
||||||
# Define your destinations here.
|
|
||||||
destinations:
|
|
||||||
# - name: YouTube
|
|
||||||
# url: rtmp://rtmp.youtube.com/myYoutubeStreamKey
|
|
||||||
# - name: Twitch
|
|
||||||
# url: rtmp://ingest.global-contribute.live-video.net/app/myTwitchStreamKey
|
|
@ -1,6 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -8,13 +9,9 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.netflux.io/rob/octoplex/internal/domain"
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed data/config.example.yml
|
|
||||||
var exampleConfig []byte
|
|
||||||
|
|
||||||
// Service provides configuration services.
|
// Service provides configuration services.
|
||||||
type Service struct {
|
type Service struct {
|
||||||
current Config
|
current Config
|
||||||
@ -100,7 +97,7 @@ func (s *Service) SetConfig(cfg Config) error {
|
|||||||
return fmt.Errorf("validate: %w", err)
|
return fmt.Errorf("validate: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfgBytes, err := yaml.Marshal(cfg)
|
cfgBytes, err := marshalConfig(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("marshal: %w", err)
|
return fmt.Errorf("marshal: %w", err)
|
||||||
}
|
}
|
||||||
@ -130,8 +127,6 @@ func (s *Service) readConfig() (cfg Config, _ error) {
|
|||||||
return cfg, fmt.Errorf("unmarshal: %w", err)
|
return cfg, fmt.Errorf("unmarshal: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.setDefaults(&cfg)
|
|
||||||
|
|
||||||
if err = validate(cfg); err != nil {
|
if err = validate(cfg); err != nil {
|
||||||
return cfg, err
|
return cfg, err
|
||||||
}
|
}
|
||||||
@ -143,17 +138,31 @@ func (s *Service) readConfig() (cfg Config, _ error) {
|
|||||||
|
|
||||||
func (s *Service) writeDefaultConfig() (Config, error) {
|
func (s *Service) writeDefaultConfig() (Config, error) {
|
||||||
var cfg Config
|
var cfg Config
|
||||||
if err := yaml.Unmarshal(exampleConfig, &cfg); err != nil {
|
s.setDefaults(&cfg)
|
||||||
return cfg, fmt.Errorf("unmarshal: %w", err)
|
|
||||||
|
cfgBytes, err := marshalConfig(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return cfg, fmt.Errorf("marshal: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.writeConfig(exampleConfig); err != nil {
|
if err := s.writeConfig(cfgBytes); err != nil {
|
||||||
return Config{}, fmt.Errorf("write config: %w", err)
|
return Config{}, fmt.Errorf("write config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func marshalConfig(cfg Config) ([]byte, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
enc := yaml.NewEncoder(&buf)
|
||||||
|
enc.SetIndent(2)
|
||||||
|
if err := enc.Encode(cfg); err != nil {
|
||||||
|
return nil, fmt.Errorf("encode: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Service) writeConfig(cfgBytes []byte) error {
|
func (s *Service) writeConfig(cfgBytes []byte) error {
|
||||||
if err := os.MkdirAll(s.appConfigDir, 0744); err != nil {
|
if err := os.MkdirAll(s.appConfigDir, 0744); err != nil {
|
||||||
return fmt.Errorf("mkdir: %w", err)
|
return fmt.Errorf("mkdir: %w", err)
|
||||||
@ -166,18 +175,10 @@ func (s *Service) writeConfig(cfgBytes []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setDefaults is called to set default values for a new, empty configuration.
|
||||||
func (s *Service) setDefaults(cfg *Config) {
|
func (s *Service) setDefaults(cfg *Config) {
|
||||||
if cfg.LogFile.Enabled && cfg.LogFile.Path == "" {
|
|
||||||
cfg.LogFile.Path = filepath.Join(s.appStateDir, domain.AppName+".log")
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg.Sources.RTMP.Enabled = true
|
cfg.Sources.RTMP.Enabled = true
|
||||||
|
cfg.Sources.RTMP.StreamKey = "live"
|
||||||
for i := range cfg.Destinations {
|
|
||||||
if strings.TrimSpace(cfg.Destinations[i].Name) == "" {
|
|
||||||
cfg.Destinations[i].Name = fmt.Sprintf("Stream %d", i+1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: validate URL format
|
// TODO: validate URL format
|
||||||
|
@ -4,27 +4,21 @@ import (
|
|||||||
_ "embed"
|
_ "embed"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.netflux.io/rob/octoplex/internal/config"
|
"git.netflux.io/rob/octoplex/internal/config"
|
||||||
"git.netflux.io/rob/octoplex/internal/shortid"
|
"git.netflux.io/rob/octoplex/internal/shortid"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed testdata/complete.yml
|
//go:embed testdata/complete.yml
|
||||||
var configComplete []byte
|
var configComplete []byte
|
||||||
|
|
||||||
//go:embed testdata/no-logfile.yml
|
|
||||||
var configNoLogfile []byte
|
|
||||||
|
|
||||||
//go:embed testdata/logfile.yml
|
//go:embed testdata/logfile.yml
|
||||||
var configLogfile []byte
|
var configLogfile []byte
|
||||||
|
|
||||||
//go:embed testdata/no-name.yml
|
|
||||||
var configNoName []byte
|
|
||||||
|
|
||||||
//go:embed testdata/invalid-destination-url.yml
|
//go:embed testdata/invalid-destination-url.yml
|
||||||
var configInvalidDestinationURL []byte
|
var configInvalidDestinationURL []byte
|
||||||
|
|
||||||
@ -60,8 +54,10 @@ func TestConfigServiceCreateConfig(t *testing.T) {
|
|||||||
p := filepath.Join(systemConfigDir, "octoplex", "config.yaml")
|
p := filepath.Join(systemConfigDir, "octoplex", "config.yaml")
|
||||||
cfgBytes, err := os.ReadFile(p)
|
cfgBytes, err := os.ReadFile(p)
|
||||||
require.NoError(t, err, "config file was not created")
|
require.NoError(t, err, "config file was not created")
|
||||||
// Ensure the example config file is written:
|
|
||||||
assert.Contains(t, string(cfgBytes), "# Octoplex is a live stream multiplexer.")
|
var readCfg config.Config
|
||||||
|
require.NoError(t, yaml.Unmarshal(cfgBytes, &readCfg))
|
||||||
|
assert.True(t, readCfg.Sources.RTMP.Enabled, "default values not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfigServiceReadConfig(t *testing.T) {
|
func TestConfigServiceReadConfig(t *testing.T) {
|
||||||
@ -97,13 +93,6 @@ func TestConfigServiceReadConfig(t *testing.T) {
|
|||||||
}, cfg)
|
}, cfg)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "logging enabled, no logfile",
|
|
||||||
configBytes: configNoLogfile,
|
|
||||||
want: func(t *testing.T, cfg config.Config) {
|
|
||||||
assert.True(t, strings.HasSuffix(cfg.LogFile.Path, "/octoplex/octoplex.log"))
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "logging enabled, logfile",
|
name: "logging enabled, logfile",
|
||||||
configBytes: configLogfile,
|
configBytes: configLogfile,
|
||||||
@ -111,13 +100,6 @@ func TestConfigServiceReadConfig(t *testing.T) {
|
|||||||
assert.Equal(t, "/tmp/octoplex.log", cfg.LogFile.Path)
|
assert.Equal(t, "/tmp/octoplex.log", cfg.LogFile.Path)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "no name",
|
|
||||||
configBytes: configNoName,
|
|
||||||
want: func(t *testing.T, cfg config.Config) {
|
|
||||||
assert.Equal(t, "Stream 1", cfg.Destinations[0].Name)
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "invalid destination URL",
|
name: "invalid destination URL",
|
||||||
configBytes: configInvalidDestinationURL,
|
configBytes: configInvalidDestinationURL,
|
||||||
|
5
internal/config/testdata/no-logfile.yml
vendored
5
internal/config/testdata/no-logfile.yml
vendored
@ -1,5 +0,0 @@
|
|||||||
---
|
|
||||||
logfile:
|
|
||||||
enabled: true
|
|
||||||
destinations:
|
|
||||||
- url: rtmp://rtmp.example.com:1935/live
|
|
6
internal/config/testdata/no-name.yml
vendored
6
internal/config/testdata/no-name.yml
vendored
@ -1,6 +0,0 @@
|
|||||||
---
|
|
||||||
logfile:
|
|
||||||
enabled: true
|
|
||||||
path: test.log
|
|
||||||
destinations:
|
|
||||||
- url: rtmp://rtmp.example.com:1935/live
|
|
12
main.go
12
main.go
@ -49,7 +49,7 @@ func run(ctx context.Context) error {
|
|||||||
} else if narg == 1 {
|
} else if narg == 1 {
|
||||||
switch flag.Arg(0) {
|
switch flag.Arg(0) {
|
||||||
case "edit-config":
|
case "edit-config":
|
||||||
return editConfigFile(configService.Path())
|
return editConfigFile(configService)
|
||||||
case "print-config":
|
case "print-config":
|
||||||
return printConfigPath(configService.Path())
|
return printConfigPath(configService.Path())
|
||||||
case "version":
|
case "version":
|
||||||
@ -108,7 +108,11 @@ func run(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// editConfigFile opens the config file in the user's editor.
|
// editConfigFile opens the config file in the user's editor.
|
||||||
func editConfigFile(configPath string) error {
|
func editConfigFile(configService *config.Service) error {
|
||||||
|
if _, err := configService.ReadOrCreateConfig(); err != nil {
|
||||||
|
return fmt.Errorf("read or create config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
editor := os.Getenv("EDITOR")
|
editor := os.Getenv("EDITOR")
|
||||||
if editor == "" {
|
if editor == "" {
|
||||||
editor = "vi"
|
editor = "vi"
|
||||||
@ -118,10 +122,10 @@ func editConfigFile(configPath string) error {
|
|||||||
return fmt.Errorf("look path: %w", err)
|
return fmt.Errorf("look path: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "Editing config file: %s\n", configPath)
|
fmt.Fprintf(os.Stderr, "Editing config file: %s\n", configService.Path())
|
||||||
fmt.Println(binary)
|
fmt.Println(binary)
|
||||||
|
|
||||||
if err := syscall.Exec(binary, []string{"--", configPath}, os.Environ()); err != nil {
|
if err := syscall.Exec(binary, []string{"--", configService.Path()}, os.Environ()); err != nil {
|
||||||
return fmt.Errorf("exec: %w", err)
|
return fmt.Errorf("exec: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user