Skip to content

Commit

Permalink
Merge pull request #9 from fujiwara/build-by-env
Browse files Browse the repository at this point in the history
build by env AWS_SDK_CLIENT_GO_GEN
  • Loading branch information
fujiwara authored May 21, 2024
2 parents 4bbdf36 + 4854801 commit 2ec9457
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,39 @@ $ brew install fujiwara/tap/aws-sdk-client-go

You can build the client yourself, including only the needed services and methods. The optimized binary is small and boots up quickly.

The client is built by a configuration file `gen.yaml`.
The client is built by a configuration file `gen.yaml` or `AWS_SDK_CLIENT_GO_GEN` environment variable.

### `AWS_SDK_CLIENT_GO_GEN` environment variable

Set the environment variable `AWS_SDK_CLIENT_GO_GEN` to list the services joined by commas.

For example, to build the client for ECS, Firehose, and S3:

```console
$ export AWS_SDK_CLIENT_GO_GEN="ecs,firehose,s3"
```

All methods of the specified services are generated. To build only specified methods, use the `gen.yaml` configuration file.

### `gen.yaml` configuration file

```yaml
# gen.yaml
services:
ecs:
- DescribeClusters
- DescribeTasks
firehose:
- DescribeDeliveryStream
- ListDeliveryStreams
kinesis:
s3:
# all methods of the service
```

Keys of `services` are AWS service names (`github.com/aws/aws-sdk-go-v2/service/*`), and values are method names of the service client (for example, `s3` is [s3.Client](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/s3#Client)). If you don't specify the method names, all methods of the service client are generated.

### Build the client

To build the client, run the following commands (or simply run `make`):

```console
Expand All @@ -52,6 +69,7 @@ $ go build -o your-client ./cmd/aws-sdk-client-go/main.go
1. `go generate ./cmd/aws-sdk-client-gen .` generates the generator by `gen.yaml`.
2. `go build -o your-client ./cmd/aws-sdk-client-go/main.go` builds your client.

If you change the configuration, run `make clean` before `make` to purge the generated files.

## Performance comparison

Expand Down
28 changes: 20 additions & 8 deletions cmd/aws-sdk-client-gen-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"log"
"os"
"strings"
"text/template"

"github.com/goccy/go-yaml"
Expand Down Expand Up @@ -41,16 +42,27 @@ type GenerateConfig struct {
func main() {
log.Println("generating gen.go")

f, err := os.Open("../../gen.yaml")
if err != nil {
log.Fatalf("failed to open gen.yaml: %v", err)
}
defer f.Close()

cfg := GenerateConfig{}
if err := yaml.NewDecoder(f).Decode(&cfg); err != nil {
log.Fatalf("failed to decode gen.yaml: %v", err)
e := os.Getenv("AWS_SDK_CLIENT_GO_GEN")
if e != "" {
log.Printf("AWS_SDK_CLIENT_GO_GEN is set, generating services: %s", e)
services := strings.Split(e, ",")
cfg.Services = make(map[string][]string, len(services))
for _, service := range services {
cfg.Services[service] = nil
}
} else {
log.Printf("AWS_SDK_CLIENT_GO_GEN is not set, reading gen.yaml")
f, err := os.Open("../../gen.yaml")
if err != nil {
log.Fatalf("failed to open gen.yaml: %v", err)
}
defer f.Close()
if err := yaml.NewDecoder(f).Decode(&cfg); err != nil {
log.Fatalf("failed to decode gen.yaml: %v", err)
}
}

tmpl, err := template.New("gen").Parse(templateStr)
if err != nil {
log.Fatalf("failed to parse template: %v", err)
Expand Down

0 comments on commit 2ec9457

Please sign in to comment.