From 083c66dca7d09134af28e62aebbeba29b42d98d0 Mon Sep 17 00:00:00 2001 From: rouggy Date: Thu, 9 Apr 2026 19:55:44 +0200 Subject: [PATCH] added logs --- hardlink.go | 3 ++- main.go | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/hardlink.go b/hardlink.go index 5309d91..0d1d572 100644 --- a/hardlink.go +++ b/hardlink.go @@ -19,7 +19,8 @@ func processHardlinks(cfg *Config, contentPath, destSubdir string) error { destDir := filepath.Join(cfg.DestBase, destSubdir) if info.IsDir() { - return hardlinkDir(contentPath, destDir) + // Preserve the source directory name inside destDir + return hardlinkDir(contentPath, filepath.Join(destDir, filepath.Base(contentPath))) } return hardlinkFile(contentPath, filepath.Join(destDir, filepath.Base(contentPath))) } diff --git a/main.go b/main.go index 138f86e..42a1abb 100644 --- a/main.go +++ b/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 --content-path [--name ] [--config ]") + fmt.Fprintln(os.Stderr, "Usage: qbhardlink --category --content-path [--name ] [--config ] [--log ]") 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)