Skip to content

Commit

Permalink
Merge pull request #47 from progrium/github-actions
Browse files Browse the repository at this point in the history
tests: migrate tests from circleci to github actions
  • Loading branch information
josegonzalez authored Mar 31, 2024
2 parents 5f673cf + f6084b8 commit 7f6b374
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 59 deletions.
7 changes: 2 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
---
version: 2
jobs:
build:
machine: true
working_directory: ~/progrium/go-basher
parallelism: 1
steps:
- checkout
- run: |
make build
- run: |
go get -d -t
go test -v
true
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
version: 2
updates:
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
labels:
- "type: dependencies"
10 changes: 10 additions & 0 deletions .github/linters/.markdown-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
default: true

# Line length
# https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md#md013
MD013: false

# Inline HTML
# https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md#md033
MD033: false
5 changes: 5 additions & 0 deletions .github/linters/.yamllint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
extends: default

rules:
line-length: disable
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
name: CI

# yamllint disable-line rule:truthy
on:
pull_request:
branches:
- "*"
push:
branches:
- "master"

jobs:
build:
name: build
runs-on: ubuntu-22.04
strategy:
fail-fast: true

steps:
- uses: actions/checkout@v4
- name: Build
shell: bash
run: |
go mod init github.com/progrium/go-basher
go mod tidy
make build
- name: Test
shell: bash
run: |
go get -d -t
go test -v
63 changes: 63 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
name: "lint"

# yamllint disable-line rule:truthy
on:
pull_request:
branches:
- "*"
push:
branches:
- "master"

jobs:
markdown-lint:
name: markdown-lint
runs-on: ubuntu-22.04
steps:
- name: Clone
uses: actions/checkout@v4
- name: Run markdown-lint
uses: avto-dev/markdown-lint@04d43ee9191307b50935a753da3b775ab695eceb
# v1.5.0 => 04d43ee9191307b50935a753da3b775ab695eceb
with:
config: ".github/linters/.markdown-lint.yml"
args: "./README.md"

shellcheck:
name: shellcheck
runs-on: ubuntu-22.04
steps:
- name: Clone
uses: actions/checkout@v4
- name: Run shellcheck
uses: ludeeus/action-shellcheck@00cae500b08a931fb5698e11e79bfbd38e612a38
# 1.1.0 => 94e0aab03ca135d11a35e5bfc14e6746dc56e7e9
env:
SHELLCHECK_OPTS: -e SC2034

shfmt:
name: shfmt
runs-on: ubuntu-22.04
steps:
- name: Clone
uses: actions/checkout@v4
- name: Run shfmt
uses: luizm/action-sh-checker@c6edb3de93e904488b413636d96c6a56e3ad671a
# v0.3.0 => 7f44869033b40ee4ffe7dc76c87a1bc66e3d025a
env:
SHFMT_OPTS: -l -bn -ci -i 2 -d
with:
sh_checker_shellcheck_disable: true

yamllint:
name: yamllint
runs-on: ubuntu-22.04
steps:
- name: Clone
uses: actions/checkout@v4
- name: Run yamllint
uses: ibiqlik/action-yamllint@2576378a8e339169678f9939646ee3ee325e845c
# v3.0.4 => ed2b6e911569708ed121c14b87d513860a7e36a7
with:
config_file: ".github/linters/.yamllint.yml"
9 changes: 1 addition & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test:
go test -v

build:
go install || true
go install

deps:
go get -u github.com/a-urth/go-bindata/...
Expand Down Expand Up @@ -38,10 +38,3 @@ bash:
go-bindata -tags=linux,amd64 -o=bash_linux_amd64.go -prefix=$(BASH_DIR)/linux-amd64 -pkg=basher $(BASH_DIR)/linux-amd64
go-bindata -tags=darwin,arm64 -o=bash_darwin_arm64.go -prefix=$(BASH_DIR)/osx-arm64 -pkg=basher $(BASH_DIR)/osx-arm64
go-bindata -tags=darwin,amd64 -o=bash_darwin_amd64.go -prefix=$(BASH_DIR)/osx-amd64 -pkg=basher $(BASH_DIR)/osx-amd64

circleci:
rm ~/.gitconfig
rm -rf /home/ubuntu/.go_workspace/src/github.com/$(OWNER)/$(NAME) && cd .. \
&& mkdir -p /home/ubuntu/.go_workspace/src/github.com/$(OWNER) \
&& mv $(NAME) /home/ubuntu/.go_workspace/src/github.com/$(OWNER)/$(NAME) \
&& ln -s /home/ubuntu/.go_workspace/src/github.com/$(OWNER)/$(NAME) $(NAME)
87 changes: 45 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A Go library for creating Bash environments, exporting Go functions in them as Bash functions, and running commands in that Bash environment. Combined with a tool like [go-bindata](https://github.com/jteeuwen/go-bindata), you can write programs that are part written in Go and part written in Bash that can be distributed as standalone binaries.

[![Circle CI](https://circleci.com/gh/progrium/go-basher.svg?style=shield)](https://circleci.com/gh/progrium/go-basher) [![GoDoc](https://godoc.org/github.com/progrium/go-basher?status.svg)](http://godoc.org/github.com/progrium/go-basher)
![Github Actions](https://github.com/github/docs/actions/workflows/ci.yml/badge.svg) [![GoDoc](https://godoc.org/github.com/progrium/go-basher?status.svg)](http://godoc.org/github.com/progrium/go-basher)

## Using go-basher

Expand All @@ -12,63 +12,64 @@ Here we have a simple Go program that defines a `reverse` function, creates a Ba
package main

import (
"os"
"io/ioutil"
"log"
"strings"
"os"
"io/ioutil"
"log"
"strings"

"github.com/progrium/go-basher"
"github.com/progrium/go-basher"
)

func reverse(args []string) {
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
runes := []rune(strings.Trim(string(bytes), "\n"))
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
println(string(runes))
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}

runes := []rune(strings.Trim(string(bytes), "\n"))
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
println(string(runes))
}

func main() {
bash, _ := basher.NewContext("/bin/bash", false)
bash.ExportFunc("reverse", reverse)
if bash.HandleFuncs(os.Args) {
os.Exit(0)
}

bash.Source("main.bash", nil)
status, err := bash.Run("main", os.Args[1:])
if err != nil {
log.Fatal(err)
}
os.Exit(status)
bash, _ := basher.NewContext("/bin/bash", false)
bash.ExportFunc("reverse", reverse)
if bash.HandleFuncs(os.Args) {
os.Exit(0)
}

bash.Source("main.bash", nil)
status, err := bash.Run("main", os.Args[1:])
if err != nil {
log.Fatal(err)
}
os.Exit(status)
}
```

Here is our `main.bash` file, the actual heart of the program:

```bash
main() {
echo "Hello world" | reverse
echo "Hello world" | reverse
}
```

## Using go-basher with go-bindata

You can bundle your Bash scripts into your Go binary using [go-bindata](https://github.com/jteeuwen/go-bindata). First install go-bindata:

$ go get github.com/jteeuwen/go-bindata/...
go get github.com/jteeuwen/go-bindata/...

Now put all your Bash scripts in a directory called `bash`. The above example program would mean you'd have a `bash/main.bash` file. Run `go-bindata` on the directory:

$ go-bindata bash
go-bindata bash

This will produce a `bindata.go` file that includes all of your Bash scripts.

> `bindata.go` includes a function called `Asset` that behaves like `ioutil.ReadFile` for files in your `bindata.go`.
> `bindata.go` includes a function called `Asset` that behaves like `ioutil.ReadFile` for files in your `bindata.go`.
Here's how you embed it into the above example program:

Expand All @@ -77,22 +78,22 @@ Here's how you embed it into the above example program:
* method B: replace all code in the `main()`-function with the `Application()`-helper function (see below)

```Go
basher.Application(
map[string]func([]string){
"reverse": reverse,
}, []string{
"bash/main.bash",
},
Asset,
true,
)
basher.Application(
map[string]func([]string){
"reverse": reverse,
}, []string{
"bash/main.bash",
},
Asset,
true,
)
```

## Batteries included, but replaceable

Did you already hear that term? Sometimes Bash binary is missing, for example when using alpine linux or busybox. Or sometimes its not the correct version. Like OSX ships with Bash 3.x which misses a lot of usefull features. Or you want to make sure to avoid shellshock attack.

For those reasons static versions of Bash binaries are included for linux and darwin. Statically linked bash binaries are released at: https://github.com/robxu9/bash-static. These are then turned into go code, with go-bindata: bindata_linux.go and bindata_darwin.go.
For those reasons static versions of Bash binaries are included for linux and darwin. Statically linked bash binaries are released at: <https://github.com/robxu9/bash-static>. These are then turned into go code, with go-bindata: bindata_linux.go and bindata_darwin.go.

When you use the `basher.Application()` function, the built in Bash binary will be extracted into the `~/.basher/` dir.

Expand All @@ -106,7 +107,9 @@ Go is a great compiled systems language, but it can still be faster to write and

Take a common task like making an HTTP request for JSON data. Parsing JSON is easy in Go, but without depending on a tool like `jq` it is not even worth trying in Bash. And some formats like YAML don't even have a good `jq` equivalent. Whereas making an HTTP request in Go in the *simplest* case is going to be 6+ lines, as opposed to Bash where you can use `curl` in one line. If we write our JSON parser in Go and fetch the HTTP doc with `curl`, we can express printing a field from a remote JSON object in one line:

curl -s https://api.github.com/users/progrium | parse-user-field email
```shell
curl -s https://api.github.com/users/progrium | parse-user-field email
```

In this case, the command `parse-user-field` is an app specific function defined in your Go program.

Expand Down
8 changes: 4 additions & 4 deletions example/bash/example.bash
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
set -eo pipefail

hello-bash() {
echo "Hello world from Bash"
echo "Hello world from Bash"
}

main() {
echo "Arguments:" "$@"
hello-bash | reverse
curl -s https://api.github.com/repos/progrium/go-basher | json-pointer /owner/login
echo "Arguments:" "$@"
hello-bash | reverse
curl -s https://api.github.com/repos/progrium/go-basher | json-pointer /owner/login
}

0 comments on commit 7f6b374

Please sign in to comment.