dimo414 / bash-cache

Transparent caching layer for bash functions; particularly useful for functions invoked as part of your prompt.
GNU General Public License v3.0
73 stars 4 forks source link

Additional feedback from ZB #5

Closed dimo414 closed 6 years ago

dimo414 commented 6 years ago

Original report by Michael Diamond (Bitbucket: dimo414).


dimo414 commented 6 years ago

Original comment by Zac Bentley (Bitbucket: zbbentley, GitHub: zbentley).


Some additional ideas:

dimo414 commented 6 years ago

Original comment by Michael Diamond (Bitbucket: dimo414).


dimo414 commented 6 years ago

Original comment by Zac Bentley (Bitbucket: zbbentley, GitHub: zbentley).


Regarding cleanup-on-exit: that's fine. Maybe a sentence in the docs indicating that some stuff may be left and/or to call a random cached function before you turn out the lights to clean it all up?

I agree re: set -e. Some people care about it, though, so if you promise compatibility, why not document that?

Regarding the replacement of "newerthan" with a potential call to "find" on specific files rather than directories: that was a suggestion motivated by clarity/code reduction, not performance. It's probably very slightly slower, since "find" is likely a slightly slower-to-start program than the stat utilities. Splitting hairs tho.

The dunder locals thing is pretty pointless, though. In this case you control all the names it uses (out, err, exit), so there's no need to "defeat" possible colliding callers. Since that function is namespaced to bash cache, it's not likely to be used by other random code, yes?

Even in the general case, the "cross your fingers and guess nobody will pass something unlikely based on a python-convention-type-thing" is not a super good approach in Bash (because looser conventions and more prevalent use of global scope). If it were a big function, some silly metaprogramming/substitution/parsing "set" output could be used to avoid collisions, but since we're talking about 3-4 lines of code here, you can just do:

#!shell
read_input() {
  if [[ "${1:?Must provide a variable to read into}" == "contents" || "$1" == "line" ]]; then
    local _contents _line
    while read -r _line; do
      _contents="${_contents}${_line}"$'\n'
    done
    printf -v "$1" '%s%s' "$_contents" "$_line"
  else
    local contents line
    while read -r line; do
      contents="${contents}${line}"$'\n'
    done
    printf -v "$1" '%s%s' "$contents" "$line"
  fi
}
dimo414 commented 6 years ago

Original comment by Michael Diamond (Bitbucket: dimo414).


Fair enough regarding __, since you're right it's not intended to be used by anyone outside the library. That said, the reason I did it in the first place was it bit me :) I think it's safe to just fail if the varname is a local variable.

dimo414 commented 6 years ago

Original comment by Michael Diamond (Bitbucket: dimo414).


dimo414 commented 6 years ago

Original comment by Michael Diamond (Bitbucket: dimo414).


Filed issues #8, #9, and #10 to track the remaining items I'd like to address. Calling this issue done.