Skip to content

Commit 5982f22

Browse files
committed
[names] use x-cli-name
1 parent 91711b6 commit 5982f22

File tree

4 files changed

+22
-102
lines changed

4 files changed

+22
-102
lines changed

README.md

Lines changed: 7 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -25,106 +25,26 @@ Experimental, in dev flux and looking for design/usage feedback!
2525
go get github.com/lispyclouds/climate
2626
```
2727

28-
### How it works and usage
28+
### Rationale
2929

3030
climate allows the server to influence the CLI behaviour by using OpenAPI's [extensions](https://swagger.io/docs/specification/v3_0/openapi-extensions/). It encourages [spec-first](https://www.atlassian.com/blog/technology/spec-first-api-development) practices thereby keeping both users and maintenance manageable. It does just enough to handle the spec and nothing more.
3131

3232
Overall, the way it works:
3333
- Each operation is converted to a Cobra command
3434
- Each parameter is converted to a flag with its corresponding type
35-
- Request bodies are a flag as of now, subject to change
35+
- Request bodies are a flag as of now, subject to change. Name defaults to `climate-data` unless specified via `x-cli-name`
3636
- The provided handlers are attached to each command, grouped and attached to the rootCmd
3737

3838
Influenced by some of the ideas behind [restish](https://rest.sh/) it uses the following extensions as of now:
39-
- `x-cli-aliases`: A list of strings which would be used as the alternate names for:
40-
- Operations: If set, will prefer the first of the list otherwise the `operationId`. Will use the rest as cobra aliases
41-
- Request Body: Same preference as above but would a default of `climate-data` as the name of the param if not set
39+
- `x-cli-aliases`: A list of strings which would be used as the alternate names for an operation
4240
- `x-cli-group`: A string to allow grouping subcommands together. All operations in the same group would become subcommands in that group name
4341
- `x-cli-hidden`: A boolean to hide the operation from the CLI menu. Same behaviour as a cobra command hide: it's present and expects a handler
4442
- `x-cli-ignored`: A boolean to tell climate to omit the operation completely
43+
- `x-cli-name`: A string to specify a differnt name. Applies to operations and request bodies as of now
4544

46-
Given an OpenAPI spec in `api.yaml`:
47-
48-
```yaml
49-
openapi: "3.0.0"
50-
51-
info:
52-
title: My calculator
53-
version: "0.1.0"
54-
description: My awesome calc!
55-
56-
paths:
57-
"/add/{n1}/{n2}":
58-
get:
59-
operationId: AddGet
60-
summary: Adds two numbers
61-
x-cli-group: ops
62-
x-cli-aliases:
63-
- add-get
64-
- ag
65-
66-
parameters:
67-
- name: n1
68-
required: true
69-
in: path
70-
description: The first number
71-
schema:
72-
type: integer
73-
- name: n2
74-
required: true
75-
in: path
76-
description: The second number
77-
schema:
78-
type: integer
79-
post:
80-
operationId: AddPost
81-
summary: Adds two numbers via POST
82-
x-cli-group: ops
83-
x-cli-aliases:
84-
- add-post
85-
- ap
86-
87-
requestBody:
88-
description: The numbers map
89-
required: true
90-
x-cli-aliases:
91-
- nmap
92-
content:
93-
application/json:
94-
schema:
95-
$ref: "#/components/schemas/NumbersMap"
96-
"/health":
97-
get:
98-
operationId: HealthCheck
99-
summary: Returns Ok if all is well
100-
x-cli-aliases:
101-
- ping
102-
"/meta":
103-
get:
104-
operationId: GetMeta
105-
summary: Returns meta
106-
x-cli-ignored: true
107-
"/info":
108-
get:
109-
operationId: GetInfo
110-
summary: Returns info
111-
x-cli-group: info
112-
113-
components:
114-
schemas:
115-
NumbersMap:
116-
type: object
117-
required:
118-
- n1
119-
- n2
120-
properties:
121-
n1:
122-
type: integer
123-
description: The first number
124-
n2:
125-
type: integer
126-
description: The second number
127-
```
45+
### Usage
46+
47+
Given an OpenAPI spec like [api.yaml](/api.yaml)
12848

12949
Load the spec:
13050

api.yaml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ paths:
1010
get:
1111
operationId: AddGet
1212
summary: Adds two numbers
13+
x-cli-name: add-get
1314
x-cli-group: ops
1415
x-cli-aliases:
15-
- add-get
1616
- ag
1717

1818
parameters:
@@ -31,16 +31,15 @@ paths:
3131
post:
3232
operationId: AddPost
3333
summary: Adds two numbers via POST
34+
x-cli-name: add-post
3435
x-cli-group: ops
3536
x-cli-aliases:
36-
- add-post
3737
- ap
3838

3939
requestBody:
4040
description: The numebers map
4141
required: true
42-
x-cli-aliases:
43-
- nmap
42+
x-cli-name: nmap
4443
content:
4544
application/json:
4645
schema:
@@ -49,8 +48,7 @@ paths:
4948
get:
5049
operationId: HealthCheck
5150
summary: Returns Ok if all is well
52-
x-cli-aliases:
53-
- ping
51+
x-cli-name: ping
5452
"/meta":
5553
get:
5654
operationId: GetMeta

lib.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type extensions struct {
4747
aliases []string
4848
group string
4949
ignored bool
50+
name string
5051
}
5152

5253
func parseExtensions(exts *orderedmap.Map[string, *yaml.Node]) (*extensions, error) {
@@ -71,6 +72,8 @@ func parseExtensions(exts *orderedmap.Map[string, *yaml.Node]) (*extensions, err
7172
ex.group = opts.(string)
7273
case "x-cli-ignored":
7374
ex.ignored = opts.(bool)
75+
case "x-cli-name":
76+
ex.name = opts.(string)
7477
}
7578
}
7679

@@ -138,8 +141,8 @@ func addRequestBody(cmd *cobra.Command, op *v3.Operation, handlerData *HandlerDa
138141
}
139142

140143
paramName := "climate-data"
141-
if aliases := bExts.aliases; len(aliases) > 0 {
142-
paramName = aliases[0]
144+
if altName := bExts.name; altName != "" {
145+
paramName = altName
143146
}
144147

145148
// TODO: Handle all the different MIME types and schemas from body.Content
@@ -210,6 +213,7 @@ func BootstrapV3(rootCmd *cobra.Command, model libopenapi.DocumentModel[v3.Docum
210213
}
211214

212215
cmd.Hidden = exts.hidden
216+
cmd.Aliases = exts.aliases
213217
cmd.Short = op.Description
214218
if op.Summary != "" {
215219
cmd.Short = op.Summary
@@ -219,11 +223,9 @@ func BootstrapV3(rootCmd *cobra.Command, model libopenapi.DocumentModel[v3.Docum
219223
handler(opts, args, hData)
220224
}
221225

222-
// TODO: hammock on better ways to handle aliases, prefers the first one as of now
223226
cmd.Use = op.OperationId // default
224-
if aliases := exts.aliases; len(exts.aliases) > 0 {
225-
cmd.Use = aliases[0]
226-
cmd.Aliases = aliases
227+
if altName := exts.name; altName != "" {
228+
cmd.Use = altName
227229
}
228230

229231
if g := exts.group; g != "" {

lib_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,17 @@ func TestBootstrapV3(t *testing.T) {
8383
"calc/ops/add-get": {
8484
"Use": "add-get",
8585
"Short": "Adds two numbers",
86-
"Aliases": []string{"add-get", "ag"},
86+
"Aliases": []string{"ag"},
8787
},
8888
"calc/ops/add-post": {
8989
"Use": "add-post",
9090
"Short": "Adds two numbers via POST",
91-
"Aliases": []string{"add-post", "ap"},
91+
"Aliases": []string{"ap"},
9292
},
9393
"calc/ping": {
9494
"Use": "ping",
9595
"Short": "Returns Ok if all is well",
96-
"Aliases": []string{"ping"},
96+
"Aliases": noAlias,
9797
},
9898
}
9999

0 commit comments

Comments
 (0)