From 5a39177405b9d689270fff01fcc548952b0d8100 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 31 Mar 2024 04:34:49 -0400 Subject: [PATCH 1/6] tests: migrate tests from circleci to github actions --- .circleci/config.yml | 13 ----- .github/dependabot.yml | 10 ++++ .github/linters/.markdown-lint.yml | 10 ++++ .github/linters/.yamllint.yml | 5 ++ .github/workflows/ci.yml | 30 +++++++++++ .github/workflows/lint.yml | 63 ++++++++++++++++++++++ Makefile | 7 --- README.md | 87 +++++++++++++++--------------- example/bash/example.bash | 8 +-- 9 files changed, 167 insertions(+), 66 deletions(-) delete mode 100644 .circleci/config.yml create mode 100644 .github/dependabot.yml create mode 100644 .github/linters/.markdown-lint.yml create mode 100644 .github/linters/.yamllint.yml create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/lint.yml diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 5a1811f..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,13 +0,0 @@ -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 diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..af4de4d --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,10 @@ +--- +version: 2 +updates: + - package-ecosystem: github-actions + directory: "/" + schedule: + interval: daily + open-pull-requests-limit: 10 + labels: + - "type: dependencies" diff --git a/.github/linters/.markdown-lint.yml b/.github/linters/.markdown-lint.yml new file mode 100644 index 0000000..2b7650b --- /dev/null +++ b/.github/linters/.markdown-lint.yml @@ -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 diff --git a/.github/linters/.yamllint.yml b/.github/linters/.yamllint.yml new file mode 100644 index 0000000..75da2b7 --- /dev/null +++ b/.github/linters/.yamllint.yml @@ -0,0 +1,5 @@ +--- +extends: default + +rules: + line-length: disable diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..d6e0bf3 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,30 @@ +--- +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: | + make build + - name: Test + shell: bash + run: | + go get -d -t + go test -v diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..115e654 --- /dev/null +++ b/.github/workflows/lint.yml @@ -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" diff --git a/Makefile b/Makefile index 97bd1d6..d8b1960 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/README.md b/README.md index b3ca4d4..b550dce 100644 --- a/README.md +++ b/README.md @@ -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 @@ -12,39 +12,40 @@ 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) } ``` @@ -52,7 +53,7 @@ Here is our `main.bash` file, the actual heart of the program: ```bash main() { - echo "Hello world" | reverse + echo "Hello world" | reverse } ``` @@ -60,15 +61,15 @@ main() { 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: @@ -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: . 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. @@ -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. diff --git a/example/bash/example.bash b/example/bash/example.bash index 2d7ba8f..ee4b6b5 100644 --- a/example/bash/example.bash +++ b/example/bash/example.bash @@ -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 } From d8952ed40a273dd354c8e2ec52b8dc00a2ccaabd Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 31 Mar 2024 04:39:06 -0400 Subject: [PATCH 2/6] tests: setup temp go module setup for basher I am not moving to go modules as I vaguely recall this actually broke something about the usage of this project in the past. --- .github/workflows/ci.yml | 2 ++ Makefile | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d6e0bf3..fd629d7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,6 +22,8 @@ jobs: - name: Build shell: bash run: | + go mod init + go mod tidy make build - name: Test shell: bash diff --git a/Makefile b/Makefile index d8b1960..eefce81 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ test: go test -v build: - go install || true + go install deps: go get -u github.com/a-urth/go-bindata/... From 11424554b08e5ea68f2c47c2f6f4106e91404281 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 31 Mar 2024 04:40:22 -0400 Subject: [PATCH 3/6] tests: add dummy circleci config This avoids an issue where I can't disable circleci because I don't have admin on the repo. --- .circleci/config.yml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..7c5e756 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,8 @@ +version: 2 +jobs: + build: + machine: true + working_directory: ~/progrium/go-basher + parallelism: 1 + steps: + - checkout From a914500d1f92356d5841b63e50e866af845c7b08 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 31 Mar 2024 04:41:29 -0400 Subject: [PATCH 4/6] tests: use dummy true step --- .circleci/config.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7c5e756..05f59fc 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -5,4 +5,5 @@ jobs: working_directory: ~/progrium/go-basher parallelism: 1 steps: - - checkout + - run: | + true \ No newline at end of file From 0b2466fed8297d5fa9e04f2870f38937b825ac2a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 31 Mar 2024 04:41:39 -0400 Subject: [PATCH 5/6] tests: name the module --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fd629d7..671b17f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,7 @@ jobs: - name: Build shell: bash run: | - go mod init + go mod init github.com/progrium/go-basher go mod tidy make build - name: Test From f6084b8c648ae5a45a633c4856fe9b76422ca63f Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 31 Mar 2024 04:42:48 -0400 Subject: [PATCH 6/6] tests: correct issues in yaml --- .circleci/config.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 05f59fc..9bb72b0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,3 +1,4 @@ +--- version: 2 jobs: build: @@ -6,4 +7,4 @@ jobs: parallelism: 1 steps: - run: | - true \ No newline at end of file + true