Skip to content

Commit

Permalink
refactor: using aep-lib-go
Browse files Browse the repository at this point in the history
Common utilities have moved to aep-lib-go.
  • Loading branch information
toumorokoshi committed Nov 10, 2024
1 parent f51f152 commit 4ba1ee8
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 865 deletions.
12 changes: 6 additions & 6 deletions cmd/aepcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"path/filepath"
"strings"

"github.com/aep-dev/aep-lib-go/pkg/api"
"github.com/aep-dev/aep-lib-go/pkg/openapi"
"github.com/aep-dev/aepcli/internal/config"
"github.com/aep-dev/aepcli/internal/openapi"
"github.com/aep-dev/aepcli/internal/service"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -95,21 +96,20 @@ func aepcli(args []string) error {
serverURL = api.ServerURL
}

openapi, err := openapi.FetchOpenAPI(fileAliasOrCore)
oas, err := openapi.FetchOpenAPI(fileAliasOrCore)
if err != nil {
return fmt.Errorf("unable to fetch openapi: %w", err)
}
serviceDefinition, err := service.GetServiceDefinition(openapi, serverURL, pathPrefix)
api, err := api.GetAPI(oas, serverURL, pathPrefix)
if err != nil {
return fmt.Errorf("unable to get service definition: %w", err)
return fmt.Errorf("unable to get api: %w", err)
}

headersMap, err := parseHeaders(headers)
if err != nil {
return fmt.Errorf("unable to parse headers: %w", err)
}

s = service.NewService(*serviceDefinition, headersMap)
s = service.NewService(api, headersMap)

result, err := s.ExecuteCommand(additionalArgs)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.3

require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/aep-dev/aep-lib-go v0.0.0-20241109204312-789b93c37d17 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/aep-dev/aep-lib-go v0.0.0-20241109204312-789b93c37d17 h1:I6JqfEQyyMZ7jMTFv2OrLBOzqCa8dFiu/FSsEx0Bty0=
github.com/aep-dev/aep-lib-go v0.0.0-20241109204312-789b93c37d17/go.mod h1:M+h1D6T2uIUPelmaEsJbjR6JhqKsTlPX3lxp25zQQsk=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
197 changes: 0 additions & 197 deletions internal/openapi/openapi.go

This file was deleted.

46 changes: 3 additions & 43 deletions internal/service/resource_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,12 @@ import (
"net/http"
"strings"

"github.com/aep-dev/aepcli/internal/openapi"
"github.com/aep-dev/aep-lib-go/pkg/api"
"github.com/aep-dev/aep-lib-go/pkg/openapi"
"github.com/spf13/cobra"
)

type Resource struct {
Singular string
Plural string
Parents []*Resource
PatternElems []string // TOO(yft): support multiple patterns
Schema *openapi.Schema
GetMethod *GetMethod
ListMethod *ListMethod
CreateMethod *CreateMethod
UpdateMethod *UpdateMethod
DeleteMethod *DeleteMethod
CustomMethods []*CustomMethod
}

type CreateMethod struct {
SupportsUserSettableCreate bool
}

type GetMethod struct {
}

type UpdateMethod struct {
}

type ListMethod struct {
}

type DeleteMethod struct {
}

type CustomMethod struct {
Name string
Method string
Request *openapi.Schema
Response *openapi.Schema
}

func (r *Resource) ExecuteCommand(args []string) (*http.Request, string, error) {
func ExecuteResourceCommand(r *api.Resource, args []string) (*http.Request, string, error) {
c := cobra.Command{Use: r.Singular}
var err error
var req *http.Request
Expand Down Expand Up @@ -205,10 +169,6 @@ func (r *Resource) ExecuteCommand(args []string) (*http.Request, string, error)
return req, stdout.String() + stderr.String(), err
}

func (r *Resource) GetPattern() string {
return strings.Join(r.PatternElems, "/")
}

func addSchemaFlags(c *cobra.Command, schema openapi.Schema, args map[string]interface{}) error {
for name, prop := range schema.Properties {
if prop.ReadOnly {
Expand Down
35 changes: 18 additions & 17 deletions internal/service/resource_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"io"
"testing"

"github.com/aep-dev/aepcli/internal/openapi"
"github.com/aep-dev/aep-lib-go/pkg/api"
"github.com/aep-dev/aep-lib-go/pkg/openapi"
)

var projectResource = Resource{
var projectResource = api.Resource{
Singular: "project",
Plural: "projects",
PatternElems: []string{"projects", "{project}"},
Parents: []*Resource{},
Parents: []*api.Resource{},
Schema: &openapi.Schema{
Properties: map[string]openapi.Schema{
"name": {
Expand All @@ -38,17 +39,17 @@ var projectResource = Resource{
},
Required: []string{"name"},
},
GetMethod: &GetMethod{},
ListMethod: &ListMethod{},
CreateMethod: &CreateMethod{},
UpdateMethod: &UpdateMethod{},
DeleteMethod: &DeleteMethod{},
GetMethod: &api.GetMethod{},
ListMethod: &api.ListMethod{},
CreateMethod: &api.CreateMethod{},
UpdateMethod: &api.UpdateMethod{},
DeleteMethod: &api.DeleteMethod{},
}

func TestExecuteCommand(t *testing.T) {
tests := []struct {
name string
resource Resource
resource api.Resource
args []string
expectedQuery string
expectedPath string
Expand Down Expand Up @@ -87,17 +88,17 @@ func TestExecuteCommand(t *testing.T) {
},
{
name: "resource with parent",
resource: Resource{
resource: api.Resource{
Singular: "dataset",
Plural: "datasets",
PatternElems: []string{"projects", "{project}", "datasets", "{dataset}"},
Parents: []*Resource{&projectResource},
Parents: []*api.Resource{&projectResource},
Schema: &openapi.Schema{},
GetMethod: &GetMethod{},
ListMethod: &ListMethod{},
CreateMethod: &CreateMethod{},
UpdateMethod: &UpdateMethod{},
DeleteMethod: &DeleteMethod{},
GetMethod: &api.GetMethod{},
ListMethod: &api.ListMethod{},
CreateMethod: &api.CreateMethod{},
UpdateMethod: &api.UpdateMethod{},
DeleteMethod: &api.DeleteMethod{},
},
args: []string{"--project=foo", "get", "abc"},
expectedPath: "projects/foo/datasets/abc",
Expand All @@ -109,7 +110,7 @@ func TestExecuteCommand(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _, err := tt.resource.ExecuteCommand(tt.args)
req, _, err := ExecuteResourceCommand(&tt.resource, tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("ExecuteCommand() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
Loading

0 comments on commit 4ba1ee8

Please sign in to comment.