From 4854801dd7fb147d39eba9c9361d1f9d73518875 Mon Sep 17 00:00:00 2001 From: fujiwara Date: Tue, 21 May 2024 10:36:07 +0900 Subject: [PATCH] build by env AWS_SDK_CLIENT_GO_GEN no need to gen.yaml for simple usage. --- README.md | 22 ++++++++++++++++++++-- cmd/aws-sdk-client-gen-gen/main.go | 28 ++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9db8e51..1615856 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,24 @@ $ 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 @@ -36,12 +51,14 @@ services: 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 @@ -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 diff --git a/cmd/aws-sdk-client-gen-gen/main.go b/cmd/aws-sdk-client-gen-gen/main.go index 70f7dbd..5b52d49 100644 --- a/cmd/aws-sdk-client-gen-gen/main.go +++ b/cmd/aws-sdk-client-gen-gen/main.go @@ -4,6 +4,7 @@ import ( "bytes" "log" "os" + "strings" "text/template" "github.com/goccy/go-yaml" @@ -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)