Add destinations configuration

This commit is contained in:
Justin Hawkins 2022-04-09 23:32:57 +09:30
parent 5c362df35d
commit 4f33603a0c
3 changed files with 69 additions and 8 deletions

View File

@ -19,21 +19,31 @@ type Server struct {
MaximumActiveDownloads int `yaml:"maximum_active_downloads_per_domain" json:"maximum_active_downloads_per_domain"` MaximumActiveDownloads int `yaml:"maximum_active_downloads_per_domain" json:"maximum_active_downloads_per_domain"`
} }
// DownloadProfile holds the details for executing a downloader
type DownloadProfile struct { type DownloadProfile struct {
Name string `yaml:"name" json:"name"` Name string `yaml:"name" json:"name"`
Command string `yaml:"command" json:"command"` Command string `yaml:"command" json:"command"`
Args []string `yaml:"args" json:"args"` Args []string `yaml:"args" json:"args"`
} }
// UI holds the configuration for the user interface
type UI struct { type UI struct {
PopupWidth int `yaml:"popup_width" json:"popup_width"` PopupWidth int `yaml:"popup_width" json:"popup_width"`
PopupHeight int `yaml:"popup_height" json:"popup_height"` PopupHeight int `yaml:"popup_height" json:"popup_height"`
} }
// Destination is the path for a place that a download can be moved to
type Destination struct {
Name string `yaml:"name" json:"name"` // Name for this location
Path string `yaml:"path" json:"path"` // Path on disk
}
// Config is the top level of the user configuration
type Config struct { type Config struct {
ConfigVersion int `yaml:"config_version" json:"config_version"` ConfigVersion int `yaml:"config_version" json:"config_version"`
Server Server `yaml:"server" json:"server"` Server Server `yaml:"server" json:"server"`
UI UI `yaml:"ui" json:"ui"` UI UI `yaml:"ui" json:"ui"`
Destinations []Destination `yaml:"destinations" json:"destinations"`
DownloadProfiles []DownloadProfile `yaml:"profiles" json:"profiles"` DownloadProfiles []DownloadProfile `yaml:"profiles" json:"profiles"`
} }
@ -76,7 +86,9 @@ func (cs *ConfigService) LoadDefaultConfig() {
defaultConfig.Server.MaximumActiveDownloads = 2 defaultConfig.Server.MaximumActiveDownloads = 2
defaultConfig.ConfigVersion = 2 defaultConfig.Destinations = make([]Destination, 0)
defaultConfig.ConfigVersion = 3
cs.Config = &defaultConfig cs.Config = &defaultConfig
@ -238,6 +250,14 @@ func (cs *ConfigService) LoadConfig() error {
c.ConfigVersion = 2 c.ConfigVersion = 2
configMigrated = true configMigrated = true
log.Print("migrated config from version 1 => 2") log.Print("migrated config from version 1 => 2")
}
if c.ConfigVersion == 2 {
c.Destinations = make([]Destination, 0)
c.ConfigVersion = 3
configMigrated = true
log.Print("migrated config from version 2 => 3")
} }
if configMigrated { if configMigrated {

View File

@ -5,7 +5,7 @@ import (
"testing" "testing"
) )
func TestMigrationV1toV2(t *testing.T) { func TestMigrationV1toV3(t *testing.T) {
v2Config := `config_version: 1 v2Config := `config_version: 1
server: server:
port: 6123 port: 6123
@ -36,13 +36,16 @@ profiles:
if err != nil { if err != nil {
t.Errorf("got error when loading config: %s", err) t.Errorf("got error when loading config: %s", err)
} }
if cs.Config.ConfigVersion != 2 { if cs.Config.ConfigVersion != 3 {
t.Errorf("did not migrate version (it is '%d')", cs.Config.ConfigVersion) t.Errorf("did not migrate version (it is '%d')", cs.Config.ConfigVersion)
} }
if cs.Config.Server.MaximumActiveDownloads != 2 { if cs.Config.Server.MaximumActiveDownloads != 2 {
t.Error("did not add MaximumActiveDownloads") t.Error("did not add MaximumActiveDownloads")
} }
t.Log(cs.ConfigPath) if len(cs.Config.Destinations) != 0 {
t.Error("incorrect number of destinations added")
}
os.Remove(cs.ConfigPath)
} }
func configServiceFromString(configString string) *ConfigService { func configServiceFromString(configString string) *ConfigService {

View File

@ -7,11 +7,17 @@
<p class="error" x-show="error_message" x-transition.duration.500ms x-text="error_message"></p> <p class="error" x-show="error_message" x-transition.duration.500ms x-text="error_message"></p>
<p class="success" x-show="success_message" x-transition.duration.500ms x-text="success_message"></p> <p class="success" x-show="success_message" x-transition.duration.500ms x-text="success_message"></p>
<p>Note: changes are not saved until the "Save Config" button is pressed at the bottom of the page.</p> <p>Note: changes are not saved until the "Save Config" button is pressed.</p>
<div class="pure-g">
<div class="pure-u-1">
<button class="pure-button pure-button-primary" @click="save_config();" href="#">Save Config</button>
</div>
</div>
<div class="pure-g"> <div class="pure-g">
<div class="pure-u-md-1-2 pure-u-1 l-box"> <div class="pure-u-lg-1-3 pure-u-1 l-box">
<form class="pure-form pure-form-stacked gropple-config"> <form class="pure-form pure-form-stacked gropple-config">
<fieldset> <fieldset>
@ -53,7 +59,7 @@
</form> </form>
</div> </div>
<div class="pure-u-md-1-2 pure-u-1 l-box"> <div class="pure-u-lg-1-3 pure-u-1 l-box">
<form class="pure-form gropple-config"> <form class="pure-form gropple-config">
<fieldset> <fieldset>
@ -103,6 +109,38 @@
</fieldset> </fieldset>
</form> </form>
</div> </div>
<div class="pure-u-lg-1-3 pure-u-1 l-box">
<form class="pure-form gropple-config">
<fieldset>
<legend>Destinations</legend>
<p>You can specify custom destinations here. You can specify that
downloads be moved to one of these directories after complete.</p>
</p>
<template x-for="(dest, i) in config.destinations">
<div>
<label x-bind:for="'config-destinations-'+i+'-name'">Name of destination <span x-text="i+1"></span>
</label>
<input type="text" x-bind:id="'config-destinations-'+i+'-name'" class="input-long" placeholder="name" x-model="dest.name" />
<span class="pure-form-message">The name of this destination. For your information only.</span>
<label x-bind:for="'config-destinations-'+i+'-command'">Path</label>
<input type="text" x-bind:id="'config-destinations-'+i+'-command'" class="input-long" placeholder="name" x-model="dest.path" />
<span class="pure-form-message">Path to move completed downloads to.</span>
<button class="pure-button button-del" href="#" @click.prevent="config.destinations.splice(i, 1);">delete destination</button>
<hr>
</div>
</template>
<button class="pure-button button-add" href="#" @click.prevent="config.destinations.push({name: 'new destination', path: '/'});">add destination</button>
</fieldset>
</form>
</div> </div>
<div class="pure-g"> <div class="pure-g">
<div class="pure-u-1"> <div class="pure-u-1">
@ -119,7 +157,7 @@
<script> <script>
function config() { function config() {
return { return {
config: { server : {}, ui : {}, profiles: [] }, config: { server : {}, ui : {}, profiles: [], destinations: []},
error_message: '', error_message: '',
success_message: '', success_message: '',