added logs
This commit is contained in:
40
main.go
40
main.go
@@ -16,6 +16,26 @@ func defaultConfigPath() string {
|
||||
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)
|
||||
|
||||
@@ -24,17 +44,31 @@ func main() {
|
||||
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>]")
|
||||
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)
|
||||
@@ -42,11 +76,11 @@ func main() {
|
||||
|
||||
destSubdir, ok := cfg.Categories[*category]
|
||||
if !ok {
|
||||
log.Printf("category %q not in config, nothing to do (torrent: %s)", *category, *name)
|
||||
log.Printf("category %q not in config, nothing to do", *category)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
log.Printf("processing torrent %q (category: %s -> %s)", *name, *category, destSubdir)
|
||||
log.Printf("category %q -> dest subdir %q", *category, destSubdir)
|
||||
|
||||
if err := processHardlinks(cfg, *contentPath, destSubdir); err != nil {
|
||||
log.Fatalf("error: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user