Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build by env AWS_SDK_CLIENT_GO_GEN #9

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading