generated from ZEISS/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/go_modules/tools/github.com/golan…
…gci/golangci-lint-1.64.5
- Loading branch information
Showing
25 changed files
with
325 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.