forked from fujiwara/awslim
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
160 lines (139 loc) · 3.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package sdkclient
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strings"
"github.com/alecthomas/kong"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/jmespath/go-jmespath"
)
var Version = "HEAD"
var clientMethods = make(map[string]ClientMethod)
type ClientMethod func(context.Context, aws.Config, json.RawMessage) (any, error)
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"`
w io.Writer
}
func Run(ctx context.Context) error {
var c CLI
c.w = os.Stdout
kong.Parse(&c)
return c.Dispatch(ctx)
}
func (c *CLI) Dispatch(ctx context.Context) error {
if c.Version {
fmt.Fprintf(c.w, "aws-sdk-client-go %s\n", Version)
return nil
}
if c.Service == "" {
return c.ListServices(ctx)
} else if c.Method == "" {
return c.ListMethods(ctx)
} else {
return c.CallMethod(ctx)
}
}
func (c *CLI) SetWriter(w io.Writer) {
c.w = w
}
func (c *CLI) CallMethod(ctx context.Context) error {
method := kebabToCamel(c.Method)
key := buildKey(c.Service, method)
if c.Input == "help" {
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)
if err != nil {
return err
}
out, err := fn(ctx, awsCfg, json.RawMessage(c.Input))
if err != nil {
return err
}
if c.Query != "" {
out, err = jmespath.Search(c.Query, out)
if err != nil {
return fmt.Errorf("failed to apply JMESPath query: %w", err)
}
}
b, err := json.Marshal(out)
if err != nil {
return fmt.Errorf("failed to marshal response: %w", err)
}
if !c.Compact {
var buf bytes.Buffer
json.Indent(&buf, b, "", " ")
buf.WriteString("\n")
buf.WriteTo(c.w)
} else {
io.WriteString(c.w, string(b))
}
return nil
}
func (c *CLI) ListMethods(_ context.Context) error {
methods := make([]string, 0)
for name := range clientMethods {
service, method := parseKey(name)
if service == c.Service {
methods = append(methods, method)
}
}
sort.Strings(methods)
for _, name := range methods {
fmt.Fprintln(c.w, name)
}
return nil
}
func (c *CLI) ListServices(_ context.Context) error {
services := make(map[string]struct{})
for name := range clientMethods {
service, _ := parseKey(name)
services[service] = struct{}{}
}
names := make([]string, 0)
for name := range services {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
fmt.Fprintln(c.w, name)
}
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)
}
func kebabToCamel(kebab string) string {
parts := strings.Split(kebab, "-")
results := make([]string, 0, len(parts))
for _, p := range parts {
if len(p) == 0 {
continue
}
results = append(results, strings.ToUpper(p[:1])+p[1:])
}
return strings.Join(results, "")
}
//go:generate go run cmd/aws-sdk-client-gen/main.go cmd/aws-sdk-client-gen/gen.go