Skip to content

Commit

Permalink
further automate new project setup with use_starter script, update de…
Browse files Browse the repository at this point in the history
…ps + pre-commit hooks, minor fixes
  • Loading branch information
toozej committed Sep 25, 2024
1 parent c1dfd15 commit 36646d9
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 28 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ QUAY_USERNAME=XXXX
QUAY_TOKEN=XXXX
DOCKERHUB_USERNAME=XXXX
DOCKERHUB_TOKEN=XXXX
SNYK_TOKEN=XXXX
3 changes: 3 additions & 0 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
run: |
pip install pre-commit
make pre-commit
- name: Run tests
run: |
make test
gitleaks:
name: gitleaks
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ repos:

# run semgrep on all known filetypes
- repo: https://github.com/returntocorp/semgrep
rev: 'v1.84.1'
rev: 'v1.89.0'
hooks:
- id: semgrep
# See https://semgrep.dev/explore to select a ruleset and copy its URL
Expand Down
10 changes: 6 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ RUN go mod download

COPY . ./

FROM init as vet
FROM init AS vet
RUN go vet ./...

# run tests
FROM init as test
RUN go test -coverprofile c.out -v ./...
FROM init AS test
RUN go test -coverprofile c.out -v ./... && \
echo "Statements missing coverage" && \
grep -v -e " 1$" c.out

# build binary
FROM init as build
FROM init AS build
ARG LDFLAGS

RUN CGO_ENABLED=0 go build -ldflags="${LDFLAGS}"
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile.distroless
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ RUN go mod download

COPY . ./

FROM init as vet
FROM init AS vet
RUN go vet ./...

# run tests
FROM init as test
FROM init AS test
RUN go test -coverprofile c.out -v ./...

# build binary
FROM init as build
FROM init AS build
ARG LDFLAGS

RUN CGO_ENABLED=0 go build -ldflags="${LDFLAGS}"
Expand Down
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ vet: ## Run `go vet` in Docker
docker build --target vet -f $(CURDIR)/Dockerfile -t toozej/golang-starter:latest .

test: ## Run `go test` in Docker
docker build --target test -f $(CURDIR)/Dockerfile -t toozej/golang-starter:latest .
@echo -e "\nStatements missing coverage"
@grep -v -e " 1$$" c.out
docker build --progress=plain --target test -f $(CURDIR)/Dockerfile -t toozej/golang-starter:latest .

build: ## Build Docker image, including running tests
docker build -f $(CURDIR)/Dockerfile -t toozej/golang-starter:latest .
Expand Down Expand Up @@ -123,6 +121,9 @@ install: local-build local-verify ## Install compiled binary to local machine
sudo cp $(CURDIR)/out/golang-starter /usr/local/bin/golang-starter
sudo chmod 0755 /usr/local/bin/golang-starter

assert-secrets-gh: ## Assert secrets from .env to GitHub Actions Secrets
$(CURDIR)/scripts/upload_secrets_to_github.sh golang-starter

docker-login: ## Login to Docker registries used to publish images to
if test -e $(CURDIR)/.env; then \
export `cat $(CURDIR)/.env | xargs`; \
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/muesli/roff v0.1.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1
go.uber.org/automaxprocs v1.5.3
go.uber.org/automaxprocs v1.6.0
)

require (
Expand All @@ -20,7 +20,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/mango v0.2.0 // indirect
github.com/muesli/mango-pflag v0.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand All @@ -31,9 +31,9 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ github.com/pelletier/go-toml/v2 v2.2.0 h1:QLgLl2yMN7N+ruc31VynXs1vhMZa7CeHHejIeB
github.com/pelletier/go-toml/v2 v2.2.0/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -257,6 +259,8 @@ go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -297,6 +301,8 @@ golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0J
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down Expand Up @@ -430,6 +436,8 @@ golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -453,6 +461,8 @@ golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
2 changes: 1 addition & 1 deletion scripts/completions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ set -e
rm -rf completions
mkdir completions
for sh in bash zsh fish; do
go run ./cmd/golang-starter/ completion "$sh" >"completions/golang-starter.$sh"
go run main.go completion "$sh" >"completions/golang-starter.$sh"
done
51 changes: 51 additions & 0 deletions scripts/upload_secrets_to_github.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash
set -Eeuo pipefail

# Helper function for error handling
function handle_error {
echo "Error: $1"
exit 1
}

# Validate that .env exists
if [[ ! -f .env ]]; then
handle_error ".env file not found. Ensure it exists before running this script."
fi

# Read GitHub username and token from the environment
GITHUB_USERNAME="${GITHUB_USERNAME:-}"
GITHUB_TOKEN="${GITHUB_TOKEN:-}"

if [[ -z "$GITHUB_USERNAME" || -z "$GITHUB_TOKEN" ]]; then
handle_error "GITHUB_USERNAME or GITHUB_TOKEN is not set in the environment. Please set them in .env."
fi

# Helper function to upload secrets to GitHub Actions
upload_secrets_to_github() {
echo "Pushing .env entries to GitHub Actions secrets for repo: $GITHUB_USERNAME/$REPO_NAME..."

while IFS='=' read -r key value; do
if [[ "$key" != "" ]]; then
response=$(curl -s -X PUT \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"encrypted_value\":\"$value\",\"key_id\":\"$key\"}" \
"https://api.github.com/repos/$GITHUB_USERNAME/$REPO_NAME/actions/secrets/$key")

if [[ "$response" == *"errors"* ]]; then
handle_error "Failed to set secret $key in GitHub Actions. Response: $response"
fi
fi
done < .env
echo "Secrets successfully uploaded to GitHub Actions."
}

# Main script logic
REPO_NAME="$1"

if [[ -z "$REPO_NAME" ]]; then
handle_error "Usage: $0 <repo_name>"
fi

# Execute the function to upload secrets
upload_secrets_to_github
Loading

0 comments on commit 36646d9

Please sign in to comment.