From 95ce7fcceeede4917cf28eb4dea67f1bf7cc586d Mon Sep 17 00:00:00 2001 From: Tony Blyler Date: Sat, 14 May 2016 10:22:10 -0400 Subject: [PATCH] Support YAML for the configuration and rearrange the config struct --- README.md | 81 ++++++++++++++++++++++----------------------- cmd/hoarder/main.go | 4 +-- queue/queue.go | 50 ++++++++++++++++------------ 3 files changed, 69 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 0d929d7..c875d0b 100644 --- a/README.md +++ b/README.md @@ -10,61 +10,58 @@ Uploads .torrent files from a local "blackhole" to a remote (SSH) rtorrent watch # Configuration ## Example -Ignore the keys that start with an underscore, they are comments. -```json -{ - "_rtorrent_addr": "The address to the rtorrent XMLRPC endpoint", - "rtorrent_addr": "mycoolrtorrentserver.com/XMLRPC", +```yaml +# The file mode to use for downloaded files and directories +file_download_filemode: 0777 - "_rtorrent_insecure_cert": "true to ignore the certificate authenticity; false to honor it", - "rtorrent_insecure_cert": false, +# The correlation of .torrent file paths and where their contents should be downloaded to" +watch_to_download_paths: + /home/tblyler/torrent_files/tv: /home/tblyler/Downloads/tv + /home/tblyler/torrent_files/movies: /home/tblyler/Downloads/movies + /home/tblyler/torrent_files: /home/tblyler/Downloads - "_torrent_username": "The HTTP Basic auth username to use for rtorrent's XMLRPC", - "rtorrent_username": "JohnDoe", +# The root path to temporarily download to and then move to the folder in the setting above. The destination path is created underneath the temp_download_path +temp_download_path: /home/tblyler/tempDownloads - "_rtorrent_password": "The HTTP Basic auth password to use for rtorrent's XMLRPC", - "rtorrent_password": "correct horse battery staple", +# If defined, the finished .torrent files finished are moved to their respected path here. Otherwise, they are deleted. +watch_to_finish_path: + /home/tblyler/torrent_files/tv: /home/tblyler/completed_torrent_files/tv + /home/tblyler/torrent_files: /home/tblyler/completed_torrent_files - "_ssh_username": "The ssh username to use for getting finished torrents from the remote host", - "ssh_username": "JohnDoe", +# The time in nano seconds to update the list of torrents and their statuses in rTorrent +rtorrent_update_interval: 60000000000 - "_SSH_AUTH_COMMENT": "You may choose to use an ssh key or ssh password. If both are supplied, the password will not be used.", +# The number of concurrent completed torrent downloads to have at one time +download_jobs: 2 - "_ssh_password": "The SSH password to use for SSH authentication", - "ssh_password": "correct horse battery staple SSH", +rtorrent: + # The address to the rtorrent XMLRPC endpoint + addr: mycoolrtorrentserver.com/XMLRPC - "_ssh_privkey_path": "The path to the private SSH key for SSH authentication", - "ssh_privkey_path": "/home/tblyler/.ssh/id_rsa", + # true to ignore the certificate authenticity; false to honor it + insecure_cert: false - "_ssh_addr": "The SSH address to connect to", - "ssh_addr": "mysshserver.com:22", + # The HTTP Basic auth username to use for rtorrent's XMLRPC + username: JohnDoe - "_ssh_connect_timeout": "The time in nano seconds to wait for an SSH connection attempt", - "ssh_connect_timeout": 30000000000, + # The HTTP Basic auth password to use for rtorrent's XMLRPC + password: "correct horse battery staple" - "_file_download_filemode": "The base 10 file mode to use for downloaded files", - "file_download_filemode": 511, +ssh: + # The ssh username to use for getting finished torrents from the remote host + username: JohnDoe - "_watch_to_download_paths": "The correlation of .torrent file paths and where their contents should be downloaded to", - "watch_to_download_paths": { - "/home/tblyler/torrent_files/tv": "/home/tblyler/Downloads/tv", - "/home/tblyler/torrent_files/movies": "/home/tblyler/Downloads/movies", - "/home/tblyler/torrent_files": "/home/tblyler/Downloads" - }, + # You may choose to use an ssh key or ssh password. If both are supplied, the password will not be used. - "_temp_download_path": "The root path to temporarily download to and then move to the folder in the setting above. The destination path is created underneath the temp_download_path", - "temp_download_path": "/home/tblyler/tempDownloads", + # The SSH password to use for SSH authentication + password: "correct horse battery staple SSH" - "_watch_to_finish_path": "If defined, the finished .torrent files finished are moved to their respected path here. Otherwise, they are deleted.", - "watch_to_finish_path": { - "/home/tblyler/torrent_files/tv": "/home/tblyler/completed_torrent_files/tv", - "/home/tblyler/torrent_files": "/home/tblyler/completed_torrent_files" - }, + # The path to the private SSH key for SSH authentication + privkey_path: /home/tblyler/.ssh/id_rsa - "_rtorrent_update_interval": "The time in nano seconds to update the list of torrents and their statuses in rTorrent", - "rtorrent_update_interval": 300000000000, + # The SSH address to connect to + addr: "mysshserver.com:22" - "_download_jobs": "The number of concurrent download streams to have at one time", - "download_jobs": 2 -} + # The time in nano seconds to wait for an SSH connection attempt + connect_timeout: 30000000000 ``` diff --git a/cmd/hoarder/main.go b/cmd/hoarder/main.go index f79566a..bed490b 100644 --- a/cmd/hoarder/main.go +++ b/cmd/hoarder/main.go @@ -1,9 +1,9 @@ package main import ( - "encoding/json" "flag" "github.com/tblyler/hoarder/queue" + "gopkg.in/yaml.v2" "io/ioutil" "log" "os" @@ -28,7 +28,7 @@ func main() { } config := &queue.Config{} - err = json.Unmarshal(configRaw, config) + err = yaml.Unmarshal(configRaw, config) if err != nil { logger.Printf("Unable to decode config json at '%s': '%s'", *configPath, err) os.Exit(1) diff --git a/queue/queue.go b/queue/queue.go index ba522e1..368986a 100644 --- a/queue/queue.go +++ b/queue/queue.go @@ -19,21 +19,27 @@ import ( // Config defines the settings for watching, uploading, and downloading type Config struct { - RtorrentAddr string `json:"rtorrent_addr"` - RtorrentInsecureCert bool `json:"rtorrent_insecure_cert"` - RtorrentUsername string `json:"rtorrent_username"` - RtorrentPassword string `json:"rtorrent_password"` - SSHUsername string `json:"ssh_username"` - SSHPassword string `json:"ssh_password"` - SSHKeyPath string `json:"ssh_privkey_path"` - SSHAddr string `json:"ssh_addr"` - SSHTimeout time.Duration `json:"ssh_connect_timeout"` - DownloadFileMode os.FileMode `json:"file_download_filemode"` - WatchDownloadPaths map[string]string `json:"watch_to_download_paths"` - TempDownloadPath string `json:"temp_download_path"` - FinishedTorrentFilePath map[string]string `json:"watch_to_finish_path"` - TorrentListUpdateInterval time.Duration `json:"rtorrent_update_interval"` - ConcurrentDownloads uint `json:"download_jobs"` + Rtorrent struct { + Addr string `json:"addr" yaml:"addr"` + InsecureCert bool `json:"insecure_cert" yaml:"insecure_cert"` + Username string `json:"username" yaml:"username"` + Password string `json:"password" yaml:"password"` + } `json:"rtorrent" yaml:"rtorrent,flow"` + + SSH struct { + Username string `json:"username" yaml:"username"` + Password string `json:"password" yaml:"password"` + KeyPath string `json:"privkey_path" yaml:"privkey_path"` + Addr string `json:"addr" yaml:"addr"` + Timeout time.Duration `json:"connect_timeout" yaml:"connect_timeout"` + } `json:"ssh" yaml:"ssh,flow"` + + DownloadFileMode os.FileMode `json:"file_download_filemode" yaml:"file_download_filemode"` + WatchDownloadPaths map[string]string `json:"watch_to_download_paths" yaml:"watch_to_download_paths,flow"` + TempDownloadPath string `json:"temp_download_path" yaml:"temp_download_path"` + FinishedTorrentFilePath map[string]string `json:"watch_to_finish_path" yaml:"watch_to_finish_path,flow"` + TorrentListUpdateInterval time.Duration `json:"rtorrent_update_interval" yaml:"rtorrent_update_interval"` + ConcurrentDownloads uint `json:"download_jobs" yaml:"download_jobs"` } // Queue watches the given folders for new .torrent files, @@ -116,15 +122,15 @@ func NewQueue(config *Config, logger *log.Logger) (*Queue, error) { } } - rtClient := rtorrent.New(config.RtorrentAddr, config.RtorrentInsecureCert) - rtClient.SetAuth(config.RtorrentUsername, config.RtorrentPassword) + rtClient := rtorrent.New(config.Rtorrent.Addr, config.Rtorrent.InsecureCert) + rtClient.SetAuth(config.Rtorrent.Username, config.Rtorrent.Password) sftpClient, err := easysftp.Connect(&easysftp.ClientConfig{ - Username: config.SSHUsername, - Password: config.SSHPassword, - KeyPath: config.SSHKeyPath, - Host: config.SSHAddr, - Timeout: config.SSHTimeout, + Username: config.SSH.Username, + Password: config.SSH.Password, + KeyPath: config.SSH.KeyPath, + Host: config.SSH.Addr, + Timeout: config.SSH.Timeout, FileMode: config.DownloadFileMode, }) if err != nil {