Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/tools/github.com/golan…
Browse files Browse the repository at this point in the history
…gci/golangci-lint-1.64.5
  • Loading branch information
katallaxie authored Feb 14, 2025
2 parents f24f45c + b94b7c5 commit e553f9a
Show file tree
Hide file tree
Showing 25 changed files with 325 additions and 206 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
with:
go-version: ${{ matrix.go }}
cache-dependency-path: |
go.sum
tools/go.sum
- run: make test
- uses: dorny/test-reporter@v1
Expand Down
15 changes: 14 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ on:

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
IMAGE_NAME: zeiss/pkg
REGISTRY: ghcr.io

jobs:
test:
Expand All @@ -19,12 +21,23 @@ jobs:
release:
runs-on: ubuntu-latest
needs: [ test ]
permissions:
packages: write
contents: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version-file: ./go.mod
go-version-file: ./tools/go.mod
cache-dependency-path: |
go.sum
tools/go.sum
- uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- run: make release
if: success()
54 changes: 53 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,59 @@
project_name: pkg

before:
hooks:
- go mod tidy

builds:
- skip: true
- id: waitfor
binary: waitfor-{{.Os}}-{{.Arch}}
main: cmd/waitfor/main.go
goos:
- linux
goarch:
- amd64
- arm
- arm64
ignore:
- goos: darwin
goarch: 386
env:
- CGO_ENABLED=0
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}
no_unique_dist_dir: true

archives:
- id: waitfor
builds:
- waitfor
name_template: "waitfor_{{.Version}}_{{.Os}}_{{.Arch}}"

dockers:
- dockerfile: Dockerfile
goos: linux
goarch: amd64
ids:
- waitfor
image_templates:
- "ghcr.io/zeiss/{{.ProjectName}}/waitfor:latest"
- "ghcr.io/zeiss/{{.ProjectName}}/waitfor:{{.Tag}}"
- "ghcr.io/zeiss/{{.ProjectName}}/waitfor"
build_flag_templates:
- "--pull"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- "--build-arg=BINARY=waitfor-linux-amd64"
- "--platform=linux/amd64"

release:
header: |
## Changelog ({{ .Date }})
Welcome to this new release! We hope you enjoy the changes we've made.
changelog:
groups:
- title: Features
Expand All @@ -24,3 +70,9 @@ changelog:
- "^docs:"
- typo
- (?i)foo

snapshot:
name_template: "{{.Tag}}"

checksum:
name_template: "pkg_checksums.txt"
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM gcr.io/distroless/static:nonroot
ARG BINARY

WORKDIR /
COPY ${BINARY} /main

USER 65532:65532

ENTRYPOINT ["/main"]
File renamed without changes.
70 changes: 70 additions & 0 deletions cmd/runproc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"bytes"
"context"
"log"
"os"

"github.com/spf13/cobra"
)

type config struct {
file string
local string
}

var cfg = &config{}

var rootCmd = &cobra.Command{
Use: "nctl",
Short: "nctl is a tool for managing operator resources",
RunE: func(cmd *cobra.Command, args []string) error {
return runRoot(cmd.Context())
},
}

func init() {
rootCmd.Flags().StringP("file", "f", cfg.file, "Procfile to run.")
rootCmd.Flags().StringP("local", "l", cfg.local, "Local Procfile to append.")
}

func runRoot(ctx context.Context) error {
data, err := os.ReadFile(cfg.file)
if err != nil {
log.Fatal(err)
}

envData, err := os.ReadFile(cfg.local)
if err != nil {
return err
}

buf := bytes.NewBuffer(data)
buf.WriteString("\n")
buf.Write(envData)

tasks, err := Parse(buf)
if err != nil {
log.Fatalln(err)
}
log.SetFlags(log.Lshortfile)

run := NewRunner(tasks)

err = run.Run(ctx)
if err != nil {
return err
}

return nil
}

func main() {
log.SetFlags(0)
log.SetOutput(os.Stderr)

if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 13 additions & 25 deletions devx/runproc/runner.go → cmd/runproc/runner.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

import (
"fmt"
"context"
"log"
"strings"
"sync"
"time"

"github.com/fatih/color"
"golang.org/x/sync/errgroup"
)

type Runner struct {
Expand All @@ -16,8 +17,6 @@ type Runner struct {
doneCh chan struct{}
stopCh chan struct{}

failed bool

stopOnce sync.Once
}

Expand Down Expand Up @@ -53,40 +52,29 @@ func NewRunner(tasks []Task) *Runner {
}
}

func (r *Runner) Run() error {
defer close(r.doneCh)
func (r *Runner) Run(ctx context.Context) error {
g, _ := errgroup.WithContext(ctx)
result := make(chan bool, len(r.procs))

for _, proc := range r.procs {
proc.Start()
go func(p ProcessRunner) {
result <- p.Wait()
}(proc)
g.Go(func() error {
proc.Start()
result <- proc.Wait()
return nil
})
}

var err error
for range r.procs {
if <-result {
go r.stopOnce.Do(r._stop)
} else {
go r.stopOnce.Do(r._stopFail)
}
if err := g.Wait(); err != nil {
return err
}

if r.failed {
return fmt.Errorf("one or more commands failed or quit early")
}
return err
return nil
}

func (r *Runner) Stop() {
r.stopOnce.Do(r._stop)
}

func (r *Runner) _stopFail() {
r.failed = true
r._stop()
}

func (r *Runner) _stop() {
close(r.stopCh)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
114 changes: 114 additions & 0 deletions cmd/waitfor/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package main

import (
"context"
"fmt"
"log"
"net/url"
"os"
"time"

"github.com/zeiss/pkg/cmd/waitfor/schemes"

_ "github.com/jackc/pgx/v5/stdlib"
"github.com/spf13/cobra"
)

var (
version = "dev"
commit = "none"
date = "unknown"
)

var build = fmt.Sprintf("%s (%s) (%s)", version, commit, date)

type config struct {
timeout time.Duration
connTimeout time.Duration
retryTime time.Duration
}

var cfg = &config{}

var rootCmd = &cobra.Command{
Use: "wait-for",
Short: `wait-for waits for other service to become available.`,
RunE: func(cmd *cobra.Command, args []string) error {
return runRoot(cmd.Context(), args...)
},
Version: build,
}

func init() {
register(schemes.HTTP(), "http", "https")
register(schemes.TCP(), "tcp")
register(schemes.Postgres(), "postgres", "postgresql")

rootCmd.Flags().DurationVar(&cfg.timeout, "timeout", time.Minute, "Timeout to wait for all checks to complete.")
rootCmd.Flags().DurationVar(&cfg.connTimeout, "connect-timeout", 5*time.Second, "Timeout to wait for a single check to complete.")
rootCmd.Flags().DurationVar(&cfg.retryTime, "retry-time", 3*time.Second, "Time to wait between retries.")
}

var (
waitFuncs = map[string]schemes.WaitFunc{}
)

func register(fn schemes.WaitFunc, schema ...string) {
for _, s := range schema {
waitFuncs[s] = fn
}
}

func waitFor(ctx context.Context, urlStr string) error {
u, err := url.Parse(urlStr)
if err != nil {
return fmt.Errorf("url parse '%s': %w", urlStr, err)
}

t := time.NewTicker(cfg.retryTime)
defer t.Stop()

for {
fn, ok := waitFuncs[u.Scheme]
if !ok {
return fmt.Errorf("unsupported schema %q", u.Scheme)
}

ct, cancel := context.WithTimeout(ctx, cfg.connTimeout)
err = fn(ct, urlStr)
cancel()
if err == nil {
return nil
}

log.Println("Waiting for", urlStr, err)
select {
case <-ctx.Done():
return fmt.Errorf("timeout waiting for %s", urlStr)
case <-t.C:
}
}
}

func runRoot(ctx context.Context, args ...string) error {
ctx, cancel := context.WithTimeout(ctx, cfg.timeout)
defer cancel()

for _, urlStr := range args {
err := waitFor(ctx, urlStr)
if err != nil {
return err
}
}

return nil
}

func main() {
log.SetFlags(0)
log.SetOutput(os.Stderr)

if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
Loading

0 comments on commit e553f9a

Please sign in to comment.