diff --git a/README.md b/README.md index 2871b61..4aa1ee0 100644 --- a/README.md +++ b/README.md @@ -53,10 +53,10 @@ Configuration fields: ## Import and archive workflow -Run the default command without a subcommand: +Run the `import` subcommand: ```bash -docspell-cli +docspell-cli import ``` The command logs in with `dsc`, scans every configured import directory, and checks each file against Docspell. @@ -76,8 +76,8 @@ If the Docspell item has no folder, `null` is used as the folder name. The corre By default, files that do not exist in Docspell stay in place. Enable uploads with either the flag or environment variable: ```bash -docspell-cli --upload-missing -DS_CC_UPLOAD_MISSING=true docspell-cli +docspell-cli import --upload-missing +DS_CC_UPLOAD_MISSING=true docspell-cli import ``` Uploaded files are not archived immediately. They are re-checked on the next run, after Docspell has processed and confirmed them. diff --git a/main.go b/main.go index 20c2cc1..243e2c3 100644 --- a/main.go +++ b/main.go @@ -38,31 +38,80 @@ type DocspellItem struct { } func main() { - cfg, password := loadConfigAndPassword() + if len(os.Args) < 2 { + printUsage(os.Stderr) + return + } - if len(os.Args) > 1 && os.Args[1] == "mount" { + switch os.Args[1] { + case "import": + if isHelpRequest(os.Args[2:]) { + runImportCommand(config{}, "", os.Args[2:]) + return + } + cfg, password := loadConfigAndPassword() + runImportCommand(cfg, password, os.Args[2:]) + case "mount": + if isHelpRequest(os.Args[2:]) { + _ = runMountCommand(os.Args[2:], config{}, "") + return + } + cfg, password := loadConfigAndPassword() if err := runMountCommand(os.Args[2:], cfg, password); err != nil { fmt.Fprintln(os.Stderr, "mount failed:", err) os.Exit(1) } - return - } - if len(os.Args) > 1 && os.Args[1] == "get-path" { + case "get-path": + if isHelpRequest(os.Args[2:]) { + _ = runGetPathCommand(os.Args[2:], config{}, "") + return + } + cfg, password := loadConfigAndPassword() if err := runGetPathCommand(os.Args[2:], cfg, password); err != nil { fmt.Fprintln(os.Stderr, "get-path failed:", err) os.Exit(1) } - return - } - if len(os.Args) > 1 && os.Args[1] == "get-url" { + case "get-url": + if isHelpRequest(os.Args[2:]) { + _ = runGetURLCommand(os.Args[2:], config{}, "") + return + } + cfg, password := loadConfigAndPassword() if err := runGetURLCommand(os.Args[2:], cfg, password); err != nil { fmt.Fprintln(os.Stderr, "get-url failed:", err) os.Exit(1) } - return + case "-h", "--help", "help": + printUsage(os.Stdout) + default: + fmt.Fprintf(os.Stderr, "unknown command %q\n\n", os.Args[1]) + printUsage(os.Stderr) + os.Exit(2) } +} - runImportCommand(cfg, password, os.Args[1:]) +func isHelpRequest(args []string) bool { + for _, arg := range args { + if arg == "-h" || arg == "--help" { + return true + } + } + return false +} + +func printUsage(out *os.File) { + name := filepath.Base(os.Args[0]) + fmt.Fprintf(out, `Usage: + %[1]s [flags] + +Commands: + import Scan configured import folders, archive confirmed files, and optionally upload missing files + mount Expose Docspell search results as a read-only FUSE filesystem + get-path Resolve a Docspell item ID or URL to the local mounted path + get-url Resolve a local mounted path, item ID, or item URL to a Docspell web URL + +Run "%[1]s -h" for command-specific help. +`, name) } func loadConfigAndPassword() (config, string) {