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

Feature/early eval for parameterized options #316

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ppcamp
Copy link
Contributor

@ppcamp ppcamp commented Sep 21, 2024

Description of changes:

Add support to early eval an option item.

# same as used to be
cat <file=$(ls)>

# same as used to be
cat <file=|__|_$(ls)_|>

# same as used to be
cat <file=|_opt1_|_opt2_|>

# ADDED
cat <file=|_$(ls)_|>

In this case (last scenario), it'll execute ls using cmd/runner/Run function (which keeps record of folder and shell to use), before create the gui, therefore, allowing user to just select one of the output elements.

example

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@RamiAwar
Copy link
Collaborator

Thanks for the contribution! Will take a look at it soon and play around with it.

@ppcamp ppcamp force-pushed the feature/early_eval_for_parametized_options branch from efffba1 to 43c3c8d Compare November 28, 2024 14:49
@RamiAwar
Copy link
Collaborator

Interesting approach you went with - I like that you didn't break existing behavior to do this.

I'm still thinking about this, reviewed part of it so far, will try to finish it today.

At a high level, I think lazy-evaluated parameters is a pretty cool concept. I think it belongs in Pet, can't see why not. It does add some complexity and risks bugs but if it's an isolated feature then should be ok.

I don't LOVE the fact that we're using the multi-default-value for this - maybe it's better to create a special representation here, but we risk creating too many special formats making pet hard to use - but also it's not too bad given that there's no reason for anyone to use multi-default-value repr for a single value.

On the other hand, we are blocking people from ever using multiple lazy-evaluated default values (ex. having 2 commands as separate choices). But that seems super unlikely to me - hard time thinking of a realistic usecase for that.

Idk, let this thought simmer maybe we come up with some other idea worth considering.

Copy link
Collaborator

@RamiAwar RamiAwar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm generally. left some small comments.

One more thing: please document this well in the README so people know how to do this.

Optionally, if you're able to, consider also documenting multi-default value usage in the 'pet new' command description. I believe that's still lacking.

cmd/util_windows.go Outdated Show resolved Hide resolved
// Example:
//
// DynamicOptions("$(fd -tf --hidden --no-ignore --max-depth=1 .)")
func DynamicOptions(what string) ([]string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function feels a bit strange - the fact that it accepts a string starting and ending with $() feels weird to me usage wise.

It might read a little nicer if we break it down one more step. Here we can relax the strictness of the check inside EvaluateDynamicOptions (works for any string now, if no dynamic options found inside it it'll just error out). We will have to regex match inside the evaluate now though, but

input := "pgcli -h $(get_available_hosts) -p 5432"
if hasDynamicOptions(input) {
    dynamicOptions, err := EvaluateDynamicOptions(input)
    // do stuff
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RamiAwar just updated the PR with your suggestions Pls review it again.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better, but still is a bit off. I'd like hasDynamicOptions to also support dynamic options that are in the middle of the string, not just strings that start with "$(" and end with ")".

This makes it easier to use, harder to misuse.

Copy link
Contributor Author

@ppcamp ppcamp Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it. Will work on that.


Just added the support to use in the middle of the string (allowing to have a prefix/suffix)

However, as described in the godoc, I'm still evaluating just the outermost command,
so if the user plan to have several commands nested like $(command $(with $(nested $(output) ) ), it'll took the whole string and send it to /bin/sh, so the shell will be responsible to evaluate with and nested, and therefore, if you don't have support to it, like cmd, than you cannot do nested evaluations.

Is it ok with this approach?

@ppcamp ppcamp force-pushed the feature/early_eval_for_parametized_options branch from 43c3c8d to b92c25e Compare December 4, 2024 21:57
Allow to pass commands in the middle of the string
and therefore, allow you to have a "prefix/suffix"
for each option.
@ppcamp ppcamp force-pushed the feature/early_eval_for_parametized_options branch from 7d3c484 to 8661af2 Compare December 12, 2024 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

2 participants