91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func defaultConfigPath() string {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
home = "."
|
|
}
|
|
return filepath.Join(home, ".config", "qbhardlink", "config.yaml")
|
|
}
|
|
|
|
func defaultLogPath() string {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
home = "."
|
|
}
|
|
return filepath.Join(home, ".config", "qbhardlink", "qbhardlink.log")
|
|
}
|
|
|
|
func setupLogger(logPath string) (*os.File, error) {
|
|
if err := os.MkdirAll(filepath.Dir(logPath), 0755); err != nil {
|
|
return nil, err
|
|
}
|
|
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
log.SetOutput(f)
|
|
return f, nil
|
|
}
|
|
|
|
func main() {
|
|
log.SetFlags(log.Ldate | log.Ltime)
|
|
|
|
var (
|
|
name = flag.String("name", "", "Torrent name (%N)")
|
|
category = flag.String("category", "", "Torrent category (%L) [required]")
|
|
contentPath = flag.String("content-path", "", "Content path (%F) [required]")
|
|
configPath = flag.String("config", defaultConfigPath(), "Path to config YAML file")
|
|
logPath = flag.String("log", defaultLogPath(), "Path to log file (empty to disable)")
|
|
)
|
|
flag.Parse()
|
|
|
|
if *category == "" || *contentPath == "" {
|
|
fmt.Fprintln(os.Stderr, "Usage: qbhardlink --category <cat> --content-path <path> [--name <name>] [--config <path>] [--log <path>]")
|
|
fmt.Fprintln(os.Stderr, "")
|
|
fmt.Fprintln(os.Stderr, "qBittorrent run-on-finish example:")
|
|
os.Stderr.WriteString(" /usr/local/bin/qbhardlink --name \"%N\" --category \"%L\" --content-path \"%F\"\n")
|
|
os.Exit(1)
|
|
}
|
|
|
|
if *logPath != "" {
|
|
f, err := setupLogger(*logPath)
|
|
if err != nil {
|
|
log.Printf("warning: could not open log file %q: %v (logging to stderr)", *logPath, err)
|
|
} else {
|
|
defer f.Close()
|
|
}
|
|
}
|
|
|
|
log.Printf("--- start ---")
|
|
log.Printf("name=%q category=%q content-path=%q", *name, *category, *contentPath)
|
|
log.Printf("config=%q", *configPath)
|
|
|
|
cfg, err := loadConfig(*configPath)
|
|
if err != nil {
|
|
log.Fatalf("config: %v", err)
|
|
}
|
|
|
|
destSubdir, ok := cfg.Categories[*category]
|
|
if !ok {
|
|
log.Printf("category %q not in config, nothing to do", *category)
|
|
os.Exit(0)
|
|
}
|
|
|
|
log.Printf("category %q -> dest subdir %q", *category, destSubdir)
|
|
|
|
if err := processHardlinks(cfg, *contentPath, destSubdir, *name); err != nil {
|
|
log.Fatalf("error: %v", err)
|
|
}
|
|
|
|
log.Printf("done")
|
|
}
|