diff --git a/README.md b/README.md index c48abb2..9522a4d 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,7 @@ find / "Journal-..-..-2024" # Or just search for all files with 2024 in the filename find / ".*2024.*" # If you want to search ignore character casing, you can do that as follows: -find / "(?!i)case_insensitive_search" +find / "(?i)case_insensitive_search" # Find files with either "Work" or "Personal" tag find --tag="Work" --tag="Personal" @@ -227,6 +227,20 @@ mput (-src sourcfolder) /Papers Use `get path_to_file` to download a file from the cloud to your local computer. +### Download flags + +- `--id`: Interpret the argument as a document ID instead of a path. This is useful when you have the document ID from the `find` command's JSON output. + +Examples: + +```bash +# Download by path +get /Books/MyBook + +# Download by document ID (find document ID using the stat command or --json flag) +get --id abc123-def456-789 +``` + ## Recursively download directories and files Use `mget path_to_dir` to recursively download all the files in that directory. diff --git a/shell/find.go b/shell/find.go index 21df97f..38e4d6c 100644 --- a/shell/find.go +++ b/shell/find.go @@ -62,7 +62,7 @@ func findCmd(ctx *ShellCtxt) *ishell.Cmd { case 0: start = ctx.path default: - c.Err(errors.New("missing arguments; usage find [options] [dir] [regexp]")) + flagSet.Usage() return } @@ -87,6 +87,11 @@ func findCmd(ctx *ShellCtxt) *ishell.Cmd { filetree.WalkTree(startNode, filetree.FileTreeVistor{ Visit: func(node *model.Node, path []string) bool { + // Skip items in trash + if node.Document != nil && node.Document.Parent == "trash" { + return false + } + // Filter by starred status if flag was set if starredFilterEnabled && node.Document != nil { if node.Document.Starred != starred { diff --git a/shell/get.go b/shell/get.go index 88c7fac..95f849f 100644 --- a/shell/get.go +++ b/shell/get.go @@ -5,11 +5,13 @@ import ( "fmt" "github.com/abiosoft/ishell" + "github.com/juruen/rmapi/model" "github.com/juruen/rmapi/util" + flag "github.com/ogier/pflag" ) func getCmd(ctx *ShellCtxt) *ishell.Cmd { - longHelp := `Usage: get ` + longHelp := `Usage: get [--id] ` return &ishell.Cmd{ Name: "get", @@ -17,25 +19,43 @@ func getCmd(ctx *ShellCtxt) *ishell.Cmd { Completer: createEntryCompleter(ctx), LongHelp: longHelp, Func: func(c *ishell.Context) { - if checkHelp(longHelp, c.Args, c) { + flagSet := flag.NewFlagSet("get", flag.ContinueOnError) + var byId bool + flagSet.BoolVar(&byId, "id", false, "interpret argument as document ID instead of path") + if !processFlagSet(flagSet, longHelp, c.Args, c) { return } + args := flagSet.Args() - if len(c.Args) == 0 { - c.Err(errors.New("missing source file")) + if len(args) == 0 { + c.Err(errors.New("missing source file or id")) return } - srcName := c.Args[0] + srcArg := args[0] + var node *model.Node + var err error - node, err := ctx.api.Filetree().NodeByPath(srcName, ctx.node) + if byId { + node = ctx.api.Filetree().NodeById(srcArg) + if node == nil { + c.Err(errors.New("document with given ID doesn't exist")) + return + } + } else { + node, err = ctx.api.Filetree().NodeByPath(srcArg, ctx.node) + if err != nil { + c.Err(errors.New("file doesn't exist")) + return + } + } - if err != nil || node.IsDirectory() { - c.Err(errors.New("file doesn't exist")) + if node.IsDirectory() { + c.Err(errors.New("cannot download a directory")) return } - c.Println(fmt.Sprintf("downloading: [%s]...", srcName)) + c.Println(fmt.Sprintf("downloading: [%s]...", node.Name())) err = ctx.api.FetchDocument(node.Document.ID, fmt.Sprintf("%s.%s", node.Name(), util.RMDOC)) @@ -44,7 +64,7 @@ func getCmd(ctx *ShellCtxt) *ishell.Cmd { return } - c.Err(errors.New(fmt.Sprintf("Failed to download file %s with %s", srcName, err.Error()))) + c.Err(errors.New(fmt.Sprintf("Failed to download file %s with %s", node.Name(), err.Error()))) }, } }