Skip to content

Commit

Permalink
add -i (--input-stream) flag to bind io.Reader in Input struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed May 20, 2024
1 parent afbbdd7 commit 8e3a051
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 23 deletions.
12 changes: 10 additions & 2 deletions cmd/aws-sdk-client-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
50 changes: 40 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions bind.go → param.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 8e3a051

Please sign in to comment.