• Stars
    star
    413
  • Rank 104,497 (Top 3 %)
  • Language
    Haskell
  • License
    BSD 3-Clause "New...
  • Created over 12 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Haskell shell scripting

Shelly

Build Status Build Status Hackage Stackage Nightly Stackage LTS

Shelly provides a single module for convenient systems programming in Haskell.

  • Shelly is aimed at convenience and getting things done rather than being a demonstration of elegance.
  • It has detailed and useful error messages.
  • It maintains its own environment, making it thread-safe.
  • It has low memory usage: It has
    • run_ and other underscore variants that do not return stdout,
    • runFoldLines to run a fold operation over each line rather than loading all of stdout into memory,
    • runHandle and runHandles for complete control over handles.

The focus of this library on convenience combined with good error messages should make shelly approachable for newer users of Haskell.

More shelly packages

The shelly-extra package has some additional functionality that requires additional dependencies, currently including a convenient concurrency/futures implementation.

Examples

Blog Posts

Testimonials

Help

Alternatives

Haskell shell scripting libraries

  • HSH: A good alternative if you want to mixup usage of String and ByteString rather than just use Text.
  • HsShellScript: Has extensive low-level shell capabilities.
  • shell-conduit: Efficient streaming via conduits. Makes some portability sacrifices by
    • encouraging one to just use the shell instead of cross-platform Haskell code, and
    • encouraging one to use a convenience function that searches the PATH at compile-time.
  • shell-monad: Compile Haskell code down to shell script. This is a different approach from all the rest of the libraries. Writing your script is not as user-friendly as the other Haskell libraries, but it nicely solves the deployment issue.
  • shh: Shell-like syntax with native piping. Can be used from GHCi as an interactive shell replacement.
  • turtle: In some sense a redesign of Shelly designed for beginner-friendliness.

HSH, HsShellScript and shh (unlike Shelly currently) implement very efficient mechanisms for piping/redirecting in the system. turtle, like Shelly offers folding as a way to efficiently deal with a stream.

None of the alternatives to Shelly offer command tracing. For some this is an absolutely critical feature, particularly given that Haskell does not yet offer up stack traces.

Haskell file-finding supplements

Shelly's finders load all files into memory. This is simpler to use if you control the filesystem structure and know the system is bounded in size. However, if the filesystem structure is unbounded it consumes unbounded memory.

Shell commands with richer input/output

Shelly does not change the nature of shell scripting (text in, text out). If you want something more revolutionary you might try these:

Usage

Shelly's main goal is ease of use. There should be a primitive for every shell operation you need so you can easily build abstractions, so there are many of the usual file and environment operations.

There are 2 main entry points for running arbitrary commands: run and cmd. They take a FilePath as their first argument. run takes a [Text] as its second argument. cmd takes a variadic number of arguments, and they can be either Text or FilePath.

Fun Example: shows an infectious script: it uploads itself to a server and runs itself over ssh. Of course, the development machine may need to be exactly the same OS as the server.

I recommend using the boilerplate at the top of this example in your projects. This includes setting line buffering if you are dealing with text and not binary data.

    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE ExtendedDefaultRules #-}
    {-# OPTIONS_GHC -fno-warn-type-defaults #-}

    import Shelly
    import System.IO
    import Data.Text as T
    default (T.Text)

    main :: IO ()
    main =  do
      hSetBuffering stdout LineBuffering
      shelly $ verbosely $ do
        host <- run "uname" ["-n"]
        if T.stripEnd host == "local-machine"
          then do d <- cmd "date"
                  c <- escaping False $ cmd "git" "log -1 | head -1 | awk '{print $2}'"
                  appendfile "log/deploy.log" $ T.intercalate " - " [T.stripEnd d, c]
                  uploads "my-server:/remote/path/" ["deploy"]
                  sshPairs_ "my-server" [("cd", ["/remote/path"]), ("./deploy", [])]
          else do
                cmd "./script/angel"

    -- same path on remote host
    -- will create directories
    uploads :: Text -> [Text] -> Sh ()
    uploads remote locals = rsync $ ["--relative"] ++ locals ++ [remote]

    rsync :: [Text] -> Sh ()
    rsync args = run_ "rsync" $ ["--delete", "-avz", "--no-g"] ++ args

Variadic arguments to cmd

Yes, as seen above you can write variadic functions in Haskell quite easily, you just can't compose them as easily. I find cmd to be more convenient, but I often use run and command variants when I am building up abstractions. Building up abstractions with cmd will require type signatures.

    -- easy signature, but only allows one argument
    let cabal = cmd "cabal" :: Text -> Sh Text

    -- more complex signature that allows partial application of cmd
    let cabal = cmd "cabal" :: Shelly.ShellCmd result => result

Escaping

By default, all commands are shell escaped. If you want the shell to interpret special characters such as *, just use escaping False $ do ....

Using Text and FilePath together

Shelly's usage of Text means you may need to convert between Text and FilePath sometimes. This should be infrequent though because:

  • cmd will convert FilePath to Text.
  • The </> and <.> combinators convert Text into a FilePath automatically.

Manual conversion is done through toTextIgnore or toTextWarn.

Thread-safe working directory and relative paths

Command cd does not change the process working directory (essentially a global variable), but instead changes the shelly state (which is thread safe). All of the Shelly API takes this into account, internally shelly converts all paths to absolute paths. You can turn a relative path into an absolute with absPath or canonic or you can make a path relative to the Shelly working directory with relPath.

Good error messages

Haskell's #1 weakness for IO code is a lack of stack traces. Shelly gives you something different: detailed logging. In most cases this should be more useful than a stack trace. Shelly keeps a log of API usage and saves it to a .shelly directory on failure. If you use shellyNoDir, the log will instead be printed to stderr. This is in addition to the verbosely settings that will print out commands and their output as the program is running. Shelly's own error messages are detailed and in some cases it will catch Haskell exceptions and re-throw them with better messages.

If you make your own primitive functions that do not use the existing Shelly API, you can create a wrapper in the Sh monad that use trace or tag to log what they are doing. You can turn tracing off (not generally recommended) by setting tracing False.

More Repositories

1

hamlet.js

Javascript template language with DRY HTML
CoffeeScript
36
star
2

redux-side-effect

simple light-weight side effects for redux
JavaScript
27
star
3

ghc-docker-dev

A docker container for hacking on the Haskell GHC compiler
Dockerfile
21
star
4

haskell-heroku

Haskell helpers for deploying to Heroku
Haskell
18
star
5

FileLocation.hs

error and debug function for haskell that give file locations
Haskell
17
star
6

rspec-multi-matchers

rspec custom matchers - each, enum, and match_in_order
Ruby
16
star
7

jquery-uitableedit

small plugin to make an html talbe clickable/editable
JavaScript
15
star
8

go-recovery

Golang recover from panics
Go
14
star
9

methodchain

ruby helpers for method chaining: tap, then, else
Ruby
13
star
10

haskell-sphinx-client

implementation of a sphinx client in haskell
Haskell
13
star
11

hamlet.rb

The Hamlet template language reduces HTML syntax to its essential parts.
Ruby
11
star
12

haskell-xss-sanitize

prevent XSS attacks by sanitizing html (this is different then esacping!)
Haskell
9
star
13

ParseHelp.hs

A friendlier interface to the CmdArgs haskell library
Haskell
9
star
14

aeson-applicative

define To/From JSON instances from one applicative definition
Haskell
6
star
15

haskell-spell-suggest

spelling suggestion library and command line tool
Haskell
6
star
16

ruby-extract

a rule-based script to extract (decompress) any file
Ruby
6
star
17

jquery-tablelib

jquery library for working with html tables and serializing objects to tables
JavaScript
6
star
18

rails-schema-validations

automatically run validations based on your schema
Ruby
4
star
19

module-import

selectively import methods from modules
Ruby
4
star
20

quicktest

Utility for inlining unit tests with the code under test.
Ruby
4
star
21

jquery.ui.datetimepicker

jQuery dateTimePIcker plugin with bugfix
JavaScript
3
star
22

ruby-hash-extensions

A ruby hash in which every value returned is an Array.
Ruby
3
star
23

blog.gregweber.info

Greg Weber's Blog
Haskell
3
star
24

dlist-instances

orphan instances for dlist
Haskell
2
star
25

gregwebs-project-helpers

common rake task helpers for projects
2
star
26

wai-no-framework

wai demo
Haskell
2
star
27

ruby-todo

a command line todo list program similiar to todo.txt that uses a kirbybase text file
2
star
28

ar-mysql-bit-boolean

Add support for MySQL's bit(1) as a boolean
Ruby
2
star
29

docker-haskell-dev

Dockerfiles for haskell development
Shell
1
star
30

go-parallel

parallelism in Go using generics
Go
1
star
31

tinyscreen-interrupt

TinyScreenPlus interrupt example
C++
1
star
32

wai-static-pages

Helpers for generating a static site from a WAI application
Haskell
1
star
33

ruby-safe-mongoDB

make using the raw mongo driver safer by using constants for columns and MongoDB operators
Ruby
1
star
34

rspec-fuzzy-matchers

fuzzy equality matchers (allow spaces, etc)
Ruby
1
star