From 8ed98b9f8154d8557d337f8f97e94b91eeacb201 Mon Sep 17 00:00:00 2001 From: Peter Malina Date: Thu, 21 Dec 2023 18:45:27 +0100 Subject: [PATCH] feat: integrate kong to support flags/envs and files in the future --- .air.toml | 2 +- cmd/pot/main.go | 40 +++++++++++++++++----------------------- go.mod | 1 + go.sum | 2 ++ readme.md | 8 ++++---- 5 files changed, 25 insertions(+), 28 deletions(-) diff --git a/.air.toml b/.air.toml index 080ef72..1a3bdf7 100644 --- a/.air.toml +++ b/.air.toml @@ -4,7 +4,7 @@ tmp_dir = "tmp" [build] args_bin = [] - bin = "./tmp/pot -bucket petomalina-pot-tests -distributed-lock -metrics -zip bundle" + bin = "./tmp/pot -b petomalina-pot-tests --distributed-lock --metrics --zip bundle" cmd = "go build -o ./tmp/pot ./cmd/pot" delay = 0 exclude_dir = ["docs", "proto", "ui", "assets", "tmp", "vendor", "testdata"] diff --git a/cmd/pot/main.go b/cmd/pot/main.go index f1632e3..d801d3a 100644 --- a/cmd/pot/main.go +++ b/cmd/pot/main.go @@ -2,31 +2,30 @@ package main import ( "context" - "flag" "log/slog" "net/http" "os" "os/signal" "time" + "github.com/alecthomas/kong" "github.com/petomalina/pot" ) -var ( - logLevelFlag = flag.String("log-level", "info", "debug | info | warn | error") - bucketNameFlag = flag.String("bucket", "", "bucket name") - zipFlag = flag.String("zip", "", "zip is the path where the zip file is stored") - distributedLockFlag = flag.Bool("distributed-lock", false, "distributed-lock enables distributed locking of the pot") - - tracing = flag.Bool("tracing", false, "tracing enables tracing") - metrics = flag.Bool("metrics", false, "metrics enables metrics") -) +var cli struct { + LogLevel string `help:"debug | info | warn | error" env:"LOG_LEVEL" default:"info"` + Bucket string `help:"bucket name" env:"BUCKET" required:"true" short:"b"` + Zip string `help:"zip is the path where the zip file is stored" env:"ZIP"` + DistributedLock bool `help:"distributed-lock enables distributed locking of the pot" env:"DISTRIBUTED_LOCK"` + Tracing bool `help:"tracing enables tracing" env:"TRACING"` + Metrics bool `help:"metrics enables metrics" env:"METRICS"` +} func main() { - flag.Parse() + _ = kong.Parse(&cli) loglevel := new(slog.Level) - err := loglevel.UnmarshalText([]byte(*logLevelFlag)) + err := loglevel.UnmarshalText([]byte(cli.LogLevel)) if err != nil { slog.Error("failed to parse log level: %v", err) os.Exit(1) @@ -35,11 +34,6 @@ func main() { h := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: *loglevel}) slog.SetDefault(slog.New(h)) - if *bucketNameFlag == "" { - slog.Error("-bucket= is required, but missing") - os.Exit(1) - } - ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) defer cancel() @@ -56,27 +50,27 @@ func main() { }() opts := []pot.Option{} - if *distributedLockFlag { + if cli.DistributedLock { slog.Debug("distributed lock enabled") opts = append(opts, pot.WithDistributedLock()) } - if *zipFlag != "" { + if cli.Zip != "" { slog.Info("zip file enabled") - opts = append(opts, pot.WithZip(*zipFlag)) + opts = append(opts, pot.WithZip(cli.Zip)) } - if *metrics { + if cli.Metrics { slog.Info("metrics enabled") opts = append(opts, pot.WithMetrics()) } - if *tracing { + if cli.Tracing { slog.Info("tracing enabled") opts = append(opts, pot.WithTracing()) } - server, err := pot.NewServer(ctx, *bucketNameFlag, opts...) + server, err := pot.NewServer(ctx, cli.Bucket, opts...) if err != nil { slog.Error("failed to create pot client: %v", err) os.Exit(1) diff --git a/go.mod b/go.mod index d4e4d11..d3c8bb4 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( ) require ( + github.com/alecthomas/kong v0.8.1 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect diff --git a/go.sum b/go.sum index 7464beb..5f5a01b 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ cloud.google.com/go/iam v1.1.1/go.mod h1:A5avdyVL2tCppe4unb0951eI9jreack+RJ0/d+K cloud.google.com/go/storage v1.33.0 h1:PVrDOkIC8qQVa1P3SXGpQvfuJhN2LHOoyZvWs8D2X5M= cloud.google.com/go/storage v1.33.0/go.mod h1:Hhh/dogNRGca7IWv1RC2YqEn0c0G77ctA/OxflYkiD8= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/alecthomas/kong v0.8.1 h1:acZdn3m4lLRobeh3Zi2S2EpnXTd1mOL6U7xVml+vfkY= +github.com/alecthomas/kong v0.8.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= diff --git a/readme.md b/readme.md index 964f260..fffd6e1 100644 --- a/readme.md +++ b/readme.md @@ -14,7 +14,7 @@ $ go install github.com/petomalina/pot/cmd/pot@latest Pot requires only a single flag to run: ```bash -$ pot -bucket +$ pot -b ``` Pot runs by default on port `8080` and doesn't respect any other opinions on port selection. It is intended to be run in a serverless environment or an environment that supports port forwarding. @@ -75,13 +75,13 @@ $ curl -X DELETE localhost:8080/users?key=John%20Doe Certain tools like [Open Policy Agent require the data to be zipped](https://www.openpolicyagent.org/docs/latest/management-bundles/#bundle-build) before they can be shipped. Pot supports zipping the content by setting the `zip` flag and providing the path to the zip file in it: ```bash -$ pot -bucket -zip +$ pot -b --zip # e.g. if you want to store the data in the bucket root in a file called bundle.zip: -$ pot -bucket -zip . +$ pot -b --zip . # or if you want to store it in a subdirectory called bundle: -$ pot -bucket -zip ./bundle +$ pot -b --zip ./bundle ``` ## Advanced Features - Using Pot as a Go Library