Skip to content

Commit e21fd8c

Browse files
Merge pull request #8 from datastack-net/feature/go
Porting to Go
2 parents ad0b493 + a87201c commit e21fd8c

File tree

14 files changed

+3106
-188
lines changed

14 files changed

+3106
-188
lines changed

.cache/.gitkeep

Whitespace-only changes.

dockerized.env renamed to .env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
DEFAULT_ARCH=x86_64
22
ALPINE_VERSION=3.14.2
3+
AB_VERSION=latest
34
AWS_VERSION=2.4.24
45
DEFAULT_BASE_IMAGE=alpine
56
DEFAULT_BASE_IMAGE_VERSION=${ALPINE_VERSION}
67
DEFAULT_BASE=${DEFAULT_BASE_IMAGE}:${DEFAULT_BASE_IMAGE_VERSION}
78
DOCTL_VERSION=1.70.0
89
DOTNET_VERSION=6.0
10+
GIT_VERSION=2.32.0
911
GH_VERSION=2.5.2
1012
GO_VERSION=1.17.8
1113
HELM_VERSION=3.8.1

.github/workflows/test.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Compile and Test
2+
on: [push]
3+
4+
jobs:
5+
CompileAndTest:
6+
runs-on: ${{ matrix.os }}
7+
strategy:
8+
matrix:
9+
os:
10+
- ubuntu-20.04
11+
# - windows-2022
12+
# - macos-10.15
13+
max-parallel: 1
14+
fail-fast: true
15+
steps:
16+
# region mac
17+
- name: Install Docker
18+
if: runner.os == 'macOS'
19+
uses: docker-practice/actions-setup-docker@master
20+
# endregion
21+
22+
- name: Checkout Dockerized
23+
uses: actions/checkout@v2
24+
25+
- run: bin/dockerized --compile --help
26+
shell: bash
27+
28+
- run: bin/dockerized go version
29+
shell: bash
30+
31+
- name: "Test: version from environment variable"
32+
run: GO_VERSION=1.16.15 bin/dockerized go version | grep "go1.16.15"
33+
shell: bash
34+
35+
- name: "Test: version from dockerized.env"
36+
run: |
37+
mkdir -p project
38+
cd project
39+
echo 'GO_VERSION=1.17.8' > dockerized.env
40+
../bin/dockerized go version | grep "1.17.8"
41+
shell: bash
42+
43+
# region windows
44+
- if: runner.os == 'windows'
45+
name: "dockerized --compile (cmd)"
46+
shell: cmd
47+
run: bin/dockerized --compile
48+
- if: runner.os == 'windows'
49+
name: "dockerized --compile (powershell)"
50+
shell: cmd
51+
run: bin/dockerized --compile
52+
# endregion

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
config.env
1+
build/*
2+
.cache/*
3+
.go/*

DEV.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ services:
1616
- `image: golang:latest` specifies the docker image to use. You can find these on [Docker Hub](https://hub.docker.com/).
1717
- `entrypoint` is the command to run when the service starts.
1818

19-
That's it. Now you can run `dockerize go`.
19+
That's it. Now you can run `dockerized go`.
2020

2121
## Configurable version
2222

@@ -25,7 +25,7 @@ Let's make sure users can choose the version of the command.
2525
Check the current version of the command. Often with `<command> --version` or `<command> version`.
2626

2727
```shell
28-
dockerize go version
28+
dockerized go version
2929
> go version go1.17.8 linux/amd64
3030
```
3131

@@ -38,7 +38,7 @@ Replace the version tag `latest` with `${GO_VERSION}`:
3838
entrypoint: [ "go" ]
3939
```
4040

41-
Set the global default version in [dockerized.env](dockerized.env):
41+
Set the global default version in [.env](.env):
4242

4343
```dotenv
4444
GO_VERSION=1.17.8
@@ -50,19 +50,19 @@ GO_VERSION=1.17.8
5050

5151
```bash
5252
# Create a branch:
53-
dockerize git checkout -b fork
53+
dockerized git checkout -b fork
5454
5555
# Commit your changes:
56-
dockerize git commit -am "Add go"
56+
dockerized git commit -am "Add go"
5757
5858
# Authenticate to github:
59-
dockerize gh auth login
59+
dockerized gh auth login
6060
```
6161

6262
- Choose SSH, and Browser authentication
6363
- See [gh](apps/gh/Readme.md) for more information.
6464

6565
```bash
6666
# Create a PR:
67-
dockerize gh pr create
67+
dockerized gh pr create
6868
```

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 DataStack.net
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

bin/dockerized

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,100 @@
11
#!/usr/bin/env bash
2+
case "$OSTYPE" in
3+
msys | cygwin)
4+
PWD_ARGS="-W"
5+
;;
6+
esac
7+
28
# CONSTANTS
3-
DOCKERIZED_ENV_FILE_NAME="dockerized.env"
4-
DOCKERIZED_ROOT=$(realpath $(dirname "$BASH_SOURCE")/..)
9+
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd $PWD_ARGS)
10+
DOCKERIZED_ROOT=$(dirname "$SCRIPT_DIR")
511
DOCKERIZED_COMPOSE_FILE="${DOCKERIZED_ROOT}/docker-compose.yml"
6-
DOCKERIZED_ENV_FILE="${DOCKERIZED_ROOT}/${DOCKERIZED_ENV_FILE_NAME}"
7-
HOST_HOME="$HOME"
12+
DOCKERIZED_BINARY="${DOCKERIZED_ROOT}/build/dockerized"
813

9-
# OPTIONS
10-
DOCKERIZED_OPT_VERBOSE=
11-
12-
# RUNTIME VARIABLES
13-
HOST_PWD=
14-
HOST_HOSTNAME=$(hostname)
15-
HOST_DIR_NAME=$(basename "$PWD")
16-
HOST_DIR_NAME=${HOST_DIR_NAME%/}
17-
SERVICE_NAME=
18-
SERVICE_ARGS=
14+
# region COMPILE DOCKERIZED
15+
DOCKERIZED_COMPILE=
16+
if [ "$1" == "--compile" ] || [ "$1" == "--compile=docker" ]; then
17+
DOCKERIZED_COMPILE="docker"
18+
shift
19+
fi
1920

20-
# PARSE OPTIONS
21-
if [ "$1" == "-v" ]; then
22-
DOCKERIZED_OPT_VERBOSE="1"
23-
shift
21+
if [ "$1" == "--compile=host" ]; then
22+
DOCKERIZED_COMPILE="host"
23+
shift
2424
fi
2525

26-
case "$(uname -s)" in
27-
CYGWIN* | MINGW32* | MSYS* | MINGW*)
28-
HOST_PWD=$(pwd -W)
26+
case "$OSTYPE" in
27+
msys | cygwin)
28+
GOOS=windows
29+
;;
30+
darwin*)
31+
GOOS=darwin
32+
;;
33+
# Operating systems below not tested. Logic based on:
34+
# - https://github.com/dylanaraps/neofetch/issues/433
35+
# - https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63
36+
openbsd*)
37+
GOOS=openbsd
38+
;;
39+
freebsd*)
40+
GOOS=freebsd
41+
;;
42+
netbsd*)
43+
GOOS=netbsd
44+
;;
45+
solaris*)
46+
GOOS=solaris
2947
;;
48+
# default
3049
*)
31-
HOST_PWD=$(pwd)
50+
GOOS=linux
3251
;;
3352
esac
3453

35-
SERVICE_ARGS="$@"
36-
SERVICE_NAME="$1"
54+
case "$(uname -m)" in
55+
x86_64)
56+
GOARCH=amd64
57+
;;
58+
i*86)
59+
GOARCH=386
60+
;;
3761

38-
function dotenv() {
39-
FILE="$1"
62+
esac
4063

41-
if [ ! -f "$FILE" ]; then
42-
return
64+
if [ $DOCKERIZED_COMPILE ] || [ ! -f "$DOCKERIZED_BINARY" ]; then
65+
echo "Compiling dockerized..." >&2
66+
GIT_COMMIT=$(cd $DOCKERIZED_ROOT; git rev-list -1 HEAD)
67+
GO_BUILD_ARGS="-ldflags '-X main.Version=${GIT_COMMIT}'"
68+
GO_LDFLAGS="-X main.Version=${GIT_COMMIT}"
69+
if [ $DOCKERIZED_COMPILE == "docker" ]; then
70+
docker run \
71+
--rm \
72+
--entrypoint=go \
73+
-e "GOOS=${GOOS}" \
74+
-v "${DOCKERIZED_ROOT}:/src" \
75+
-v "${DOCKERIZED_ROOT}/build:/build" \
76+
-v "${DOCKERIZED_ROOT}/.cache:/go/pkg" \
77+
-w //src \
78+
"golang:1.17.8" \
79+
build -ldflags "$GO_LDFLAGS" -o //build/ lib/dockerized.go
80+
else
81+
(
82+
cd "$DOCKERIZED_ROOT"
83+
go build -ldflags "$GO_LDFLAGS" -o build/ lib/dockerized.go
84+
)
4385
fi
4486

45-
if [ "$DOCKERIZED_OPT_VERBOSE" ]; then
46-
echo -e "\033[0;32mLoading environment from $FILE\033[0m"
87+
if [ $? -ne 0 ]; then
88+
echo "Failed to compile dockerized" >&2
89+
exit 1
4790
fi
4891

49-
set -o allexport
50-
source "$FILE"
51-
set +o allexport
52-
}
53-
54-
function findUp() {
55-
local FILE="$1"
56-
local DIR="$2"
57-
while [ ! -f "$DIR/$FILE" ]; do
58-
DIR=$(dirname "$DIR")
59-
if [ "$(realpath "$DIR")" == "$(realpath "$(dirname "$DIR")")" ]; then
60-
return 1
61-
fi
62-
done
63-
echo "$DIR/$FILE"
64-
}
65-
66-
function loadEnvironment() {
67-
local CUSTOM_ENV_FILE=$(findUp "$DOCKERIZED_ENV_FILE_NAME" "$HOST_PWD")
68-
dotenv "$HOME/$DOCKERIZED_ENV_FILE_NAME"
69-
dotenv "$CUSTOM_ENV_FILE"
70-
}
71-
72-
loadEnvironment
73-
74-
SERVICE_EXISTS=$(docker-compose -f "$DOCKERIZED_COMPOSE_FILE" config --services | grep -w $SERVICE_NAME)
75-
76-
if [ -z "$SERVICE_EXISTS" ]; then
77-
echo -e "\033[0;31mCommand $1 is not fully supported. Trying r.j3ss.co/$SERVICE_NAME instead." >&2
78-
echo -e "See https://github.com/jessfraz/dockerfiles/blob/master/README.md for more information.\033[0m" >&2
79-
shift
80-
docker \
81-
run --rm -it \
82-
-w "//host/$HOST_DIR_NAME" \
83-
-v "$HOST_PWD://host/$HOST_DIR_NAME" \
84-
-e "HOST_HOSTNAME=$HOST_HOSTNAME" \
85-
r.j3ss.co/$SERVICE_NAME \
86-
"$@"
87-
88-
exit $?
92+
if [ $# -eq 0 ]; then
93+
echo "Compiled dockerized" >&2
94+
exit 0
95+
fi
8996
fi
97+
# endregion
9098

91-
docker-compose \
92-
--env-file "$DOCKERIZED_ENV_FILE" \
93-
-f "$DOCKERIZED_COMPOSE_FILE" \
94-
run --rm \
95-
-e "HOST_HOSTNAME=$HOST_HOSTNAME" \
96-
-v "$HOST_PWD://host/$HOST_DIR_NAME" \
97-
-w "//host/$HOST_DIR_NAME" \
98-
$SERVICE_ARGS
99+
# RUN DOCKERIZED:
100+
"$DOCKERIZED_BINARY" "$@"

0 commit comments

Comments
 (0)