How to Get Started with go-script-bash

Blogged about in:

This guide will show you how to add go-bash-script to your project. Mike's documentation is already a great place to start. Hopefully this example is a useful addition.

Open all sections

Copy the go-template into your project root

How
$ curl https://raw.githubusercontent.com/mbland/go-script-bash/master/go-template >./go
$ chmod ugo+rx ./go
Why

The curl command lets us quickly copy a single file out of GitHub, and the chmod command sets the write and execute permissions so we can run the script.

Edit the template to set a location for our scripts

How

Edit the go file and change the line that declares GO_SCRIPTS_DIR:

declare GO_SCRIPTS_DIR=.scripts
Why

The scripts feel like configuration/metadata for my project, so I wanted to tuck them into a directory named with a leading dot.

Add /.scripts/go-script-bash to .gitignore

How

Edit your .gitignore file (assuming you're using git) to prevent the go-script-bash framework from being added to your repository.

/.scripts/go-script-bash
Why

Running the go command for the first time will download the go-script-bash framework from GitHub. You could check that in along with your code, or add it as a submodule as Mike explains in his documentation. I chose to let it download once every time I set up my project on a new computer. I feel that option keeps my repository the cleanest.

Define "go" as a function

How
$ ./go env | bash
Why

./go env will print out a command to define the go script as a function that you can use from anywhere in your project. Piping that output into bash runs the command immediately.

Create your first go command script

How

Use the go new command to create a new script file:

$ go new --command serve

Edit the new file (.scripts/serve) to do something useful:

#! /usr/bin/env bash
#
# Starts the built-in Jekyll development server

_serve() {
        local IP=$(ip -f inet addr show eth0 | sed -n -e 's/^    inet \(.*\)\/.*$/\1/p')
        bundle exec jekyll serve --host=$IP ${_GO_CMD_ARGV}
}

_serve "$@"
Why

My serve command was getting a little unwieldly after I had to start specifying the IP address to bind to (I'll address that more when I get to writing about my experience with developing on a Pixelbook). This command uses sed to parse an IP address from the output of ip and then passes that on to Jekyll. ${_GO_CMD_ARGV} lets me pass additional arguments, so I can do something like go serve --drafts to include draft posts when running my website locally.

That was fun. Let's do another one!

How

Use the go new command to create a new script file:

$ go new --command setup

Edit the new file (.scripts/setup) to do something useful:

#! /usr/bin/env bash
#
# Installs ruby, jekyll, bundler, and their dependencies.

_setup() {
        sudo apt-get install ruby ruby-all-dev zlib1g-dev
        sudo gem install jekyll bundler
        bundle install
}

_setup "$@"
Why

This command should come in handy the next time I have to set up a new workspace for this project. It will install ruby, jekyll, bundler, and all the other dependencies needed to run my website.

At this point I have some useful scripts that make it easy to do things like set up my project on a new computer, or start up the Jekyll server to preview my website. I'm sure I'll add more since the go-script-bash framework makes it easy to do.