Stream: beginners

Topic: detect directory in basic-cli


view this post on Zulip Artur Swiderski (Dec 27 2023 at 15:10):

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

view this post on Zulip Anton (Dec 27 2023 at 15:33):

Yeah, we should create a function for this, I'll create a topic in the ideas stream for discussion.

view this post on Zulip Anton (Dec 27 2023 at 15:36):

For now you can use the unix command test -d dirOrFilePath through Cmd

view this post on Zulip Artur Swiderski (Dec 27 2023 at 15:53):

ok thx for tips

view this post on Zulip Anton (Dec 29 2023 at 08:22):

I made issue 145 for the new functions to be added.

view this post on Zulip Artur Swiderski (Dec 29 2023 at 20:35):

it does not work for me it is suggested to do this

test -f file; echo $?

but $? can't be understood

view this post on Zulip Artur Swiderski (Dec 29 2023 at 20:36):

in general test gives no visible output like ls and others

view this post on Zulip Karakatiza (Dec 29 2023 at 21:00):

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

view this post on Zulip Artur Swiderski (Dec 29 2023 at 21:01):

yes and I think that's the problem because I do not get typical output to console

view this post on Zulip Karakatiza (Dec 29 2023 at 21:05):

I just realized Cmd module does not return program result value :sweat_smile:

view this post on Zulip Karakatiza (Dec 29 2023 at 21:06):

@Anton regarding 145 - would it make sense to have isADir : Path -> [IsDir, Other] and so on, to have a version consistent with type?

view this post on Zulip Anton (Dec 30 2023 at 09:16):

@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.

view this post on Zulip Anton (Dec 30 2023 at 09:18):

Anton regarding 145 - would it make sense to have isADir : Path -> [IsDir, Other] and so on, to have a version consistent with type?

Do you mean in addition to the proposed isDir or instead of it?

view this post on Zulip Karakatiza (Dec 30 2023 at 13:43):

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

view this post on Zulip Anton (Dec 30 2023 at 14:16):

Don't know how common a usecase this would be.

I think it's wise to wait until there is demand in this case.

view this post on Zulip Artur Swiderski (Dec 31 2023 at 20:39):

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