Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Command.value to return a promise #732

Open
Radiergummi opened this issue Aug 28, 2024 · 0 comments
Open

Allow Command.value to return a promise #732

Radiergummi opened this issue Aug 28, 2024 · 0 comments

Comments

@Radiergummi
Copy link

Cliffy allows to map option values, but doesn't wait for any promises returned from the value function. One could make the argument that this is by design and the action code should deal with that, but it bars one from use cases like allowing users to pass a value or a file path, and read the content from those before calling the action. Sample code for posterity:

.option("-j, --jwk", "JWK to use for verifying JWTs", {
    async value(value) {
        if (value === "-") {
            // If it's stupid, but it works, it ain't stupid.
            return await new Response(Deno.stdin.readable).text();
        }

        // Apply some heuristics to check whether this might be a file
        // path, and if so, try to read the file. If this fails for
        // whatever reason, treat the value as a literal JWK and
        // continue; the error will be caught during validation later.
        if (
            value && !value.startsWith("{") && (
                value.startsWith("/") ||
                value.startsWith(".") ||
                /^[a-zA-Z]:[\\/]/.test(value)
            )
        ) {
            try {
                await Deno.stat(value);

                return await Deno.readTextFile(value,{signal});
            } catch {
                return value;
            }
        }

        return value;
    },
})

This allows users to call the command with either of these:

# Pipe to stdin
cat jwk.json | cmd --jwk -

# Pass a file path
cmd --jwk ./jwk.json

# Pass the literal value on the command line
cmd --jwk "$(cat ./jwk.json)"

However, now my action needs to be aware of the fact that jwk may be a promise or a string. Yes, this is easily handled, but I'd prefer Cliffy to await any promises returned by value(), to stay consistent with non-mapped values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant