diff --git a/README.md b/README.md index a115d62..2c6edcd 100644 --- a/README.md +++ b/README.md @@ -233,6 +233,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/api/sync15/blobdoc.go b/api/sync15/blobdoc.go index 3aab17d..46199ce 100644 --- a/api/sync15/blobdoc.go +++ b/api/sync15/blobdoc.go @@ -214,7 +214,7 @@ func (d *BlobDoc) LineWithSchema(schema string) string { // Mirror updates the document to be the same as the remote func (d *BlobDoc) Mirror(e *Entry, r RemoteStorage) error { d.Entry = *e - entryIndex, err := r.GetReader(e.Hash, e.DocumentID) + entryIndex, err := r.GetReader(e.Hash, addExt(e.DocumentID, archive.DocSchemaExt)) if err != nil { return err } diff --git a/shell/get.go b/shell/get.go index 88c7fac..644a20e 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" + "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 := pflag.NewFlagSet("get", pflag.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()))) }, } -} +} \ No newline at end of file