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

Sanity Check #1

Closed
DamonOehlman opened this issue Mar 30, 2015 · 14 comments
Closed

Sanity Check #1

DamonOehlman opened this issue Mar 30, 2015 · 14 comments

Comments

@DamonOehlman
Copy link
Contributor

@dominictarr @substack @rvagg @hughsk - Just looking for a quick sanity check as to whether this is a (semi) sensible thing to do. There are lots of problems with the approach (no isolation of packages, versions will currently clobber each other etc).

I've thought about this before though when I played around with a pure BASH based way of attempting to provision a machine, but shied away from it. I've published this one package to npm, but I'm going to hold off publishing any more until the general concept has been peer reviewed.

  1. Is this a semi-sensible idea.
  2. Has it been done before, and if so, then I'll just use that instead of starting something new.

Be brutal, I'm quite ok if this goes nowhere, and if brutality makes it good then that's awesome :)

@dominictarr
Copy link

I have thought about this also, and experimented a bit. I think a serious language certainly should be able to do this. Though I am unsure whether you wouldn't be creating more pain for your self by treating bash as a serious language though.

Btw, readlink is not cross platform (I have been told it does not come installed on mac)

I definitely think this is worth experimenting with though!

you might be able to do something with subshells to support version conflicts?

@DamonOehlman
Copy link
Contributor Author

Thanks Dominic:

I think a serious language certainly should be able to do this. Though I am unsure whether you wouldn't be creating more pain for your self by treating bash as a serious language though.

I think I should quote you on this in the README (if you are ok with that), as it does help set some reasonable expectations of what can be achieved in the BASH environment.

Thanks for the heads up on readlink too. I'll get some of the mac folks at work to try it and let me know how thing go. Nice idea re subshells too - will investigate.

@dominictarr
Copy link

@DamonOehlman sure. ANYTHING can be achieved in bash, but can your sanity be preserved?
once this is working it would be cool to use it for otp.sh as discussed with @nathan7 (see: https://www.youtube.com/watch?v=rRbY3TMUcgQ)

@DamonOehlman
Copy link
Contributor Author

You're right, I just don't have that much sanity to be able to afford significant losses. Perhaps our collective sanity, however, can afford to take the hit. I propose that I create an org (see https://github.com/bashinate but a better name is probably required) and invite those willing to risk some sanity to help make BASH a tad more package and require friendly.

Long term success being the goal, no promises or expectations in the short term.

@DamonOehlman
Copy link
Contributor Author

@dominictarr @nathan7 you're both invited by the way, but no pressure to accept or do anything.

@stylemistake
Copy link

@DamonOehlman I've got an idea how to make modular bash with subshells.

Basically you want to write your module "module-aware":

## inside module 'foo'
foo_bar() {
    echo "hello, ${1}!"
}

module export foo_bar as bar

What module export does is create a big switch case of exported functions which is run when that module is executed.

Then, in your program:

import foo
foo bar user # -> hello, user!

You could also do it like this:

import bar from foo
bar user # -> hello, user!
import bar as b from foo
b user # -> hello, user!

The benefit from this is that it's stupidly simple to use and completely modular.

The drawbacks are:

  • Lots of overhead (subshells, deep function nesting, etc.);
  • Modules are not singletons.

P.S. We can also go further and add some special pseudo-syntax to access modules:

import foo
foo.bar username
# or...
foo::bar username
# or...
foo_bar username
# etc.

@dominictarr
Copy link

oh btw, I'm not sure what @stylemistake is intending as implementation, but I often do something similar in bash, to make commands

# foobar.sh
foo () {
  echo foo
}

bar () {
  echo bar
}

if [ "$0" = "$BASH_SOURCE" ]; then
  "$@"
fi

so running ./foobar.sh foo would echo foo etc.
the key part is "$@" which expands all the arguments of the script into separate arguments.
so if the first arg is a command name then it calls it. of course, this is basically evalling anything
command in the context of this script, security nightmare (welcome to bash)

@stylemistake
Copy link

@dominictarr safer alternative:

case ${1} in
    'bar') shift; foo_bar "${@}" ;;
esac

or

# didn't test this, but you get the idea
declare -A exports=(
    [bar]=foo_bar
)

name="${1}"
fn="${exports[$name]}"
shift

[[ -n "${fn}" ]] "${fn}" "${@}"

This is incompatible with bash-3.x tho, but it must die anyway (brew install bash for OSX), so version 4 ftw.

@dominictarr
Copy link

the point of coding in bash is that it is already everywhere, if you are requiring a specific version of bash, you might as well just use well... any of the programming languages you use when you have a choice about which programming language you can use.

@stylemistake
Copy link

Proof of concept: https://github.com/stylemistake/bash-module
I'll experiment more with it to make modules singletons (crazy idea, i know), and minimize overhead when calling module functions.

@dominictarr
Copy link

@stylemistake if modules arn't singletons then you don't have version conflicts. node.js has non singleton modules and it works great! (i wish another language was like that)

@stylemistake
Copy link

@dominictarr making it work like require in node.js would be perfect.

@DamonOehlman
Copy link
Contributor Author

👍 sounds awesome to me :)

@DamonOehlman
Copy link
Contributor Author

Closing - though still unsure about my sanity. Haven't done too much recently with this, but if others have achieved anything interesting in the land of BASH then I'm keen to know :)

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

No branches or pull requests

3 participants