Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,19 @@ For development the following command using [Reflex](https://github.com/cespare/

```bash
git fetch --all --tags && reflex -v -r '(\.go$)|(resources.yaml|go.mod)$' -- sh -c "go build -ldflags=\"-X github.com/elasticpath/epcc-cli/external/version.Version=$(git describe --tags --abbrev=0)+1 -X github.com/elasticpath/epcc-cli/external/version.Commit=$(git rev-parse --short HEAD)-dirty\" -o ./epcc"
```
```


### Git Hooks

The following git pre-commit hook will run go fmt before committing anything

```bash
#!/bin/bash

echo "Running go fmt"
go fmt "./..."

echo "Adding changed files back to git"
git diff --cached --name-only --diff-filter=ACM | grep -E "\.(go)$" | xargs git add
```
150 changes: 80 additions & 70 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,81 +21,13 @@ var create = &cobra.Command{
Short: "Creates an entity of a resource.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Find Resource
resource, ok := resources.GetResourceByName(args[0])
if !ok {
return fmt.Errorf("could not find resource %s", args[0])
}

if resource.CreateEntityInfo == nil {
return fmt.Errorf("resource %s doesn't support CREATE", args[0])
}

// Count ids in CreateEntity
resourceURL := resource.CreateEntityInfo.Url

idCount, err := resources.GetNumberOfVariablesNeeded(resourceURL)
body, err := createInternal(args)

if err != nil {
return err
}

// Replace ids with args in resourceURL
resourceURL, err = resources.GenerateUrl(resource.CreateEntityInfo, args[1:])

if err != nil {
return err
}

var resp *http.Response = nil
var resBody []byte

if resource.CreateEntityInfo.ContentType == "multipart/form-data" {

byteBuf, contentType, err := encoding.ToMultiPartEncoding(args[(idCount+1):], resource.NoWrapping, resource.JsonApiFormat == "complaint", resource.Attributes)
if err != nil {
return err
}

// Submit request
resp, err = httpclient.DoFileRequest(context.TODO(), resourceURL, byteBuf, contentType)

} else {
// Assume it's application/json

if !resource.NoWrapping {
args = append(args, "type", resource.JsonApiType)
}
// Create the body from remaining args
body, err := json.ToJson(args[(idCount+1):], resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes)

if err != nil {
return err
}

// Submit request
resp, err = httpclient.DoRequest(context.TODO(), "POST", resourceURL, "", strings.NewReader(body))

}

if err != nil {
return fmt.Errorf("got error %s", err.Error())
}
defer resp.Body.Close()

// Print the body
resBody, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

// Check if error response
if resp.StatusCode >= 400 && resp.StatusCode <= 600 {
json.PrintJson(string(resBody))
return fmt.Errorf(resp.Status)
}
aliases.SaveAliasesForResources(string(resBody))
return json.PrintJson(string(resBody))
return json.PrintJson(body)
},

ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -156,3 +88,81 @@ var create = &cobra.Command{
return []string{}, cobra.ShellCompDirectiveNoFileComp
},
}

func createInternal(args []string) (string, error) {
// Find Resource
resource, ok := resources.GetResourceByName(args[0])
if !ok {
return "", fmt.Errorf("could not find resource %s", args[0])
}

if resource.CreateEntityInfo == nil {
return "", fmt.Errorf("resource %s doesn't support CREATE", args[0])
}

// Count ids in CreateEntity
resourceURL := resource.CreateEntityInfo.Url

idCount, err := resources.GetNumberOfVariablesNeeded(resourceURL)

if err != nil {
return "", err
}

// Replace ids with args in resourceURL
resourceURL, err = resources.GenerateUrl(resource.CreateEntityInfo, args[1:])

if err != nil {
return "", err
}

var resp *http.Response = nil
var resBody []byte

if resource.CreateEntityInfo.ContentType == "multipart/form-data" {

byteBuf, contentType, err := encoding.ToMultiPartEncoding(args[(idCount+1):], resource.NoWrapping, resource.JsonApiFormat == "complaint", resource.Attributes)
if err != nil {
return "", err
}

// Submit request
resp, err = httpclient.DoFileRequest(context.TODO(), resourceURL, byteBuf, contentType)

} else {
// Assume it's application/json

if !resource.NoWrapping {
args = append(args, "type", resource.JsonApiType)
}
// Create the body from remaining args
body, err := json.ToJson(args[(idCount+1):], resource.NoWrapping, resource.JsonApiFormat == "compliant", resource.Attributes)

if err != nil {
return "", err
}

// Submit request
resp, err = httpclient.DoRequest(context.TODO(), "POST", resourceURL, "", strings.NewReader(body))

}

if err != nil {
return "", fmt.Errorf("got error %s", err.Error())
}
defer resp.Body.Close()

// Print the body
resBody, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

// Check if error response
if resp.StatusCode >= 400 && resp.StatusCode <= 600 {
json.PrintJson(string(resBody))
return "", fmt.Errorf(resp.Status)
}
aliases.SaveAliasesForResources(string(resBody))
return string(resBody), nil
}
41 changes: 25 additions & 16 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,12 @@ var get = &cobra.Command{
Short: "Retrieves a single resource.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
resp, err := getResource(args)

err, body := getInternal(args)
if err != nil {
return err
}

// Print the body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

// Check if error response
if resp.StatusCode >= 400 && resp.StatusCode <= 600 {
json.PrintJson(string(body))
return fmt.Errorf(resp.Status)
}

aliases.SaveAliasesForResources(string(body))
return json.PrintJson(string(body))
return json.PrintJson(body)
},

ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -104,6 +90,29 @@ var get = &cobra.Command{
},
}

func getInternal(args []string) (error, string) {
resp, err := getResource(args)

if err != nil {
return err, ""
}

// Print the body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

// Check if error response
if resp.StatusCode >= 400 && resp.StatusCode <= 600 {
json.PrintJson(string(body))
return fmt.Errorf(resp.Status), ""
}

aliases.SaveAliasesForResources(string(body))
return nil, string(body)
}

func getUrl(resource resources.Resource, args []string) (*resources.CrudEntityInfo, error) {

if resource.GetCollectionInfo == nil && resource.GetEntityInfo == nil {
Expand Down
Loading