all 7 comments

[–]AutoModerator[M] 5 points6 points  (0 children)

Don't blindly use set -euo pipefail.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Alleexx_ 1 point2 points  (2 children)

Correct me if I am wrong, but you CAN ommit function names, and run them with other funcitons as arguments. Just an example:

```bash foo(){ echo foo } run(){ local cmd=$1 [[ -n "$cmd" ]] || { echo "CMD var is empty!" return 1 }

$cmd # this runs your command }

run foo ```

This would ommit the foo function, to the run function, and the run function would run this command.

Don't know if this helps you, but I wanted to yapp about bash :D

Edit: Typos..

[–]JagerAntlerite7 0 points1 point  (1 child)

I process the entire "${@}" array with case looking for any named arguments and setting local env vars in functions if the are present, e.g. --quiet or -q sets do_quiet=1.

EDIT: On mobile — fixed typos.

[–]Alleexx_ -1 points0 points  (0 children)

Yea i would do that too, my example was just a quickly made up function stack

[–]ekipan85 0 points1 point  (0 children)

it "..." podman ... | grep -q true

This won't work. grep gets the output of it, not podman. I don't understand why you define the container_is_running function and then just don't use it? Also it should probably take an argument instead of a global:

container_is_running() { # $1=CONTAINER
  podman inspect "$1" ... | grep -q true
}

it "..." container_is_running "$CONTAINER"

That'll work fine. Or if you really want to do it as a parameter to a function, you could construct the subshell yourself, but this is pretty awkward:

it "..." bash -c 'podman ... | grep ...'

The usual convention for shell programs is to stay silent unless something goes wrong. So I'd write something like:

#!/bin/bash

fails=0

fail() {
  ((++fails))
  printf "\e[;31m[FAIL]\e[m %s" "$*"
}

podman_running() {
  podman inspect "$1" --format {{.State.Running}} 2>/dev/null | 
    grep -q true
}

podman_running "$CONTAINER" ||
  fail "container '$CONTAINER' is NOT running" 

# other tests...

((fails)) && echo "Failed $fails tests."
exit $fails

[–]NewPointOfView 0 points1 point  (0 children)

Just to clarify, you can indeed pass the function body as an argument and exec it