Skip to content

Commit

Permalink
feat: integrate kong to support flags/envs and files in the future
Browse files Browse the repository at this point in the history
  • Loading branch information
petomalina committed Dec 21, 2023
1 parent c906f14 commit 8ed98b9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
40 changes: 17 additions & 23 deletions cmd/pot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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=<name> is required, but missing")
os.Exit(1)
}

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $ go install github.com/petomalina/pot/cmd/pot@latest
Pot requires only a single flag to run:

```bash
$ pot -bucket <bucket-name>
$ pot -b <bucket-name>
```

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.
Expand Down Expand Up @@ -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 <bucket-name> -zip <zip-path>
$ pot -b <bucket-name> --zip <zip-path>

# e.g. if you want to store the data in the bucket root in a file called bundle.zip:
$ pot -bucket <bucket-name> -zip .
$ pot -b <bucket-name> --zip .

# or if you want to store it in a subdirectory called bundle:
$ pot -bucket <bucket-name> -zip ./bundle
$ pot -b <bucket-name> --zip ./bundle
```

## Advanced Features - Using Pot as a Go Library
Expand Down

0 comments on commit 8ed98b9

Please sign in to comment.