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

style(bash-v2): various cleanups #1702

Merged
merged 6 commits into from Oct 17, 2022
Merged

Conversation

scop
Copy link
Contributor

@scop scop commented May 17, 2022

Here's a bunch of bash code cleanups I made on the side while working on the performance improvements. See individual commits and messages for details. Some of these are also performance improvements in principle in addition to stylistic ones, but I believe they're not on any hot path so that side is negligible.

Some of these might be a bit opinionated/subjective but read the way I'd personally put them, let me know if you'd rather not make some of these changes and I'll follow suit.

@github-actions github-actions bot added the size/M Denotes a PR that chanes 24-99 lines label May 17, 2022
local subdir
subdir=$(printf "%%s" "${out}")
if [ -n "$subdir" ]; then
subdir=${out%%$'\n'}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here, in the final bash script, do we want a single or a double %%? If we want a double one to remove all trailing new lines, we need to put four of them in go (%%%%)

Copy link
Contributor Author

@scop scop May 21, 2022

Choose a reason for hiding this comment

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

It doesn't make a difference; there are no globs involved, so we are removing just one character (the linefeed) no matter if we do % or %%. Given this, the former seems a bit cleaner to me here, which is why I chose that option.

There is indeed a small difference in behavior here: the former implementation removed all trailing linefeeds, this removes just one. But I assume we don't have cases where there would be more than one, I chose the simplest available approach enabled by that assumption.

One way to remove multiple with a simple parameter expansion would require use of extended globbing, which we currently do not rely on in the script. bash-completion is a requirement and it does enable extglob unconditionally, and I don't see that changing, so in that sense it would be safe to use it. In that case the expansion could be written as ${out%%%%+($'\n')} (in Go code).

Another a bit hacky way which doesn't require extended globbing would be to do ${out//$'\n'} which is semantically different, but would for practical purposes work just fine here I think. Oops, no it wouldn't ;) Or hmm, I'm not actually sure, maybe it would, depends what $out can have here. Too lazy to dig in deep to figure out right now :)

Let me know if you prefer one of these approaches and I'll adjust.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It doesn't make a difference; there are no globs involved, so we are removing just one character (the linefeed) no matter if we do % or %%.

Ah yes. I missed that. Thanks.

I agree it should be safe to strip a single linefeed.

Copy link
Collaborator

@marckhouzam marckhouzam left a comment

Choose a reason for hiding this comment

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

Thanks for sharing your bash expertise!
This all makes sense and all the tests pass.

I'll just wait for the one question about the double %%

Copy link
Collaborator

@marckhouzam marckhouzam left a comment

Choose a reason for hiding this comment

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

Thanks once more @scop!

I'll just run a bit of extra manual testing later today, especially for the lesser used directive code paths and will merge after.

@marckhouzam
Copy link
Collaborator

Just an update that I haven't forgot about this, I'm just having trouble with my setup (waiting on a new computer). I will get back to this as soon as I can.

@scop
Copy link
Contributor Author

scop commented Jun 2, 2022

Sure, no rush at all as far as I'm concerned, take all the time you like 👍

@scop scop mentioned this pull request Jun 17, 2022
@marckhouzam
Copy link
Collaborator

@scop Finally getting back to this but the PR now has conflicts 😞 Could you rebase when you have a moment?

@scop
Copy link
Contributor Author

scop commented Jun 18, 2022

Reworked and still untested, hopefully I didn't add too many bugs.

@marckhouzam
Copy link
Collaborator

Reworked and still untested, hopefully I didn't add too many bugs.

Thanks @scop. After looking at this one again, I suggest changing our strategy.
Since this is a cleanup and not an actual fix, and that there will be a Cobra release soon, let's play it safe and wait on this one until after the release so we can get some more soak time.

@marckhouzam marckhouzam added area/shell-completion All shell completions kind/cleanup General cleanup of code, issues, etc. labels Jun 20, 2022
The result of the expansion is null no matter if the variable is unset
or null in both cases; the former form is arguably easier on the eye.
Herestrings read cleaner than process substitutions, and work in posix
mode (but we do and will have some process substitutions so this doesn't
matter much). Both approaches may end up using temporary files.
@scop
Copy link
Contributor Author

scop commented Aug 19, 2022

Rebased.

@marckhouzam
Copy link
Collaborator

I'm going to merge this improvement now to give it a lot of soak time until the next release.

@marckhouzam marckhouzam merged commit badcce1 into spf13:main Oct 17, 2022
@marckhouzam marckhouzam added this to the 1.7.0 milestone Oct 17, 2022
@scop scop deleted the style/bash-v2-cleanups branch October 17, 2022 19:39
hoshsadiq pushed a commit to zulucmd/zulu that referenced this pull request May 16, 2023
* use arithmetic evaluation in numeric context

* remove unnecessary $ from array index variables

* [[ ]] over [ ], == over =, remove unnecessary quoting

* use ${foo-} rather than ${foo:-} in emptiness check

The result of the expansion is null no matter if the variable is unset
or null in both cases; the former form is arguably easier on the eye.

* remove unnecessary trailing linefeed removal

Merge spf13/cobra#1702
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/cleanup General cleanup of code, issues, etc. size/M Denotes a PR that chanes 24-99 lines
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants