refactor: extract streaming engine into internal/flinger
Move the capture/encode/send pipeline out of cmd/teleportfling into a GUI-free Engine so the CLI and the desktop app share one implementation: - flinger: Config/Engine/Status, audio+video+stats loops, pattern source - config: JSON persistence at ~/.config/teleportfling/config.json - cmd/teleportfling: thin CLI wrapper around the engine (same flags) - golangci: extend exclusions to flinger/config
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
// Package config loads and saves the teleportfling settings file.
|
||||
//
|
||||
// The file is JSON at ~/.config/teleportfling/config.json and holds the
|
||||
// user-visible knobs exposed by the GUI (name, port, quality, fps, source,
|
||||
// audio, monitor). Secrets and runtime state are deliberately excluded.
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"teleportfling/internal/flinger"
|
||||
)
|
||||
|
||||
// Config mirrors flinger.Config with JSON tags for persistence.
|
||||
type Config struct {
|
||||
Name string `json:"name"`
|
||||
Port int `json:"port"`
|
||||
Quality int `json:"quality"`
|
||||
FPS int `json:"fps"`
|
||||
Source string `json:"source"`
|
||||
Audio bool `json:"audio"`
|
||||
StreamIndex int `json:"stream_index"`
|
||||
}
|
||||
|
||||
// Default returns the default configuration.
|
||||
func Default() Config {
|
||||
c := flinger.DefaultConfig()
|
||||
return Config{
|
||||
Name: c.Name,
|
||||
Port: c.Port,
|
||||
Quality: c.Quality,
|
||||
FPS: c.FPS,
|
||||
Source: c.Source,
|
||||
Audio: c.Audio,
|
||||
StreamIndex: c.StreamIndex,
|
||||
}
|
||||
}
|
||||
|
||||
// ToFlinger converts a persisted config to the engine config.
|
||||
func (c Config) ToFlinger() flinger.Config {
|
||||
return flinger.Config{
|
||||
Name: c.Name,
|
||||
Port: c.Port,
|
||||
Quality: c.Quality,
|
||||
FPS: c.FPS,
|
||||
Source: c.Source,
|
||||
Audio: c.Audio,
|
||||
StreamIndex: c.StreamIndex,
|
||||
}
|
||||
}
|
||||
|
||||
// pathVar is the config file location; overridable in tests.
|
||||
var pathVar = func() string {
|
||||
dir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
dir = "."
|
||||
}
|
||||
return filepath.Join(dir, "teleportfling", "config.json")
|
||||
}()
|
||||
|
||||
// Path returns the config file location.
|
||||
func Path() string {
|
||||
return pathVar
|
||||
}
|
||||
|
||||
// Load reads the config file, returning Default when it does not exist.
|
||||
func Load() (Config, error) {
|
||||
p := Path()
|
||||
data, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return Default(), nil
|
||||
}
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
var c Config
|
||||
if err := json.Unmarshal(data, &c); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
// Fill any zero values with defaults so a hand-edited file still works.
|
||||
d := Default()
|
||||
if c.Port == 0 {
|
||||
c.Port = d.Port
|
||||
}
|
||||
if c.Quality == 0 {
|
||||
c.Quality = d.Quality
|
||||
}
|
||||
if c.FPS == 0 {
|
||||
c.FPS = d.FPS
|
||||
}
|
||||
if c.Source == "" {
|
||||
c.Source = d.Source
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Save writes the config file, creating the directory if needed.
|
||||
func Save(c Config) error {
|
||||
p := Path()
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.MarshalIndent(c, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(p, data, 0o600)
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestLoadDefaultsWhenMissing verifies Load returns defaults when the file
|
||||
// does not exist.
|
||||
func TestLoadDefaultsWhenMissing(t *testing.T) {
|
||||
// Point at a non-existent directory so we never touch the real config.
|
||||
old := pathVar
|
||||
pathVar = filepath.Join(t.TempDir(), "nope", "config.json")
|
||||
defer func() { pathVar = old }()
|
||||
|
||||
c, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if c.Port != 9756 {
|
||||
t.Errorf("default port = %d, want 9756", c.Port)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSaveLoadRoundTrip writes a config and reads it back.
|
||||
func TestSaveLoadRoundTrip(t *testing.T) {
|
||||
old := pathVar
|
||||
pathVar = filepath.Join(t.TempDir(), "config.json")
|
||||
defer func() { pathVar = old }()
|
||||
|
||||
want := Config{
|
||||
Name: "Studio",
|
||||
Port: 9898,
|
||||
Quality: 95,
|
||||
FPS: 60,
|
||||
Source: "pattern",
|
||||
Audio: false,
|
||||
StreamIndex: 1,
|
||||
}
|
||||
if err := Save(want); err != nil {
|
||||
t.Fatalf("Save: %v", err)
|
||||
}
|
||||
|
||||
got, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if got != want {
|
||||
t.Errorf("round trip mismatch:\n got %+v\nwant %+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadFillsZeroValues ensures a partial file gets defaults filled.
|
||||
func TestLoadFillsZeroValues(t *testing.T) {
|
||||
old := pathVar
|
||||
pathVar = filepath.Join(t.TempDir(), "config.json")
|
||||
defer func() { pathVar = old }()
|
||||
|
||||
if err := os.WriteFile(pathVar, []byte(`{"name":"Partial"}`), 0o600); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
|
||||
c, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if c.Name != "Partial" {
|
||||
t.Errorf("name = %q, want Partial", c.Name)
|
||||
}
|
||||
if c.Port != 9756 {
|
||||
t.Errorf("port = %d, want filled default 9756", c.Port)
|
||||
}
|
||||
if c.Quality != 80 {
|
||||
t.Errorf("quality = %d, want filled default 80", c.Quality)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user