Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
madhuravius committed Jun 3, 2023
0 parents commit 670c5a2
Show file tree
Hide file tree
Showing 24 changed files with 687 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/*
!bin/.gitkeep
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @madhuravius
20 changes: 20 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
labels:
- dependencies
- actions
- Skip Changelog
schedule:
interval: weekly
day: sunday
- package-ecosystem: gomod
directory: /
labels:
- dependencies
- go
- Skip Changelog
schedule:
interval: weekly
day: sunday
53 changes: 53 additions & 0 deletions .github/workflows/build-and-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Credit to Mark van Holsteijn - shorturl.at/rzLPR
name: Build and publish the container image

on:
push:
tags:
- '*'
branches:
- 'main'

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
-
name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0

-
name: Get tag
id: repository
run: echo "tag=$(git describe --tags HEAD)" > $GITHUB_ENV

-
name: Set up QEMU
uses: docker/setup-qemu-action@v2

-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

-
name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

-
name: Build and push
uses: docker/build-push-action@v4
with:
context: .
file: ./Containerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ghcr.io/${{ github.repository }}:${{ env.tag }}
15 changes: 15 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Lint

on:
push:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '>=1.20.0'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
25 changes: 25 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Go Releaser

on:
release:
types:
- published

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v4
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
if: startsWith(github.ref, 'refs/tags/')
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
15 changes: 15 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Test

on:
push:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '>=1.20.0'
- run: go install gotest.tools/gotestsum@latest
- run: gotestsum --format testname -- -coverprofile=cover.out ./...
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
.idea

bin/*
!bin/.gitkeep
coverage.out
15 changes: 15 additions & 0 deletions Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM docker.io/golang:latest as build

WORKDIR /app
COPY Makefile .
COPY go.* .
RUN make deps

COPY . .
RUN make build

# cannot use scratch image as some bits are needed for the webserver
FROM docker.io/debian:stable-slim
COPY --from=build /app/bin/ready /app/bin/ready

ENTRYPOINT ["/app/bin/ready"]
19 changes: 19 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2023 madhuravius

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
deps:
go mod download
.PHONY: deps

init: deps
go generate ./...
.PHONY: init

build-image:
docker build -f ./Containerfile -t ghcr.io/madhuravius/ready:latest .
.PHONY: build-image

build:
go build -ldflags="-s -w" -o ./bin/ready main.go
.PHONY: build

lint:
docker run \
--rm \
-v $(shell pwd):/app \
-w /app \
docker.io/golangci/golangci-lint:v1.52 \
golangci-lint run
.PHONY: lint

format:
go fmt ./...
.PHONY: format

test:
go test ./... -v -coverprofile=coverage.out
.PHONY: test
Empty file added bin/.gitkeep
Empty file.
17 changes: 17 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module ready

go 1.20

require (
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.25.5
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/urfave/cli/v2 v2.25.5 h1:d0NIAyhh5shGscroL7ek/Ya9QYQE0KNabJgiUinIQkc=
github.com/urfave/cli/v2 v2.25.5/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
42 changes: 42 additions & 0 deletions internal/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package internal

import "github.com/urfave/cli/v2"

func NewApp() *cli.App {
return &cli.App{
Name: "ready",
Usage: "wait for a group of hosts and ports to be ready",
Commands: []*cli.Command{
{
Name: RunKey,
Aliases: []string{""},
Usage: "start ready",
Action: func(cCtx *cli.Context) error {
if err := RunLoop(cCtx); err != nil {
return err
}
return nil
},
},
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: DebugKey,
Required: false,
Value: true,
Usage: "if enabled, will print out logs",
},
&cli.StringSliceFlag{
Name: HostPortsKey,
Required: true,
Usage: "as a csv, specify a range of hosts and ports to check (ex: \"localhost:3000,test:1234\" )",
},
&cli.IntFlag{
Value: 30,
Name: TimeoutKey,
Required: false,
Usage: "as an integer, maximum number of seconds to wait and error if ready checks do not all pass by",
},
},
}
}
30 changes: 30 additions & 0 deletions internal/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package internal

import (
"errors"
"time"
)

const (
// RunKey - main key for subcommand to run the app
RunKey = "run"
// DebugKey - enable debug mode that gives some more logs
DebugKey = "debug"
// HostPortsKey - flag to search on when trying to search against ports
HostPortsKey = "host-ports"
// TimeoutKey - flag to wait for application to error if not found
TimeoutKey = "timeout"
// DebugLogInterval - amount of time to wait in between each call
DebugLogInterval = time.Second * 5
// PortCheckTimeout - amount of time to check if a port is open
PortCheckTimeout = time.Second
// Heartbeat - regular heartbeat for all critical functions in core loop
Heartbeat = time.Millisecond * 100
)

var (
// ErrorTimeoutNotReady - error that gets raised when ready checks are not met in time
ErrorTimeoutNotReady = errors.New("err: operation timed out, ready checks not all met")
// ErrorHostPortBadFormat - error if bad input err
ErrorHostPortBadFormat = errors.New("err: host port provided in bad format (not - host:port)")
)
15 changes: 15 additions & 0 deletions internal/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package internal

import "time"

type HostPort struct {
host string
port int
}

type RunConfig struct {
debug bool
hostPortsReady map[HostPort]bool
started time.Time
timeout time.Duration
}
Loading

0 comments on commit 670c5a2

Please sign in to comment.