Skip to content

Commit

Permalink
fixup! Big refactor of newfile() / newdir() + BlobTree API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
c42f committed Apr 26, 2022
1 parent 4872395 commit 144353b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
14 changes: 5 additions & 9 deletions src/BlobTree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,8 @@ end

function _check_new_item(tree, path, overwrite)
_check_writeable(tree)
if haskey(tree, path)
if overwrite
delete!(tree, path)
else
error("Overwriting a path $path which already exists requires the keyword `overwrite=true`")
end
if haskey(tree, path) && !overwrite
error("Overwriting a path $path which already exists requires the keyword `overwrite=true`")
end
end

Expand All @@ -417,7 +413,7 @@ collection.
function newdir(tree::BlobTree, path::RelPath; overwrite=false)
_check_new_item(tree, path, overwrite)
p = joinpath(tree.path, RelPath(path))
newdir(tree.root, p)
newdir(tree.root, p; overwrite=overwrite)
return BlobTree(tree.root, p)
end

Expand Down Expand Up @@ -445,14 +441,14 @@ end
function newfile(tree::BlobTree, path::RelPath; overwrite=false)
_check_new_item(tree, path, overwrite)
p = joinpath(tree.path, path)
newfile(tree.root, p)
newfile(tree.root, p; overwrite=overwrite)
return Blob(tree.root, p)
end

function newfile(func::Function, tree::BlobTree, path::RelPath; overwrite=false)
_check_new_item(tree, path, overwrite)
p = joinpath(tree.path, path)
newfile(func, tree.root, p)
newfile(func, tree.root, p; overwrite=overwrite)
return Blob(tree.root, p)
end

Expand Down
14 changes: 11 additions & 3 deletions src/filesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,12 @@ function newdir()
return BlobTree(TempFilesystemRoot(path))
end

function newdir(root::AbstractFileSystemRoot, path::RelPath)
mkpath(sys_abspath(root, path))
function newdir(root::AbstractFileSystemRoot, path::RelPath; overwrite=false)
p = sys_abspath(root, path)
if overwrite
rm(p, force=true, recursive=true)
end
mkpath(p)
end


Expand All @@ -174,12 +178,16 @@ function newfile(func=nothing)
return Blob(TempFilesystemRoot(path))
end

function newfile(f::Function, root::AbstractFileSystemRoot, path::RelPath)
function newfile(f::Function, root::AbstractFileSystemRoot, path::RelPath; kws...)
p = sys_abspath(root, path)
mkpath(dirname(p))
open(f, p, write=true)
end

function newfile(root::AbstractFileSystemRoot, path::RelPath; kws...)
newfile(io->nothing, root, path; kws...)
end

#-------------------------------------------------------------------------------
# Deprecated newdir() and newfile() variants
function newdir(ctx::AbstractFileSystemRoot)
Expand Down

0 comments on commit 144353b

Please sign in to comment.