From 8e3a05187faa853db6ecef1eff2cc9dd50562c86 Mon Sep 17 00:00:00 2001 From: fujiwara Date: Mon, 20 May 2024 11:40:03 +0900 Subject: [PATCH] add -i (--input-stream) flag to bind io.Reader in Input struct. --- cmd/aws-sdk-client-gen/main.go | 12 ++++++-- gen.yaml | 13 +++++++++ go.mod | 1 - go.sum | 6 ---- main.go | 50 +++++++++++++++++++++++++++------- bind.go => param.go | 7 ++--- 6 files changed, 66 insertions(+), 23 deletions(-) rename bind.go => param.go (57%) diff --git a/cmd/aws-sdk-client-gen/main.go b/cmd/aws-sdk-client-gen/main.go index b8037df..95f1fd1 100644 --- a/cmd/aws-sdk-client-gen/main.go +++ b/cmd/aws-sdk-client-gen/main.go @@ -31,6 +31,11 @@ func {{ $.PkgName }}_{{ .Name }}(ctx context.Context, p *clientMethodParam) (any if err := json.Unmarshal(p.b, &in); err != nil { return nil, fmt.Errorf("failed to unmarshal request: %w", err) } + {{- if .InputReaderKey }} + if p.inputReader != nil { + in.{{ .InputReaderKey }} = p.inputReader + } + {{- end }} return svc.{{ .Name }}(ctx, &in) } @@ -65,15 +70,18 @@ func gen(pkgName string, clientType reflect.Type, genNames []string) error { continue } inputParam := method.Type.In(2) + var inputReaderKey string for j := 0; j < inputParam.Elem().NumField(); j++ { field := inputParam.Elem().Field(j) if t := field.Type.String(); t == "io.Reader" { log.Printf("found %s field in %s.%sInput %s %s", t, pkgName, method.Name, field.Name, t) + inputReaderKey = field.Name } } methods = append(methods, map[string]string{ - "Name": method.Name, - "Input": strings.TrimPrefix(params[2], "*"), + "Name": method.Name, + "Input": strings.TrimPrefix(params[2], "*"), + "InputReaderKey": inputReaderKey, }) /* output := method.Type.Out(0) diff --git a/gen.yaml b/gen.yaml index d010461..117714c 100644 --- a/gen.yaml +++ b/gen.yaml @@ -7,3 +7,16 @@ services: - ListDeliveryStreams kinesis: # all methods of the service + s3: + - GetBucketLocation + - GetObject + - ListBuckets + - ListObjects + - ListObjectsV2 + - PutObject + - PutObjectAcl + - PutObjectTagging + - RestoreObject + - SelectObjectContent + - UploadPart + - UploadPartCopy diff --git a/go.mod b/go.mod index c069334..3fec4c9 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.21.0 require ( github.com/alecthomas/kong v0.9.0 - github.com/aws/aws-sdk-go v1.53.3 github.com/aws/aws-sdk-go-v2 v1.27.0 github.com/aws/aws-sdk-go-v2/config v1.27.14 github.com/goccy/go-yaml v1.11.3 diff --git a/go.sum b/go.sum index 047159a..7231b27 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,6 @@ github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os= github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc= github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= -github.com/aws/aws-sdk-go v1.53.3 h1:xv0iGCCLdf6ZtlLPMCBjm+tU9UBLP5hXnSqnbKFYmto= -github.com/aws/aws-sdk-go v1.53.3/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= github.com/aws/aws-sdk-go-v2 v1.27.0 h1:7bZWKoXhzI+mMR/HjdMx8ZCC5+6fY0lS5tr0bbgiLlo= github.com/aws/aws-sdk-go-v2 v1.27.0/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM= github.com/aws/aws-sdk-go-v2/config v1.27.14 h1:QOg8Ud53rrmdjBHX080AaYUBhG2ER28kP/yjE7afF/0= @@ -63,14 +61,10 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= -golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= -golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= -golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/main.go b/main.go index 6ad845c..1c96662 100644 --- a/main.go +++ b/main.go @@ -25,9 +25,11 @@ type CLI struct { Service string `arg:"" help:"service name" default:""` Method string `arg:"" help:"method name" default:""` Input string `arg:"" help:"input JSON" default:"{}"` - Compact bool `short:"c" help:"compact JSON output"` - Query string `short:"q" help:"JMESPath query to apply to output"` - Version bool `short:"v" help:"show version"` + + InputStream string `short:"i" help:"bind input filename or '-' to io.Reader field in the input struct"` + Compact bool `short:"c" help:"compact JSON output"` + Query string `short:"q" help:"JMESPath query to apply to output"` + Version bool `short:"v" help:"show version"` w io.Writer } @@ -64,20 +66,15 @@ func (c *CLI) CallMethod(ctx context.Context) error { fmt.Fprintf(c.w, "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 function %s", key) } - - awsCfg, err := config.LoadDefaultConfig(ctx) + p, err := c.clientMethodParam(ctx) if err != nil { return err } - p := &clientMethodParam{ - awsCfg: awsCfg, - b: json.RawMessage(c.Input), - } + out, err := fn(ctx, p) if err != nil { return err @@ -105,6 +102,39 @@ func (c *CLI) CallMethod(ctx context.Context) error { return nil } +func (c *CLI) clientMethodParam(ctx context.Context) (*clientMethodParam, error) { + awsCfg, err := config.LoadDefaultConfig(ctx) + if err != nil { + return nil, err + } + p := &clientMethodParam{ + awsCfg: awsCfg, + b: json.RawMessage(c.Input), + inputReader: nil, + } + + switch c.InputStream { + case "": + // do nothing + case "-": // stdin + buf := &bytes.Buffer{} + if _, err := io.Copy(buf, os.Stdin); err != nil { + if err != io.EOF { + return nil, fmt.Errorf("failed to read from stdin: %w", err) + } + } + p.inputReader = buf + default: + f, err := os.Open(c.InputStream) + if err != nil { + return nil, fmt.Errorf("failed to open input file: %w", err) + } + defer f.Close() + p.inputReader = f + } + return p, nil +} + func (c *CLI) ListMethods(_ context.Context) error { methods := make([]string, 0) for name := range clientMethods { diff --git a/bind.go b/param.go similarity index 57% rename from bind.go rename to param.go index f771bc0..a6e791e 100644 --- a/bind.go +++ b/param.go @@ -8,8 +8,7 @@ import ( ) type clientMethodParam struct { - awsCfg aws.Config - b json.RawMessage - bindKey string - bindReader io.Reader + awsCfg aws.Config + b json.RawMessage + inputReader io.Reader }