From e4ad61ce47bbefae8139c7938701d61f5f12278f Mon Sep 17 00:00:00 2001 From: Artur Troian Date: Tue, 19 Mar 2024 12:03:48 +0100 Subject: [PATCH] feat: initial commit Signed-off-by: Artur Troian --- .env | 13 ++ .envrc | 74 ++++++++++ .github/workflows/release.yaml | 48 +++++++ .gitignore | 2 + .goreleaser.yaml | 65 +++++++++ Dockerfile | 14 ++ Makefile | 55 +++++++ go.mod | 5 + go.sum | 2 + main.go | 161 +++++++++++++++++++++ script/semver.sh | 256 +++++++++++++++++++++++++++++++++ script/tools.sh | 68 +++++++++ 12 files changed, 763 insertions(+) create mode 100644 .env create mode 100644 .envrc create mode 100644 .github/workflows/release.yaml create mode 100644 .gitignore create mode 100644 .goreleaser.yaml create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100755 script/semver.sh create mode 100755 script/tools.sh diff --git a/.env b/.env new file mode 100644 index 0000000..18962a3 --- /dev/null +++ b/.env @@ -0,0 +1,13 @@ +#KINDEST_VERSION=v1.24.7 +#GO111MODULE=on +#REDIS_VERSION=7 +#ROOT_DIR=${AP_ROOT} +#AP_DEVCACHE_BASE=${AP_ROOT}/.cache +#AP_DEVCACHE=${AP_DEVCACHE_BASE} +#AP_DEVCACHE_BIN=${AP_DEVCACHE}/bin +#AP_DEVCACHE_INCLUDE=${AP_DEVCACHE}/include +#AP_DEVCACHE_VERSIONS=${AP_DEVCACHE}/versions +#AP_DEVCACHE_NODE_MODULES=${AP_DEVCACHE} +#AP_DEVCACHE_NODE_BIN=${AP_DEVCACHE_NODE_MODULES}/node_modules/.bin +#AP_DEVCACHE_TESTS=${AP_DEVCACHE}/tests +#DEVCACHE_RUN=${AP_DEVCACHE}/run diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..bff51f8 --- /dev/null +++ b/.envrc @@ -0,0 +1,74 @@ +direnv_version_major=$(direnv version | cut -d "." -f1 | tr -d '\n') +direnv_version_minor=$(direnv version | cut -d "." -f2 | tr -d '\n') + +if [[ $direnv_version_major -lt 2 ]] || [[ $direnv_version_major -eq 2 ]] && [[ $direnv_version_minor -lt 32 ]]; then + echo -e "\033[31munsupported direnv version $(direnv version) < 2.32.x" + exit 1 +fi + +if [[ "$SHELL" == "bash" ]]; then + if [ "${BASH_VERSINFO:-0}" -lt 4 ]; then + echo -e "\033[31mthe environment needs BASH 4 or above" >&2 + exit 1 + fi +fi + +AP_ROOT=$(pwd) +export AP_ROOT + +dotenv +dotenv_if_exists dev.env + +if ! has make ; then + echo -e "\033[31mmake is not installed"; exit 1 +fi + +if ! has go ; then + echo -e "\033[31mgo is not installed"; exit 1 +fi + +TOOLS=${AP_ROOT}/script/tools.sh +SEMVER=${AP_ROOT}/script/semver.sh + +GOTOOLCHAIN=$(${TOOLS} gotoolchain) +GOTOOLCHAIN_SEMVER=$(echo "${GOTOOLCHAIN}" | sed 's/go*/v/' | tr -d '\n') + +AKASH_DIRENV_SET=1 + +if [[ "$OSTYPE" == "darwin"* ]]; then + # on MacOS disable deprecation warnings security framework + CGO_CFLAGS=-Wno-deprecated-declarations + + export CGO_CFLAGS + + if ! has brew; then + echo -e "\033[31mhomebrew is not installed. visit https://brew.sh" + exit 1 + fi + + if [[ -z $HOMEBREW_PREFIX ]]; then + HOMEBREW_PREFIX=$(brew --prefix) + fi + + # don't use brew list, as it is utterly slow + getopt_bin=${HOMEBREW_PREFIX}/opt/gnu-getopt/bin + + if [ ! -d "$getopt_bin" ]; then + echo -e "\033[31mgnu-getopt is not installed. to install \"brew install gnu-getopt\"" + exit 1 + else + path=$path:"$getopt_bin" + fi +fi + +if [[ -z "$GOPATH" ]]; then + GOPATH=$(go env GOPATH) + export GOPATH +fi + +PATH_add "$path" + +export SEMVER +export GOTOOLCHAIN +export GOTOOLCHAIN_SEMVER +export AKASH_DIRENV_SET diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..5ce3ffe --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,48 @@ +--- +name: release +defaults: + run: + shell: bash +on: + push: + tags: + - v* + +jobs: + publish: + runs-on: ubuntu-latest + env: + DOCKER_CLI_EXPERIMENTAL: "enabled" + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Detect required Go version + run: | + toolchain=$(./script/tools.sh gotoolchain | sed 's/go*//') + echo "GOVERSION=${toolchain}" >> $GITHUB_ENV + - uses: actions/setup-go@v5 + with: + go-version: "${{ env.GOVERSION }}" + - name: Setup direnv + uses: HatsuneMiku3939/direnv-action@v1 + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: release version + run: echo "RELEASE_TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + - name: Login to GHCR + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: release publish + run: | + sudo rm -rf dist + make release + env: + GORELEASER_RELEASE: true + GORELEASER_MOUNT_CONFIG: true + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..747b01f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +e2e-test +dist diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..de58a70 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,65 @@ +--- +project_name: e2e-test +env: + - GO111MODULE=on +builds: + - id: e2e-test-linux + binary: e2e-test + main: ./ + goarch: + - amd64 + - arm64 + goos: + - linux + flags: + - -trimpath +archives: + - format: binary +dockers: + - dockerfile: Dockerfile + use: buildx + goos: linux + goarch: amd64 + build_flag_templates: + - --platform=linux/amd64 + - --label=org.opencontainers.image.title={{ .ProjectName }} + - --label=org.opencontainers.image.description={{ .ProjectName }} + - --label=org.opencontainers.image.url={{.GitURL}} + - --label=org.opencontainers.image.source={{.GitURL}} + - --label=org.opencontainers.image.version={{ .Version }} + - --label=org.opencontainers.image.created={{ time "2006-01-02T15:04:05Z07:00" }} + - --label=org.opencontainers.image.revision={{ .FullCommit }} + image_templates: + - "{{ .Env.DOCKER_IMAGE }}:{{ .ShortCommit }}-amd64" + - "{{ .Env.DOCKER_IMAGE }}:{{ .Version }}-amd64" + - "{{ .Env.DOCKER_IMAGE }}:latest-amd64" + - dockerfile: Dockerfile + use: buildx + goos: linux + goarch: arm64 + build_flag_templates: + - --platform=linux/arm64 + - --label=org.opencontainers.image.title={{ .ProjectName }} + - --label=org.opencontainers.image.description={{ .ProjectName }} + - --label=org.opencontainers.image.url={{.GitURL}} + - --label=org.opencontainers.image.source={{.GitURL}} + - --label=org.opencontainers.image.version={{ .Version }} + - --label=org.opencontainers.image.created={{ time "2006-01-02T15:04:05Z07:00" }} + - --label=org.opencontainers.image.revision={{ .FullCommit }} + image_templates: + - "{{ .Env.DOCKER_IMAGE }}:{{ .ShortCommit }}-arm64" + - "{{ .Env.DOCKER_IMAGE }}:{{ .Version }}-arm64" + - "{{ .Env.DOCKER_IMAGE }}:latest-arm64" +docker_manifests: + - name_template: "{{ .Env.DOCKER_IMAGE }}:{{ .ShortCommit }}" + image_templates: + - "{{ .Env.DOCKER_IMAGE }}:{{ .ShortCommit }}-amd64" + - "{{ .Env.DOCKER_IMAGE }}:{{ .ShortCommit }}-arm64" + - name_template: "{{ .Env.DOCKER_IMAGE }}:{{ .Version }}" + image_templates: + - "{{ .Env.DOCKER_IMAGE }}:{{ .Version }}-amd64" + - "{{ .Env.DOCKER_IMAGE }}:{{ .Version }}-arm64" + - name_template: "{{ .Env.DOCKER_IMAGE }}:latest" + image_templates: + - "{{ .Env.DOCKER_IMAGE }}:latest-amd64" + - "{{ .Env.DOCKER_IMAGE }}:latest-arm64" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..90bb2a9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM debian + +COPY ./e2e-test /usr/local/bin + +RUN \ + apt-get update \ + && apt-get install -y --no-install-recommends \ + tini \ + ca-certificates \ + curl \ + bash \ + && rm -rf /var/lib/apt/lists/* + +ENTRYPOINT ["/tini", "--", "e2e-test"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5f0e7da --- /dev/null +++ b/Makefile @@ -0,0 +1,55 @@ +GORELEASER_RELEASE ?= false +GORELEASER_DEBUG ?= false +GORELEASER_IMAGE := ghcr.io/goreleaser/goreleaser:$(GOTOOLCHAIN_SEMVER) +GORELEASER_MOUNT_CONFIG ?= false +GORELEASER_SNAPSHOT ?= false +GORELEASER_SKIP_FLAGS := $(GORELEASER_SKIP) +GORELEASER_SKIP := +GORELEASER_CONFIG ?= .goreleaser.yaml + +RELEASE_DOCKER_IMAGE ?= ghcr.io/akash-network/e2e-test + +GO_MOD_NAME := $(shell go list -m 2>/dev/null) + +null := +space := $(null) # +comma := , + +ifneq ($(GORELEASER_RELEASE),true) + GITHUB_TOKEN= + GORELEASER_SKIP_FLAGS += publish +endif + +ifneq ($(GORELEASER_SKIP_FLAGS),) + GORELEASER_SKIP := --skip=$(subst $(space),$(comma),$(strip $(GORELEASER_SKIP_FLAGS))) +endif + +ifeq ($(GORELEASER_MOUNT_CONFIG),true) + GORELEASER_IMAGE := -v $(HOME)/.docker/config.json:/root/.docker/config.json $(GORELEASER_IMAGE) +endif + +.PHONY: release +release: + docker run \ + --rm \ + -e MOD="$(GO_MOD)" \ + -e BUILD_TAGS="$(BUILD_TAGS)" \ + -e BUILD_VARS="$(GORELEASER_BUILD_VARS)" \ + -e STRIP_FLAGS="$(GORELEASER_STRIP_FLAGS)" \ + -e LINKMODE="$(GO_LINKMODE)" \ + -e GITHUB_TOKEN="$(GITHUB_TOKEN)" \ + -e GORELEASER_CURRENT_TAG="$(RELEASE_TAG)" \ + -e DOCKER_IMAGE=$(RELEASE_DOCKER_IMAGE) \ + -e GOTOOLCHAIN="$(GOTOOLCHAIN)" \ + -e GOPATH=/go \ + -v /var/run/docker.sock:/var/run/docker.sock \ + -v $(GOPATH):/go \ + -v $(shell pwd):/go/src/$(GO_MOD_NAME) \ + -w /go/src/$(GO_MOD_NAME)\ + $(GORELEASER_IMAGE) \ + -f "$(GORELEASER_CONFIG)" \ + release \ + $(GORELEASER_SKIP) \ + --debug=$(GORELEASER_DEBUG) \ + --snapshot=$(GORELEASER_SNAPSHOT) \ + --clean diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..18212e7 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/akash-network/e2e-test + +go 1.21 + +require github.com/gorilla/mux v1.8.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7128337 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..2b16015 --- /dev/null +++ b/main.go @@ -0,0 +1,161 @@ +package main + +import ( + "context" + "fmt" + "io" + "io/ioutil" + "math" + "net" + "net/http" + "os" + "os/signal" + "strconv" + "strings" + "sync" + "syscall" + "time" + + "github.com/gorilla/mux" +) + +func main() { + port := "8080" + e2eDir := "/var/lib/e2e-test" + + if val := os.Getenv("E2E_PORT"); val != "" { + res, err := strconv.ParseUint(val, 10, 32) + if err != nil || res > math.MaxUint16 { + fmt.Printf("E2E_PORT contains invalid value\n") + return + } + + port = val + } + + if val := os.Getenv("E2E_DIR"); val != "" { + e2eDir = val + } + + e2eDir = strings.TrimSuffix(e2eDir, "/") + + if err := os.MkdirAll(e2eDir, os.ModeDir); err != nil { + fmt.Printf("couldn't create work dir: %s\n", err.Error()) + return + } + + file, err := os.OpenFile(e2eDir+"/testfile", os.O_RDWR|os.O_CREATE, 0644) + if err != nil { + fmt.Printf("%s\n", err.Error()) + } + + fmt.Printf("saving to file %s\n", e2eDir+"/testfile") + + data, err := ioutil.ReadAll(file) + if err != nil { + fmt.Printf("%s\n", err.Error()) + } + + if string(data) == "" { + if _, err = file.WriteString("default"); err != nil { + fmt.Printf("%s\n", err.Error()) + } + + _, _ = file.Seek(0, io.SeekStart) + } + + var lock sync.Mutex + + shutdownCh := make(chan struct{}, 1) + + ctx, cancel := context.WithCancel(context.Background()) + + srv := &http.Server{ + Addr: "0.0.0.0:" + port, + Handler: newRouter(shutdownCh, file, &lock), + BaseContext: func(_ net.Listener) context.Context { + return ctx + }, + } + + wg := sync.WaitGroup{} + + wg.Add(2) + + go func() { + defer wg.Done() + + err = srv.ListenAndServe() + if err != nil { + fmt.Println(err.Error()) + } + }() + + go func() { + defer wg.Done() + + select { + case <-ctx.Done(): + break + case <-shutdownCh: + <-time.After(3 * time.Second) + + _ = syscall.Kill(syscall.Getpid(), syscall.SIGINT) + } + }() + + fmt.Printf("app started\n") + + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) + sig := <-ch + + cancel() + fmt.Printf("service received signal: %s. shutting down\n", sig.String()) + + go func() { + _ = srv.Shutdown(context.Background()) + }() + + wg.Wait() +} + +func newRouter(shChan chan<- struct{}, file *os.File, lock *sync.Mutex) *mux.Router { + router := mux.NewRouter() + + router.HandleFunc("/kill", + func(resp http.ResponseWriter, req *http.Request) { + resp.WriteHeader(http.StatusOK) + shChan <- struct{}{} + }). + Methods("GET") + + router.HandleFunc("/GET/value", + func(resp http.ResponseWriter, request *http.Request) { + defer lock.Unlock() + lock.Lock() + + _, _ = file.Seek(0, io.SeekStart) + + data, _ := ioutil.ReadAll(file) + + _, _ = resp.Write(data) + }).Methods("GET") + + router.HandleFunc("/SET/value", + func(resp http.ResponseWriter, request *http.Request) { + defer lock.Unlock() + lock.Lock() + + _, _ = file.Seek(0, io.SeekStart) + + data, _ := ioutil.ReadAll(request.Body) + + _, _ = file.Write(data) + _ = file.Sync() + + resp.WriteHeader(http.StatusOK) + }).Methods("GET") + + return router +} diff --git a/script/semver.sh b/script/semver.sh new file mode 100755 index 0000000..abdecaa --- /dev/null +++ b/script/semver.sh @@ -0,0 +1,256 @@ +#!/usr/bin/env bash + +set -o errexit -o nounset -o pipefail + +SEMVER_REGEX="^[v|V]?(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$" + +SEMVER_REGEX_LEGACY="^[v|V]?(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\.0|[1-9][0-9]*)?(\\-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$" + +PROG=semver +PROG_VERSION=2.1.0 + +USAGE="\ +Usage: + $PROG bump (major|minor|patch|release|prerel |build ) + $PROG compare + $PROG get (major|minor|patch|release|prerel|build) + $PROG --help + $PROG --version +Arguments: + A version must match the following regex pattern: + \"${SEMVER_REGEX}\". + In english, the version must match X.Y.Z(-PRERELEASE)(+BUILD) + where X, Y and Z are positive integers, PRERELEASE is an optional + string composed of alphanumeric characters and hyphens and + BUILD is also an optional string composed of alphanumeric + characters and hyphens. + See definition. + String that must be composed of alphanumeric characters and hyphens. + String that must be composed of alphanumeric characters and hyphens. +Options: + -v, --version Print the version of this tool. + -h, --help Print this help message. +Commands: + bump Bump by one of major, minor, patch, prerel, build + or a forced potentially conflicting version. The bumped version is + shown to stdout. + compare Compare with , output to stdout the + following values: -1 if is newer, 0 if equal, 1 if + older. + get Extract given part of , where part is one of major, minor, + patch, prerel, build. + validate Check version string is valid" + + +function error { + echo -e "$1" >&2 + exit 1 +} + +function usage-help { + error "$USAGE" +} + +function usage-version { + echo -e "${PROG}: $PROG_VERSION" + exit 0 +} + +function validate-version { + local version=$1 + if [[ "$version" =~ $SEMVER_REGEX ]]; then + # if a second argument is passed, store the result in var named by $2 + if [[ "$#" -eq "2" ]]; then + local major=${BASH_REMATCH[1]} + local minor=${BASH_REMATCH[2]} + local patch=${BASH_REMATCH[3]} + local prere=${BASH_REMATCH[4]} + local build=${BASH_REMATCH[6]} + eval "$2=(\"${major}\" \"${minor}\" \"${patch}\" \"${prere}\" \"${build}\")" + else + echo "$version" + fi + elif [[ "$version" =~ $SEMVER_REGEX_LEGACY ]]; then + # if a second argument is passed, store the result in var named by $2 + if [[ "$#" -eq "2" ]]; then + local major=${BASH_REMATCH[1]} + local minor=${BASH_REMATCH[2]} + local patch=0 + local prere=${BASH_REMATCH[4]} + local build=${BASH_REMATCH[6]} + eval "$2=(\"${major}\" \"${minor}\" \"${patch}\" \"${prere}\" \"${build}\")" + else + echo "$version" + fi + else + error "version $version does not match the semver scheme 'X.Y.Z(-PRERELEASE)(+BUILD)'. See help for more information." + fi +} + +function compare-version { + validate-version "$1" V + validate-version "$2" V_ + + # MAJOR, MINOR and PATCH should compare numerically + for i in 0 1 2; do + local diff=$((${V[$i]} - ${V_[$i]})) + if [[ ${diff} -lt 0 ]]; then + echo -1; + return 0 + elif [[ ${diff} -gt 0 ]]; then + echo 1; + return 0 + fi + done + + # PREREL should compare with the ASCII order. + if [[ -z "${V[3]}" ]] && [[ -n "${V_[3]}" ]]; then + echo -1; + return 0; + elif [[ -n "${V[3]}" ]] && [[ -z "${V_[3]}" ]]; then + echo 1; + return 0; + elif [[ -n "${V[3]}" ]] && [[ -n "${V_[3]}" ]]; then + if [[ "${V[3]}" > "${V_[3]}" ]]; then + echo 1; + return 0; + elif [[ "${V[3]}" < "${V_[3]}" ]]; then + echo -1; + return 0; + fi + fi + + echo 0 +} + +function command-bump { + local new; + local version; + local sub_version; + local command; + + case $# in + 2) + case $1 in + major | minor | patch | release) + command=$1; + version=$2 ;; + *) + usage-help ;; + esac ;; + 3) + case $1 in + prerel | build) + command=$1; + sub_version=$2 version=$3 ;; + *) + usage-help ;; + esac ;; + *) + usage-help ;; + esac + + validate-version "$version" parts + # shellcheck disable=SC2154 + local major="${parts[0]}" + local minor="${parts[1]}" + local patch="${parts[2]}" + local prere="${parts[3]}" + local build="${parts[4]}" + + case "$command" in + major) + new="$((major + 1)).0.0" ;; + minor) + new="${major}.$((minor + 1)).0" ;; + patch) + new="${major}.${minor}.$((patch + 1))" ;; + release) + new="${major}.${minor}.${patch}" ;; + prerel) + new=$(validate-version "${major}.${minor}.${patch}-${sub_version}") ;; + build) + new=$(validate-version "${major}.${minor}.${patch}${prere}+${sub_version}") ;; + *) + usage-help ;; + esac + + echo "$new" + exit 0 +} + +function command-compare { + local v; + local v_; + + case $# in + 2) + v=$(validate-version "$1"); + v_=$(validate-version "$2") ;; + *) + usage-help ;; + esac + + compare-version "$v" "$v_" + exit 0 +} + +# shellcheck disable=SC2034 +function command-get { + local part version + + if [[ "$#" -ne "2" ]] || [[ -z "$1" ]] || [[ -z "$2" ]]; then + usage-help + fi + + part="$1" + version="$2" + + validate-version "$version" parts + local major="${parts[0]}" + local minor="${parts[1]}" + local patch="${parts[2]}" + local prerel="${parts[3]:1}" + local build="${parts[4]:1}" + + case "$part" in + "major-minor") + echo "$major.$minor" + ;; + major | minor | patch | release | prerel | build) + echo "${!part}" ;; + *) + usage-help ;; + esac + + exit 0 +} + +case $# in + 0) + echo "Unknown command: $*"; + usage-help ;; +esac + +case $1 in + --help | -h) + echo -e "$USAGE"; + exit 0 ;; + --version | -v) + usage-version ;; + bump) + shift; + command-bump "$@" ;; + get) + shift; + command-get "$@" ;; + compare) + shift; + command-compare "$@" ;; + validate) + shift; + validate-version "$@" V ;; + *) + echo "Unknown arguments: $*"; + usage-help ;; +esac diff --git a/script/tools.sh b/script/tools.sh new file mode 100755 index 0000000..5807313 --- /dev/null +++ b/script/tools.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" +SEMVER=$SCRIPT_DIR/semver.sh + +gomod="$SCRIPT_DIR/../go.mod" + +function get_gotoolchain() { + local gotoolchain + local goversion + local local_goversion + + gotoolchain=$(grep -E '^toolchain go[0-9]{1,}.[0-9]{1,}.[0-9]{1,}$' < "$gomod" | cut -d ' ' -f 2 | tr -d '\n') + goversion=$(grep -E '^go [0-9]{1,}.[0-9]{1,}(.[0-9]{1,})?$' < "$gomod" | cut -d ' ' -f 2 | tr -d '\n') + + if [[ ${gotoolchain} == "" ]]; then + # determine go toolchain from go version in go.mod + if which go > /dev/null 2>&1 ; then + local_goversion=$(GOTOOLCHAIN=local go version | cut -d ' ' -f 3 | sed 's/go*//' | tr -d '\n') + if [[ $($SEMVER compare "v$local_goversion" v"$goversion") -ge 0 ]]; then + goversion=$local_goversion + else + local_goversion= + fi + fi + + if [[ "$local_goversion" == "" ]]; then + goversion=$(curl -s "https://go.dev/dl/?mode=json&include=all" | jq -r --arg regexp "^go$goversion" '.[] | select(.stable == true) | select(.version | match($regexp)) | .version' | head -n 1 | sed -e s/^go//) + fi + + if [[ $goversion != "" ]] && [[ $($SEMVER compare "v$goversion" v1.21.0) -ge 0 ]]; then + gotoolchain=go${goversion} + else + gotoolchain=go$(grep -E '^go [0-9]{1,}.[0-9]{1,}$' < "$gomod" | cut -d ' ' -f 2 | tr -d '\n').0 + fi + fi + + echo -n "$gotoolchain" +} + +function build_akash() { + dev_cache=${AP_DEVCACHE_BIN} + cd "$1" || exit 1 + export AKASH_ROOT="$1" + source .env + make akash AKASH="${dev_cache}/akash" +} + +function build_akash_docker() { + cd "$1" || exit 1 + export AKASH_ROOT="$1" + source .env + make docker-image +} + +case "$1" in +gotoolchain) + get_gotoolchain + ;; +build-akash) + shift + build_akash "$@" + ;; +build-akash-docker) + shift + build_akash_docker "$@" + ;; +esac