Coder Social home page Coder Social logo

bash-monad's Introduction

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.

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:

#!/bin/bash

set -euo pipefail

if [ $# -ne 2 ]; then
  echo "Usage: $0 <DEPLOY_REGION> <PAYLOAD_NAME>"
  exit 1
fi

DEPLOY_REGION=$1
PAYLOAD_NAME=$2

echo 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 whose logging type is List[String]

bash-monad's People

Contributors

mcanlas avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.