-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
107 lines (93 loc) · 2.86 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package open_in_mpv
import (
"log"
"path/filepath"
"strings"
"github.com/shibukawa/configdir"
"gopkg.in/yaml.v2"
)
// The Player struct contains useful informations for an mpv-based player,
// such as binary name and fullscreen/pip/enqueue/new_window flags overrides for
// use in GenerateIPC(). A way to override any generic flags is also
// provided through the map FlagOverrides.
type Player struct {
// Full name of the player
Name string
// Executable path/name (if already in $PATH) for the player
Executable string
// Flag override for fullscreen
Fullscreen string
// Flag override for picture-in-picture mode
Pip string
// Flag override for enqueuing videos
Enqueue string
// Flag override for forcing new window
NewWindow string `yaml:"new_window"`
// Controls whether this player needs IPC command to enqueue videos
NeedsIpc bool `yaml:"needs_ipc"`
// Controls which (video URL) schemes are to be opened by the current
// player. There is no match-all, each protocol has to be manuall specified
SupportedSchemes []string `yaml:"supported_protocols"`
// Overrides for any generic flag
FlagOverrides map[string]string `yaml:"flag_overrides"`
}
// Top-level configuration object, maps a player by name to its Player object
type Config struct {
Players map[string]Player
}
var defaultSupportedSchemas = []string{
"http",
"https",
}
var defaultConfig = Config{
Players: map[string]Player{
"mpv": {
Name: "mpv",
Executable: "mpv",
Fullscreen: "--fs",
Pip: `--ontop --no-border --autofit=384x216 --geometry=98%:98%`,
Enqueue: "",
NewWindow: "",
NeedsIpc: true,
SupportedSchemes: defaultSupportedSchemas,
FlagOverrides: map[string]string{},
},
},
}
// Tries to load configuration file with fallback to a default configuration
// object
func LoadConfig() error {
confDirs := configdir.New("", "open-in-mpv")
confDirs.LocalPath, _ = filepath.Abs(".")
confFolder := confDirs.QueryFolderContainsFile("config.yml")
if confFolder == nil {
log.Println("No config file found, using default")
return nil
}
data, err := confFolder.ReadFile("config.yml")
if err != nil {
return err
}
err = yaml.Unmarshal(data, &defaultConfig)
if err != nil {
return err
}
// If the player has no external configuration, use strict defaults
for name, player := range defaultConfig.Players {
if len(player.SupportedSchemes) == 0 {
log.Printf("Player '%s' has no schemas, setting to defaults", player.Name)
player.SupportedSchemes = defaultSupportedSchemas
defaultConfig.Players[name] = player
}
// TODO: restrict/escape custom flags
}
return nil
}
// Returns player information for the given name if present, otherwise nil
func GetPlayerConfig(name string) *Player {
lowerName := strings.ToLower(name)
if p, ok := defaultConfig.Players[lowerName]; ok {
return &p
}
return nil
}