gregwebs / Shelly.hs

Haskell shell scripting
BSD 3-Clause "New" or "Revised" License
418 stars 88 forks source link

A module to expose all commands in PATH #77

Closed chrisdone closed 9 years ago

chrisdone commented 10 years ago

I've been experimenting with this approach, using template-haskell.

It allows you to just write git ["status"] or date [] or rm ["file"]. For me this changes scripts from "this is a bit tedious" to "this feels more bash-convenient".

Would you be interested in adding such a thing to shelly? It takes some seconds to compile the module (maybe 10) that uses generateBinaries, and makes a rather large haddock output, so you might want to make it a flag or something.

gregwebs commented 10 years ago

Interesting! Shelley already has code for searching the PATH. Why does it need to be flagged? Shouldn't it just take time to compile when the user uses it?

chrisdone commented 10 years ago

Interesting! Shelley already has code for searching the PATH

Sweet. I just cobbled mine together, I expect you'll have something more honed and battle-tested. Can you point me to that code?

Why does it need to be flagged? Shouldn't it just take time to compile when the user uses it?

Oh, was just a thought. It's indeed just compile-time, just adds a bit of time in compilation. If you're cool with that by default, so am I.

chrisdone commented 10 years ago

Hold that. I'm cloning, I'll have a look around and make a PR for this.

gregwebs commented 10 years ago

https://github.com/yesodweb/Shelly.hs/blob/master/src/Shelly.hs#L573

gregwebs commented 10 years ago

But I don't get the by default bit: doesn't a user choose whether to use this?

chrisdone commented 10 years ago

But I don't get the by default bit: doesn't a user choose whether to use this?

I just meant that it should build this large module. Nevermind, I was making a big deal out of the build time, it's not that bad.

Anyway, I suppose someone would write:

import Shelly.Bin

And that module brings a bunch of git = cmd "git", etc. top-level bindings. What do you think?

chrisdone commented 10 years ago

https://github.com/yesodweb/Shelly.hs/blob/master/src/Shelly.hs#L573

Right, so can I move the code at here to the top-level as an IO function that can be used from within TH?

chrisdone commented 10 years ago

I.e. to get a function like this.

gregwebs commented 10 years ago

sounds good! If it just works on unix for now that is ok (there is the .exe bit at least)

chrisdone commented 10 years ago

Cool. Alright, I'll make a PR later this evening!

gregwebs commented 10 years ago

Looking at this more I feel that there are some downsides to the approach as is

1) additional build time and binary size 2) scope pollution, which requires a remap for things I don't even care about and makes the script non-portable 3) the PATH can change from build time to run time

It seems like the issues can be solved by stating which binaries you desire: a white-list api:

generateCmds ["git", "less"]

For point 3, the script no longer has to check for the existence of a binary, although it could warn about the binary not existing.

I can see how you wouldn't want to take this approach in a shell like Hell though.