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

doc(README): Make shell snippet copy&paste safe #336

Merged
merged 1 commit into from
Jun 26, 2024

Conversation

bugwelle
Copy link
Contributor

Many bash/shell snippets in the README use an environment variable YOUR_CONTAINER_REGISTRY, which is used as $YOUR_CONTAINER_REGISTRY.

However, that has the downside that if users copy & paste the snippet and accidentally press enter or use a shell that doesn't allow multi- line pasting (and executes it immediately), then the variable will be replaced by an empty string, because it wasn't set before.

It's good practice to make those snippets copy&paste-safe. We can achieve that by adding :? to the variable, which emits an error if the variable is unset or null.

See https://devhints.io/bash for a bash cheat sheet.

Example:

docker push ${YOUR_CONTAINER_REGISTRY:?}/bookshop-srv

On ZSH:

zsh: YOUR_CONTAINER_REGISTRY: parameter not set

On Bash:

bash: YOUR_CONTAINER_REGISTRY: parameter null or not set

Many bash/shell snippets in the README use an environment variable
`YOUR_CONTAINER_REGISTRY`, which is used as `$YOUR_CONTAINER_REGISTRY`.

However, that has the downside that if users copy & paste the snippet
and accidentally press enter or use a shell that doesn't allow multi-
line pasting (and executes it immediately), then the variable will be
replaced by an empty string, because it wasn't set before.

It's good practice to make those snippets copy&paste-safe.
We can achieve that by adding `:?` to the variable, which emits an
error if the variable is unset or null.

See https://devhints.io/bash for a bash cheat sheet.

Example:

```sh
docker push ${YOUR_CONTAINER_REGISTRY:?}/bookshop-srv
```

On ZSH:

```
zsh: YOUR_CONTAINER_REGISTRY: parameter not set
```

On Bash:

```
bash: YOUR_CONTAINER_REGISTRY: parameter null or not set
```
@davidhunglam
Copy link
Contributor

davidhunglam commented Jun 26, 2024

Actually, most people replace the whole string in the copied command. But in cases where an actual env variable is used, this is quite nice.

@bugwelle
Copy link
Contributor Author

@davidhunglam The issue is that some shells auto-executed a pasted snippet if it ends with a trailing newline. That has happened a few times in the past for me, when I didn't setup my shell properly. 😄

This is just a safe-guard. I'd also expect users to replace the variable altogether and not set it in their shell.

@bugwelle
Copy link
Contributor Author

Thanks for reviewing my change, David! 😄
I don't have merge-permissions. Unless anybody else needs to review this, it would be great if you could merge this PR. Thanks!

@beckermarc beckermarc merged commit d73afa3 into SAP-samples:main Jun 26, 2024
2 checks passed
@bugwelle bugwelle deleted the andre/doc branch June 26, 2024 08:02
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.

3 participants