I want to detect that something is directory in basic cli I figured out that I will list it, but this does not work for empty directories because usual content ".." "." is not there
Yeah, we should create a function for this, I'll create a topic in the ideas stream for discussion.
For now you can use the unix command test -d dirOrFilePath
through Cmd
ok thx for tips
I made issue 145 for the new functions to be added.
it does not work for me it is suggested to do this
test -f file; echo $?
but $? can't be understood
in general test gives no visible output like ls and others
If I understand you correctly - test
returns a truthy or falsy value which can be chained with &&, for example; and $?
resolves to the return value of the latest executed command, in this case - test
yes and I think that's the problem because I do not get typical output to console
I just realized Cmd module does not return program result value :sweat_smile:
@Anton regarding 145 - would it make sense to have isADir : Path -> [IsDir, Other]
and so on, to have a version consistent with type
?
@Artur Swiderski for test -f
you need to use the Cmd.status
function. test -f fileOrDir
returns with exit code 0 if fileOrDir
is a file and with exit code 1 if it is not a file. The status function will return a success task if the exit code was 0 and an error task if not. You can use Task.attempt
to pattern match on the Result
.
Anton regarding 145 - would it make sense to have
isADir : Path -> [IsDir, Other]
and so on, to have a version consistent withtype
?
Do you mean in addition to the proposed isDir
or instead of it?
In addition - as having just Bool functions is useful, too
This way, the same function will be able to be used in different contexts, something like:
logDirectory = \ elem -> when elem is
IsDir -> Log.writeLine "encountered \(dirname)"
_ -> Task.ok
...
type pathA |> logDirectory
isDir pathB |> logDirectory
Don't know how common a usecase this would be.
But the Bool one is more important anyway.
Also, then it would make sense to swap the names:
isDir: Path -> [IsDir, Other]
isADir: Path -> Bool
Don't know how common a usecase this would be.
I think it's wise to wait until there is demand in this case.
ok below works for me
isDirectoryPath : Str -> Task Bool *
isDirectoryPath = \ str ->
command =
Cmd.new "test"
|> Cmd.args ["-d", str]
result <- Cmd.status command |> Task.attempt
when result is
Ok out ->
Task.ok Bool.true
Err _ ->
Task.ok Bool.false
Last updated: Jul 06 2025 at 12:14 UTC