Skip to content

Commit a51c34e

Browse files
committed
fix: resource command group
resource command group was broken in a previous refactor.
1 parent e666a30 commit a51c34e

5 files changed

Lines changed: 46 additions & 18 deletions

File tree

internal/service/resource_definition.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type ListMethod struct {
4040
type DeleteMethod struct {
4141
}
4242

43-
func (r *Resource) ExecuteCommand(args []string) (*http.Request, error) {
43+
func (r *Resource) ExecuteCommand(args []string) (*http.Request, error, string) {
4444
c := cobra.Command{Use: r.Singular}
4545
var err error
4646
var req *http.Request
@@ -157,12 +157,15 @@ func (r *Resource) ExecuteCommand(args []string) (*http.Request, error) {
157157
}
158158
c.AddCommand(listCmd)
159159
}
160-
160+
var stderr strings.Builder
161+
var stdout strings.Builder
162+
c.SetOut(&stdout)
163+
c.SetErr(&stderr)
161164
c.SetArgs(args)
162165
if err := c.Execute(); err != nil {
163-
return nil, err
166+
return nil, err, stdout.String() + stderr.String()
164167
}
165-
return req, err
168+
return req, err, stdout.String() + stderr.String()
166169
}
167170

168171
func addSchemaFlags(c *cobra.Command, schema openapi.Schema, args map[string]interface{}) error {

internal/service/resource_definition_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestExecuteCommand(t *testing.T) {
109109

110110
for _, tt := range tests {
111111
t.Run(tt.name, func(t *testing.T) {
112-
req, err := tt.resource.ExecuteCommand(tt.args)
112+
req, err, _ := tt.resource.ExecuteCommand(tt.args)
113113
if (err != nil) != tt.wantErr {
114114
t.Errorf("ExecuteCommand() error = %v, wantErr %v", err, tt.wantErr)
115115
return

internal/service/service.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,23 @@ func (s *Service) ExecuteCommand(args []string) (string, error) {
3535
if err != nil {
3636
return "", fmt.Errorf("%v\n%v", err, s.PrintHelp())
3737
}
38-
req, err := r.ExecuteCommand(args)
38+
req, err, output := r.ExecuteCommand(args[1:])
3939
if err != nil {
4040
return "", fmt.Errorf("unable to execute command: %v", err)
4141
}
4242
if req == nil {
43-
return "", nil
43+
return output, nil
4444
}
4545
url, err := url.Parse(fmt.Sprintf("%s/%s", s.ServerURL, req.URL.String()))
4646
if err != nil {
4747
return "", fmt.Errorf("unable to create url: %v", err)
4848
}
4949
req.URL = url
50-
return s.doRequest(req)
50+
reqOutput, err := s.doRequest(req)
51+
if err != nil {
52+
return "", fmt.Errorf("unable to execute request: %v", err)
53+
}
54+
return strings.Join([]string{output, reqOutput}, "\n"), nil
5155
}
5256

5357
func (s *Service) doRequest(r *http.Request) (string, error) {

internal/service/service_definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func GetServiceDefinition(api *openapi.OpenAPI, serverURL, pathPrefix string) (*
131131
func (s *ServiceDefinition) GetResource(resource string) (*Resource, error) {
132132
r, ok := (*s).Resources[resource]
133133
if !ok {
134-
return nil, fmt.Errorf("Resource %s not found", resource)
134+
return nil, fmt.Errorf("Resource %q not found", resource)
135135
}
136136
return r, nil
137137
}

internal/service/service_test.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,58 @@ func TestService_ExecuteCommand_ListResources(t *testing.T) {
1010
svc := NewService(ServiceDefinition{
1111
ServerURL: "http://test.com",
1212
Resources: map[string]*Resource{
13+
"project": &projectResource,
1314
"user": {},
1415
"post": {},
1516
"comment": {},
1617
},
1718
}, nil)
1819

1920
tests := []struct {
20-
name string
21-
args []string
22-
expected string
21+
name string
22+
args []string
23+
expectAsError bool
24+
expected string
2325
}{
2426
{
2527
name: "no arguments",
2628
args: []string{},
27-
expected: "Available resources:\n - comment\n - post\n - user\n",
29+
expected: "Available resources:\n - comment\n - post\n - project\n - user\n",
2830
},
2931
{
3032
name: "help flag",
3133
args: []string{"--help"},
32-
expected: "Available resources:\n - comment\n - post\n - user\n",
34+
expected: "Available resources:\n - comment\n - post\n - project\n - user\n",
35+
},
36+
{
37+
name: "unknown resource",
38+
args: []string{"users"},
39+
expectAsError: true,
40+
expected: "Resource \"users\" not found",
41+
},
42+
{
43+
name: "help for project",
44+
args: []string{"project", "--help"},
45+
expected: "Available Commands:",
46+
},
47+
{
48+
name: "help for project",
49+
args: []string{"project"},
50+
expected: "Available Commands:",
3351
},
3452
}
3553

3654
for _, tt := range tests {
3755
t.Run(tt.name, func(t *testing.T) {
3856
result, err := svc.ExecuteCommand(tt.args)
3957
if err != nil {
40-
t.Errorf("ExecuteCommand() error = %v, expected no error", err)
41-
}
42-
if !strings.Contains(result, tt.expected) {
43-
t.Errorf("ExecuteCommand() = %v, expected it to contain %v", result, tt.expected)
58+
if !tt.expectAsError {
59+
t.Errorf("ExecuteCommand() error = %v, expected no error", err)
60+
} else if !strings.Contains(err.Error(), tt.expected) {
61+
t.Errorf("ExecuteCommand() error = %v, expected it to contain %v", err, tt.expected)
62+
}
63+
} else if !strings.Contains(result, tt.expected) {
64+
t.Errorf("ExecuteCommand() = %q, expected it to contain %q", result, tt.expected)
4465
}
4566
})
4667
}

0 commit comments

Comments
 (0)