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

Export options as environment variables #448

Merged
merged 9 commits into from
Aug 19, 2020
Merged

Export options as environment variables #448

merged 9 commits into from
Aug 19, 2020

Conversation

YerinAlexey
Copy link
Contributor

@YerinAlexey YerinAlexey commented Aug 14, 2020

All options will be available in commands via lf_OPTION environment variables

Closes #443, closes #376

Alexey Yerin added 3 commits August 14, 2020 17:09
Any options from gOpts are available via lf_OPTION environment
variables. For now it works only on booleans, integers and strings (no
array support)
@YerinAlexey YerinAlexey marked this pull request as ready for review August 14, 2020 15:02
@gokcehan
Copy link
Owner

@YerinAlexey Thanks for the patch. I never considered using reflect to simplify things here.

Is it possible to also add sortType manually somehow? I think some people might be upset when these options are missing (e.g. #411 specifically mentions hidden option). Though if you don't want to work on it, we can leave it for later.

I see you also you 1 and 0 to represent true and false in booleans. Booleans are kind of confusing in shell because return codes use the reverse semantic compared to C-family languages (i.e. return 0 for success, non-zero for fail). I was thinking, maybe values true and false might be a better approach for booleans. Since they are shell builtins, they could be used as follows:

if $lf_number; then
    echo 'numbers are enabled'
else
    echo 'numbers are disabled'
fi

What do you think?

@YerinAlexey
Copy link
Contributor Author

YerinAlexey commented Aug 16, 2020

Is it possible to also add sortType manually somehow?

Yes, I'll add it.

I see you also you 1 and 0 to represent true and false in booleans. Booleans are kind of confusing in shell because return codes use the reverse semantic compared to C-family languages (i.e. return 0 for success, non-zero for fail). I was thinking, maybe values true and false might be a better approach for booleans.

I also think true/false for booleans are better. I'll fix them soon.

@gokcehan
Copy link
Owner

@YerinAlexey Since true and false are the same in Go, there is strconv.FormatBool function you can use to get rid of conditionals if you like.

@YerinAlexey
Copy link
Contributor Author

@YerinAlexey Since true and false are the same in Go, there is strconv.FormatBool function you can use to get rid of conditionals if you like.

Thanks for advice, I didn't know about this function before

@gokcehan
Copy link
Owner

@YerinAlexey It seems good to me. Thank you again for implementing this.

@gokcehan gokcehan merged commit 25c2f03 into gokcehan:master Aug 19, 2020
@SeerLite
Copy link
Contributor

Thank you so much for implementing this! It works perfectly!

I see you also you 1 and 0 to represent true and false in booleans. Booleans are kind of confusing in shell because return codes use the reverse semantic compared to C-family languages (i.e. return 0 for success, non-zero for fail). I was thinking, maybe values true and false might be a better approach for booleans. Since they are shell builtins, they could be used as follows:

if $lf_number; then
    echo 'numbers are enabled'
else
    echo 'numbers are disabled'
fi

Hey, I don't actually like this idea at all because it encourages the execution of arbitrary variables. Having a stricter (although more verbose) representation of the variables sounds like a more secure approach. Besides, it makes it a bit more consistent with non-boolean variables: You'd have to test or [ <...> = <...> ] both kinds.

Note: Currently, doing [ <...> = "true" ] also works. I just wanted to give my opinion on this: naming booleans true and false with the intention of them being executed sounds like a bad idea.

@gokcehan
Copy link
Owner

@SeerLite You have a point, though I can't think of any scenario where the values can be set to arbitrary commands to be executed which is otherwise not possible. I can see how some people may try to accidentally execute non-boolean variables (e.g. if $lf_ratios; then ...), though this will likely end up with garbage errors at worst.

Anyway, [ <...> = "true" ] seems perfectly fine. It is often a good idea to be explicit about things. Though I should note that [ and test have their own quirks. For example, this might fail depending on the quoting rules of your shell when the variable is empty (e.g. when you're using an older version of lf where options were not available). Also you should be aware the option value does not contain anything that starts with - to be confused as flags to [ and test commands.

@SeerLite
Copy link
Contributor

I can't think of any scenario where the values can be set to arbitrary commands to be executed which is otherwise not possible.

Currently, me neither. But that doesn't mean that something like this can't come up in the future!
I understand if you think it's ok to leave it like this for now though.

For example, this might fail depending on the quoting rules of your shell when the variable is empty (e.g. when you're using an older version of lf where options were not available)

I'm assuming POSIX shell for this, and I think test is a lot safer. If the variable is empty, it's gonna be parsed as [ "" = 1] which IMO is a lot nicer than, when executing inside an if, it ends up like: if ; then.
I just tested this in bash and dash, if $empty-var; then always succeeds, which I think is quite counter-intuitive.

Also you should be aware the option value does not contain anything that starts with - to be confused as flags to [ and test commands.

I tested this and as long as there's a = in the command, - aren't parsed as flags. I'm not sure if this is officially defined in POSIX but it worked in dash, bash and busybox shell. Considering this, I think test is pretty safe to use.

Provessor added a commit to Provessor/lf that referenced this pull request Sep 1, 2020
Fix colour construction issue

This also has a test to mitigate it in the future

Remove `colormode` option

The original issue it was trying to solve is no longer present with
tcell (it being a holdover from `color256` on termbox) so it is not
needed.

retire gitter channel in favor of irc/matrix

Export options as environment variables (gokcehan#448)

* Export options as environment variables

Any options from gOpts are available via lf_OPTION environment
variables. For now it works only on booleans, integers and strings (no
array support)

* Do not export some of the options

* Add support for arrays and fix numbers

* Fix comments

* Replace 1 and 0 with true and false

* Export hidden,reverse,dirfirst and sortby options

* Fix comments

* Little fix

* Simplify boolean conversion

log readlink errors instead of fail

Related gokcehan#447 and gokcehan#374
gokcehan pushed a commit that referenced this pull request Sep 1, 2020
Fix colour construction issue

This also has a test to mitigate it in the future

Remove `colormode` option

The original issue it was trying to solve is no longer present with
tcell (it being a holdover from `color256` on termbox) so it is not
needed.

retire gitter channel in favor of irc/matrix

Export options as environment variables (#448)

* Export options as environment variables

Any options from gOpts are available via lf_OPTION environment
variables. For now it works only on booleans, integers and strings (no
array support)

* Do not export some of the options

* Add support for arrays and fix numbers

* Fix comments

* Replace 1 and 0 with true and false

* Export hidden,reverse,dirfirst and sortby options

* Fix comments

* Little fix

* Simplify boolean conversion

log readlink errors instead of fail

Related #447 and #374
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

Successfully merging this pull request may close these issues.

[Feature request] Export lf options to shell commands inspect remote options at runtime
3 participants