Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mcanlas/bash-monad
A DSL for Bash scripts, in Scala
https://github.com/mcanlas/bash-monad
bash dsl scala unix
Last synced: 7 days ago
JSON representation
A DSL for Bash scripts, in Scala
- Host: GitHub
- URL: https://github.com/mcanlas/bash-monad
- Owner: mcanlas
- License: mit
- Created: 2022-12-23T01:16:30.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-20T22:54:23.000Z (17 days ago)
- Last Synced: 2024-10-21T02:55:37.301Z (17 days ago)
- Topics: bash, dsl, scala, unix
- Language: Scala
- Homepage:
- Size: 80.1 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bash-monad
Exploring Bash script generation with a Scala DSL
## In action
Using a DSL, Bash programs can be encoded as a large, monadic data structure.
```scala
val program =
for {
_ <- BashProgram("#!/bin/bash")// seperate steps are padded with newlines
_ <- BashProgram("set -euo pipefail")// positional argument extraction
tuple <- Args
.string("DEPLOY_REGION")
.string("PAYLOAD_NAME")// downstream commands can statically depend on variables declared earlier
(deployRegion, payloadName) = tuple// bash interpolator allows for proper escaping and inline use of bash variables
// note that the type of `deployRegion` is not `String`
_ <- Cmd("echo", bash"""deploying to "$deployRegion" \o/ woohoo!!""")// this could be defined externally
awsLambda = MultiLineCommand("aws", "lambda")// pretty printing for subcommand style commands
_ <- Aws.Lambda("list-tags")
.opt("foo")
.opt("payload", payloadName)
.opt("region", deployRegion)
} yield ()val compiled =
Encoder.encode(program)println(compiled)
```When compiled, the above will yield the following Bash script:
```bash
#!/bin/bashset -euo pipefail
if [ $# -ne 2 ]; then
echo "Usage: $0 "
exit 1
fiDEPLOY_REGION=$1
PAYLOAD_NAME=$2echo deploying to \"${DEPLOY_REGION}\" \\o/ woohoo\!\!
aws lambda list-tags \
--foo \
--payload \
"$PAYLOAD_NAME" \
--region \
"$DEPLOY_REGION"
```## Elsewhere
A `BashProgram[_]` feels isomorphic to a [`Writer[_]` monad](https://typelevel.org/cats/datatypes/writer.html) whose logging type is `List[String]`