Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/bash/sdkman-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if [ -z "$SDKMAN_CANDIDATES_API" ]; then
export SDKMAN_CANDIDATES_API="@SDKMAN_CANDIDATES_API@"
fi

if [ -z "$SDKMAN_DIR" ]; then
if [ -z "$SDKMAN_DIR" ] || [ $(ls -ld "${SDKMAN_DIR}" | awk '{ print $3 }') != $(whoami) ]; then
Copy link
Member

Choose a reason for hiding this comment

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

Although I do see the point, the implementation does feel a bit hacky to me.
Can we assign this expression to a clearly named variable? Can we unset said variable at the end of the script? Can we collapse the two conditional blocks into one?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, and of course, please refrain from using awk as this forces a hard dependency that will often not be met on very basic systems.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, and of course, please refrain from using awk as this forces a hard dependency that will often not be met on very basic systems.

Which systems are that? The smallest I know comes with (an) awk:

$ docker run --rm alpine awk -version
BusyBox v1.30.1 (2019-06-12 17:51:55 UTC) multi-call binary.

Of course, BusyBox binaries are quite often not compatible with GNU ones... :/

That said, which dependencies are you comfortable with? For instance, I used to use the innocuous stat but it turns out that macOS and Ubuntu come with incompatible versions (FreeBSD-ish vs GNU-ish). ls + awk was the best I could come up with -- I'm open for suggestions!

Copy link
Member

Choose a reason for hiding this comment

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

The cut command is preferable as it is packaged as part of gnu coreutils. You could try something like this:

ls -ld "${SDKMAN_DIR}" | cut -d" " -f3

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although I do see the point, the implementation does feel a bit hacky to me.

Do you have a proposal? I felt it's a pretty straight-forward implementation for "do we point at this user's installation?". Of course, it will fail with system installations, but IIRC that's not a use-case you support?

Can we assign this expression to a clearly named variable? Can we unset said variable at the end of the script?

Sure!

Can we collapse the two conditional blocks into one?

What do you mean? [ -z ... ] || [ ... ] into [ ... ]? No, I don't think so; [ 1 -eq 1 || 1 -eq 2 ] doesn't parse. [[ 1 -eq 1 || 1 -eq 2 ]] would work, but you use single brackets everywhere else?

Copy link
Member

Choose a reason for hiding this comment

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

Actually, yes I do think so. We use double square brackets throughout the sdkman codebase when dealing with complex expressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The cut command is preferable as it is packaged as part of gnu coreutils. You could try something like this:

ls -ld "${SDKMAN_DIR}" | cut -d" " -f3

This does fail on the current Alpine (BusyBox v1.30.1). :/

Then, maybe stat wrapped away in a function and distinguish flavors there? That's in coreutils, after all.

export SDKMAN_DIR="$HOME/.sdkman"
fi

Expand Down