Skip to content

Commit 43b56a2

Browse files
committed
[housekeeping] refactor file names, better interpolation perf
1 parent 8a80350 commit 43b56a2

File tree

4 files changed

+12
-18
lines changed

4 files changed

+12
-18
lines changed

cobra.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ package climate
1010
import (
1111
"fmt"
1212
"log/slog"
13-
"regexp"
1413
"strconv"
14+
"strings"
1515

1616
"github.com/pb33f/libopenapi"
1717
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
@@ -94,11 +94,6 @@ func interpolatePathCobra(cmd *cobra.Command, h *HandlerData) error {
9494
flags := cmd.Flags()
9595

9696
for _, param := range h.PathParams {
97-
pattern, err := regexp.Compile(fmt.Sprintf("({%s})+", param.Name))
98-
if err != nil {
99-
return err
100-
}
101-
10297
var value string
10398

10499
switch param.Type {
@@ -115,7 +110,7 @@ func interpolatePathCobra(cmd *cobra.Command, h *HandlerData) error {
115110
value = strconv.FormatBool(v)
116111
}
117112

118-
h.Path = pattern.ReplaceAllString(h.Path, value)
113+
h.Path = strings.ReplaceAll(h.Path, "{"+param.Name+"}", value)
119114
}
120115

121116
return nil

cobra_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestInterpolatePath(t *testing.T) {
1111
cmd := cobra.Command{}
1212
hData := HandlerData{
1313
Method: "get",
14-
Path: "/path/{foo}/to/{bar}/with/{baz}/and/{quxx}/together",
14+
Path: "/path/{foo}/to/{bar}/with/{baz}/and/{quxx}/together/{foo}",
1515
PathParams: []ParamMeta{
1616
{Name: "foo", Type: String},
1717
{Name: "bar", Type: Integer},
@@ -28,7 +28,7 @@ func TestInterpolatePath(t *testing.T) {
2828
err := interpolatePathCobra(&cmd, &hData)
2929
assert.NoError(t, err)
3030

31-
assert.Equal(t, hData.Path, "/path/yes/to/420/with/420.69/and/false/together")
31+
assert.Equal(t, hData.Path, "/path/yes/to/420/with/420.69/and/false/together/yes")
3232
}
3333

3434
func assertCmdTree(t *testing.T, cmd *cobra.Command, assertConf map[string]map[string]any, prefix string) {

urfavecli.go renamed to urfavecliv3.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"context"
1212
"fmt"
1313
"log/slog"
14-
"regexp"
1514
"strconv"
15+
"strings"
1616

1717
"github.com/pb33f/libopenapi"
1818
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
@@ -109,11 +109,6 @@ func addRequestBodyUrfaveCliV3(cmd *cli.Command, op *v3.Operation, handlerData *
109109
func interpolatePathUrfaveCliV3(cmd *cli.Command, h *HandlerData) error {
110110
// TODO: Extract commom
111111
for _, param := range h.PathParams {
112-
pattern, err := regexp.Compile(fmt.Sprintf("({%s})+", param.Name))
113-
if err != nil {
114-
return err
115-
}
116-
117112
var value string
118113

119114
switch param.Type {
@@ -127,7 +122,7 @@ func interpolatePathUrfaveCliV3(cmd *cli.Command, h *HandlerData) error {
127122
value = strconv.FormatBool(cmd.Bool(param.Name))
128123
}
129124

130-
h.Path = pattern.ReplaceAllString(h.Path, value)
125+
h.Path = strings.ReplaceAll(h.Path, "{"+param.Name+"}", value)
131126
}
132127

133128
return nil
@@ -179,6 +174,10 @@ func BootstrapV3UrfaveCliV3(rootCmd *cli.Command, model libopenapi.DocumentModel
179174
if altName := exts.name; altName != "" {
180175
cmd.Name = altName
181176
}
177+
cmd.CommandNotFound = func(_ context.Context, cmd *cli.Command, command string) {
178+
slog.Error("Unknown command", "command", command)
179+
cli.ShowSubcommandHelpAndExit(cmd, 1)
180+
}
182181

183182
if g := exts.group; g != "" {
184183
_, ok := cmdGroups[g]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func TestInterpolatePathUrfaveCliV3(t *testing.T) {
1212
hData := HandlerData{
1313
Method: "get",
14-
Path: "/path/{foo}/to/{bar}/with/{baz}/and/{quxx}/together",
14+
Path: "/path/{foo}/to/{bar}/with/{baz}/and/{quxx}/together/{foo}",
1515
PathParams: []ParamMeta{
1616
{Name: "foo", Type: String},
1717
{Name: "bar", Type: Integer},
@@ -47,7 +47,7 @@ func TestInterpolatePathUrfaveCliV3(t *testing.T) {
4747
err := interpolatePathUrfaveCliV3(&cmd, &hData)
4848
assert.NoError(t, err)
4949

50-
assert.Equal(t, hData.Path, "/path/yes/to/420/with/420.69/and/false/together")
50+
assert.Equal(t, hData.Path, "/path/yes/to/420/with/420.69/and/false/together/yes")
5151
}
5252

5353
func assertCmdTreeUrfaveCliV3(t *testing.T, cmd *cli.Command, expected *cli.Command) {

0 commit comments

Comments
 (0)