Destinations are now DownloadOptions

This commit is contained in:
2023-11-20 21:01:50 +10:30
parent fa978fecc2
commit a1e6421842
12 changed files with 200 additions and 139 deletions

View File

@@ -27,6 +27,12 @@ type DownloadProfile struct {
Args []string `yaml:"args" json:"args"`
}
// DownloadOption contains configuration for extra arguments to pass to the download command
type DownloadOption struct {
Name string `yaml:"name" json:"name"`
Args []string `yaml:"args" json:"args"`
}
// UI holds the configuration for the user interface
type UI struct {
PopupWidth int `yaml:"popup_width" json:"popup_width"`
@@ -44,8 +50,9 @@ type Config struct {
ConfigVersion int `yaml:"config_version" json:"config_version"`
Server Server `yaml:"server" json:"server"`
UI UI `yaml:"ui" json:"ui"`
Destinations []Destination `yaml:"destinations" json:"destinations"`
Destinations []Destination `yaml:"destinations" json:"destinations"` // no longer in use, see DownloadOptions
DownloadProfiles []DownloadProfile `yaml:"profiles" json:"profiles"`
DownloadOptions []DownloadOption `yaml:"download_options" json:"download_options"`
}
// ConfigService is a struct to handle configuration requests, allowing for the
@@ -88,7 +95,8 @@ func (cs *ConfigService) LoadDefaultConfig() {
defaultConfig.Server.MaximumActiveDownloads = 2
defaultConfig.Destinations = make([]Destination, 0)
defaultConfig.Destinations = nil
defaultConfig.DownloadOptions = make([]DownloadOption, 0)
defaultConfig.ConfigVersion = 3
@@ -96,7 +104,7 @@ func (cs *ConfigService) LoadDefaultConfig() {
}
// ProfileCalled returns the corresponding profile, or nil if it does not exist
// ProfileCalled returns the corresponding DownloadProfile, or nil if it does not exist
func (c *Config) ProfileCalled(name string) *DownloadProfile {
for _, p := range c.DownloadProfiles {
if p.Name == name {
@@ -106,11 +114,11 @@ func (c *Config) ProfileCalled(name string) *DownloadProfile {
return nil
}
// DestinationCalled returns the corresponding destination, or nil if it does not exist
func (c *Config) DestinationCalled(name string) *Destination {
for _, p := range c.Destinations {
if p.Name == name {
return &p
// DownloadOptionCalled returns the corresponding DownloadOption, or nil if it does not exist
func (c *Config) DownloadOptionCalled(name string) *DownloadOption {
for _, o := range c.DownloadOptions {
if o.Name == name {
return &o
}
}
return nil
@@ -187,17 +195,6 @@ func (c *Config) UpdateFromJSON(j []byte) error {
}
}
// check destinations
for _, dest := range newConfig.Destinations {
s, err := os.Stat(dest.Path)
if err != nil {
return fmt.Errorf("destination '%s' (%s) is bad: %s", dest.Name, dest.Path, err)
}
if !s.IsDir() {
return fmt.Errorf("destination '%s' (%s) is not a directory", dest.Name, dest.Path)
}
}
*c = newConfig
return nil
}
@@ -295,6 +292,20 @@ func (cs *ConfigService) LoadConfig() error {
log.Print("migrated config from version 2 => 3")
}
if c.ConfigVersion == 3 {
c.ConfigVersion = 4
for i := range c.Destinations {
newDownloadOption := DownloadOption{
Name: c.Destinations[i].Name,
Args: []string{"-o", fmt.Sprintf("%s/%%(title)s [%%(id)s].%%(ext)s", c.Destinations[i].Path)},
}
c.DownloadOptions = append(c.DownloadOptions, newDownloadOption)
c.Destinations = nil
}
configMigrated = true
log.Print("migrated config from version 3 => 4")
}
if configMigrated {
log.Print("Writing new config after version migration")
cs.WriteConfig()

View File

@@ -3,10 +3,12 @@ package config
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMigrationV1toV3(t *testing.T) {
v2Config := `config_version: 1
func TestMigrationV1toV4(t *testing.T) {
v1Config := `config_version: 1
server:
port: 6123
address: http://localhost:6123
@@ -31,12 +33,12 @@ profiles:
- --audio-format
- mp3
`
cs := configServiceFromString(v2Config)
cs := configServiceFromString(v1Config)
err := cs.LoadConfig()
if err != nil {
t.Errorf("got error when loading config: %s", err)
}
if cs.Config.ConfigVersion != 3 {
if cs.Config.ConfigVersion != 4 {
t.Errorf("did not migrate version (it is '%d')", cs.Config.ConfigVersion)
}
if cs.Config.Server.MaximumActiveDownloads != 2 {
@@ -48,6 +50,58 @@ profiles:
os.Remove(cs.ConfigPath)
}
func TestMigrateV3toV4(t *testing.T) {
v3Config := `config_version: 3
server:
port: 6123
address: http://localhost:6123
download_path: /tmp/Downloads
maximum_active_downloads_per_domain: 2
ui:
popup_width: 900
popup_height: 900
destinations:
- name: cool destination
path: /tmp/coolness
profiles:
- name: standard video
command: yt-dlp
args:
- --newline
- --write-info-json
- -f
- bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best
- name: standard mp3
command: yt-dlp
args:
- --newline
- --write-info-json
- --extract-audio
- --audio-format
- mp3`
cs := configServiceFromString(v3Config)
err := cs.LoadConfig()
if err != nil {
t.Errorf("got error when loading config: %s", err)
}
if cs.Config.ConfigVersion != 4 {
t.Errorf("did not migrate version (it is '%d')", cs.Config.ConfigVersion)
}
if cs.Config.Server.MaximumActiveDownloads != 2 {
t.Error("did not add MaximumActiveDownloads")
}
if len(cs.Config.Destinations) != 0 {
t.Error("incorrect number of destinations from migrated file")
}
if assert.Len(t, cs.Config.DownloadOptions, 1) {
if assert.Len(t, cs.Config.DownloadOptions[0].Args, 2) {
assert.Equal(t, "-o", cs.Config.DownloadOptions[0].Args[0])
assert.Equal(t, "/tmp/coolness/%(title)s [%(id)s].%(ext)s", cs.Config.DownloadOptions[0].Args[1])
}
}
os.Remove(cs.ConfigPath)
}
func configServiceFromString(configString string) *ConfigService {
tmpFile, _ := os.CreateTemp("", "gropple_test_*.yml")
tmpFile.Write([]byte(configString))