Skip to content

Commit

Permalink
add help
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed May 9, 2024
1 parent 264ac82 commit b140fc3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 38 deletions.
8 changes: 1 addition & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: clean test
.PHONY: clean test gen

aws-sdk-client-go: go.* *.go gen
go build -o $@ cmd/aws-sdk-client-go/main.go
Expand All @@ -13,9 +13,3 @@ gen:

test:
go test -v ./...

install:
go install github.com/fujiwara/aws-sdk-client-go/cmd/aws-sdk-client-go

dist:
goreleaser build --snapshot --rm-dist
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# aws-sdk-client-go

aws-sdk-client-go is a Go client for AWS SDK services.
aws-sdk-client-go is a Go client CLI for AWS SDK services.

This client is auto generated from the AWS SDK Go service client.
This CLI is auto generated from the AWS SDK Go v2 service client.

## Motivation

Expand All @@ -28,7 +28,7 @@ services:

Keys of `services` are AWS service names (`github.com/aws/aws-sdk-go-v2/service/*`), and values are method names of the service client. If you don't specify the method names, all methods of the service client are generated.

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

```console
$ go generate ./cmd/aws-sdk-client-gen .
Expand Down Expand Up @@ -72,6 +72,14 @@ $ aws-sdk-client-go ecs DescribeClusters '{"Cluster":"default"}'

The third argument is JSON input for the method. If the method does not require input, you can omit the third argument (implicitly `{}` passed).

#### Show help

aws-sdk-client-go is a simple wrapper of the AWS SDK Go v2 service client. Its usage is the same as that of the AWS SDK Go v2.

```console
$ aws-sdk-client-go ecs DescribeClusters help
See https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/ecs#Client.DescribeClusters
```

## LICENSE

Expand Down
22 changes: 7 additions & 15 deletions cmd/aws-sdk-client-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func gen(pkgName string, clientType reflect.Type, genNames []string) error {
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/%s"
)
`, pkgName)
Expand All @@ -44,40 +46,30 @@ func gen(pkgName string, clientType reflect.Type, genNames []string) error {
}
methodNames = append(methodNames, method.Name)
log.Printf("generating %s_%s", pkgName, method.Name)
fmt.Fprintf(buf, `func %s_%s(ctx context.Context, b json.RawMessage) (any, error) {
fmt.Fprintf(buf, `func %s_%s(ctx context.Context, awsCfg aws.Config, b json.RawMessage) (any, error) {
svc := %s.NewFromConfig(awsCfg)
var in %s
if err := json.Unmarshal(b, &in); err != nil {
return nil, fmt.Errorf("failed to unmarshal request: %%w", err)
}
if out, err := svc.%s(ctx, &in); err != nil {
return nil, fmt.Errorf("failed to call %s: %%w", err)
} else {
return out, nil
}
return svc.%s(ctx, &in)
}
`,
pkgName,
method.Name,
pkgName,
strings.TrimPrefix(params[2], "*"), // (receiver, context.Context, *Request, ...)
method.Name,
method.Name,
)
fmt.Fprintln(buf)
}
fmt.Fprintln(buf, `func init() {`)
for _, name := range methodNames {
fmt.Fprintf(buf, `clientMethods["%s_%s"] = %s_%s
`, pkgName, name, pkgName, name)
fmt.Fprintf(buf, " clientMethods[\"%s#Client.%s\"] = %s_%s\n", pkgName, name, pkgName, name)
}
fmt.Fprintln(buf, "}")
f, err := os.OpenFile(pkgName+"_gen.go", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString(buf.String()); err != nil {

if err := os.WriteFile(pkgName+"_gen.go", []byte(buf.String()), 0644); err != nil {
return err
}
log.Printf("generated %s_gen.go", pkgName)
Expand Down
42 changes: 29 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ import (
"github.com/aws/aws-sdk-go-v2/config"
)

var awsCfg aws.Config

var clientMethods = make(map[string]func(context.Context, json.RawMessage) (any, error))
var clientMethods = make(map[string]func(context.Context, aws.Config, json.RawMessage) (any, error))

func Run(ctx context.Context) error {
var err error
awsCfg, err = config.LoadDefaultConfig(ctx)
awsCfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return err
}
Expand All @@ -33,23 +31,28 @@ func Run(ctx context.Context) error {
pkgName := os.Args[1]
methodName := os.Args[2]
input := json.RawMessage(`{}`)
return dispatchMethod(ctx, pkgName, methodName, input)
return dispatchMethod(ctx, awsCfg, pkgName, methodName, input)
case 4:
pkgName := os.Args[1]
methodName := os.Args[2]
input := json.RawMessage(os.Args[3])
return dispatchMethod(ctx, pkgName, methodName, input)
return dispatchMethod(ctx, awsCfg, pkgName, methodName, input)
default:
return fmt.Errorf("too many args")
}
}

func dispatchMethod(ctx context.Context, pkgName, methodName string, in json.RawMessage) error {
fn := clientMethods[pkgName+"_"+methodName]
func dispatchMethod(ctx context.Context, awsCfg aws.Config, pkgName, methodName string, in json.RawMessage) error {
key := buildKey(pkgName, methodName)
if bytes.Equal(in, []byte(`help`)) {
fmt.Printf("See https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/%s\n", key)
return nil
}
fn := clientMethods[key]
if fn == nil {
return fmt.Errorf("unknown method %s of %s", methodName, pkgName)
return fmt.Errorf("unknown function %s", key)
}
out, err := fn(ctx, in)
out, err := fn(ctx, awsCfg, in)
if err != nil {
return err
}
Expand All @@ -67,8 +70,9 @@ func dispatchMethod(ctx context.Context, pkgName, methodName string, in json.Raw
func listMethods(pkgName string) error {
methods := make([]string, 0)
for name := range clientMethods {
if strings.HasPrefix(name, pkgName+"_") {
methods = append(methods, strings.TrimPrefix(name, pkgName+"_"))
service, method := parseKey(name)
if service == pkgName {
methods = append(methods, method)
}
}
sort.Strings(methods)
Expand All @@ -81,7 +85,8 @@ func listMethods(pkgName string) error {
func listServices() error {
services := make(map[string]struct{})
for name := range clientMethods {
services[strings.Split(name, "_")[0]] = struct{}{}
service, _ := parseKey(name)
services[service] = struct{}{}
}
names := make([]string, 0)
for name := range services {
Expand All @@ -94,4 +99,15 @@ func listServices() error {
return nil
}

func parseKey(key string) (string, string) {
parts := strings.Split(key, "#")
service := parts[0]
method := strings.SplitN(parts[1], ".", 2)[1]
return service, method
}

func buildKey(service, method string) string {
return fmt.Sprintf("%s#Client.%s", service, method)
}

//go:generate go run cmd/aws-sdk-client-gen/main.go cmd/aws-sdk-client-gen/gen.go

0 comments on commit b140fc3

Please sign in to comment.