mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
Merge pull request #8340 from jbouwman/files-cp-parent
Add flag to create parent directories in files cp command
This commit is contained in:
commit
299fc0b725
@ -356,12 +356,21 @@ GC'ed.
|
||||
cmds.StringArg("source", true, false, "Source IPFS or MFS path to copy."),
|
||||
cmds.StringArg("dest", true, false, "Destination within MFS."),
|
||||
},
|
||||
Options: []cmds.Option{
|
||||
cmds.BoolOption(filesParentsOptionName, "p", "Make parent directories as needed."),
|
||||
},
|
||||
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||
mkParents, _ := req.Options[filesParentsOptionName].(bool)
|
||||
nd, err := cmdenv.GetNode(env)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
prefix, err := getPrefixNew(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
api, err := cmdenv.GetApi(env, req)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -389,6 +398,13 @@ GC'ed.
|
||||
return fmt.Errorf("cp: cannot get node from path %s: %s", src, err)
|
||||
}
|
||||
|
||||
if mkParents {
|
||||
err := ensureContainingDirectoryExists(nd.FilesRoot, dst, prefix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err = mfs.PutNode(nd.FilesRoot, dst, node)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cp: cannot put node in path %s: %s", dst, err)
|
||||
|
||||
@ -296,6 +296,20 @@ test_files_api() {
|
||||
ipfs files cp /ipfs/$FILE3 /cats/this/is/a/dir/file3
|
||||
'
|
||||
|
||||
test_expect_success "can copy file into deep dir using -p flag $EXTRA" '
|
||||
ipfs files cp -p /ipfs/$FILE3 /cats/some/other/dir/file3
|
||||
'
|
||||
|
||||
test_expect_success "file copied into deep dir exists $EXTRA" '
|
||||
ipfs files read /cats/some/other/dir/file3 > file_out &&
|
||||
echo "baz" > file_exp &&
|
||||
test_cmp file_out file_exp
|
||||
'
|
||||
|
||||
test_expect_success "cleanup deep cp -p test $EXTRA" '
|
||||
ipfs files rm -r /cats/some
|
||||
'
|
||||
|
||||
test_expect_success "can read file $EXTRA" '
|
||||
ipfs files read /cats/this/is/a/dir/file3 > output
|
||||
'
|
||||
|
||||
Loading…
Reference in New Issue
Block a user