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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ e.g. `export EPCC_API_BASE_URL=https://api.moltin.com`
| EPCC_API_BASE_URL | This is the API base URL which can be retrieved via CM. |
| EPCC_CLIENT_ID | This is the Client ID which can be retrieved via CM. |
| EPCC_CLIENT_SECRET | This is the Client Secret which can be retrieved via CM. |
| EPCC_PROFILE | A profile name that allows for an independent session and isolation (e.g., distinct histories) |
| EPCC_BETA_API_FEATURES | This variable allows you to set [Beta Headers](https://documentation.elasticpath.com/commerce-cloud/docs/api/basics/api-contract.html#beta-apis) for all API calls. |
| EPCC_CLI_HTTP_HEADER_**N** | Setting any environment variable with this prefix will cause it's value to be parsed and added to all HTTP headers (e.g., `EPCC_CLI_HTTP_HEADER_0=Cache-Control: no-cache` will add `Cache-Control: no-cache` as a header). FYI, the surprising syntax is due to different encoding rules. |

Expand Down
13 changes: 12 additions & 1 deletion cmd/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,19 @@ var aliasListCmd = &cobra.Command{

sort.Strings(sortedAliasNames)

fmt.Printf("%40s || Values\n", "Alias Name")

for _, alias := range sortedAliasNames {
fmt.Printf("%40s => %s\n", alias, aliases[alias])
fmt.Printf("%40s => ID: %s", alias, aliases[alias].Id)
if aliases[alias].Sku != "" {
fmt.Printf(" Sku: %10s", aliases[alias].Sku)
}

if aliases[alias].Slug != "" {
fmt.Printf(" Slug: %10s", aliases[alias].Slug)
}

fmt.Println()
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var create = &cobra.Command{
}

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

if err != nil {
return err
Expand Down
46 changes: 29 additions & 17 deletions cmd/delete-all.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/elasticpath/epcc-cli/external/apihelper"
"github.com/elasticpath/epcc-cli/external/completion"
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/id"
"github.com/elasticpath/epcc-cli/external/resources"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -48,9 +49,9 @@ var DeleteAll = &cobra.Command{
}

for _, parentEntityIds := range allParentEntityIds {
lastIds := make([][]string, 1)
lastIds := make([][]id.IdableAttributes, 1)
for {
resourceURL, err := resources.GenerateUrl(resource, resource.GetCollectionInfo.Url, parentEntityIds)
resourceURL, err := resources.GenerateUrlViaIdableAttributes(resource.GetCollectionInfo, parentEntityIds)

if err != nil {
return err
Expand All @@ -68,7 +69,7 @@ var DeleteAll = &cobra.Command{
ids, err := apihelper.GetResourceIdsFromHttpResponse(resp)
resp.Body.Close()

allIds := make([][]string, 0)
allIds := make([][]id.IdableAttributes, 0)
for _, id := range ids {
allIds = append(allIds, append(parentEntityIds, id))
}
Expand Down Expand Up @@ -96,7 +97,7 @@ var DeleteAll = &cobra.Command{
break
}

delPage(resource.PluralName, allIds)
delPage(resource.DeleteEntityInfo, allIds)
}
}

Expand All @@ -116,9 +117,9 @@ var DeleteAll = &cobra.Command{
}

//
func getParentIds(ctx context.Context, resource resources.Resource) ([][]string, error) {
func getParentIds(ctx context.Context, resource resources.Resource) ([][]id.IdableAttributes, error) {

myEntityIds := make([][]string, 0)
myEntityIds := make([][]id.IdableAttributes, 0)
if resource.GetCollectionInfo == nil {
return myEntityIds, fmt.Errorf("resource %s doesn't support GET collection", resource.PluralName)
}
Expand All @@ -130,7 +131,7 @@ func getParentIds(ctx context.Context, resource resources.Resource) ([][]string,
}

if len(types) == 0 {
myEntityIds = append(myEntityIds, make([]string, 0))
myEntityIds = append(myEntityIds, make([]id.IdableAttributes, 0))
return myEntityIds, nil
} else {
immediateParentType := types[len(types)-1]
Expand All @@ -145,19 +146,30 @@ func getParentIds(ctx context.Context, resource resources.Resource) ([][]string,
}
}

func delPage(resourceName string, ids [][]string) {
func delPage(urlInfo *resources.CrudEntityInfo, ids [][]id.IdableAttributes) {
// Create a wait group to run DELETE in parallel
wg := sync.WaitGroup{}
for _, id := range ids {
for _, idAttr := range ids {
wg.Add(1)
go func(id []string) {
args := make([]string, 0)
args = append(args, resourceName)
args = append(args, id...)

deleteResource(args)
wg.Done()
}(id)
go func(idAttr []id.IdableAttributes) {

defer wg.Done()
// Find Resource
// Replace ids with args in resourceURL
resourceURL, err := resources.GenerateUrlViaIdableAttributes(urlInfo, idAttr)

if err != nil {
return
}

// Submit request
resp, err := httpclient.DoRequest(context.TODO(), "DELETE", resourceURL, "", nil)
if err != nil {
return
}
defer resp.Body.Close()

}(idAttr)
}
wg.Wait()
}
8 changes: 3 additions & 5 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ var delete = &cobra.Command{
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

idCount, err := resources.GetNumberOfVariablesNeeded(resourceURL)
idCount, err := resources.GetNumberOfVariablesNeeded(resourceURL.Url)

if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

if len(args) > 0 && len(args) < 1+idCount {
// Must be for a resource completion
types, err := resources.GetTypesOfVariablesNeeded(resourceURL)
types, err := resources.GetTypesOfVariablesNeeded(resourceURL.Url)

if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
Expand Down Expand Up @@ -99,10 +99,8 @@ func deleteResource(args []string) (*http.Response, error) {
return nil, fmt.Errorf("resource %s doesn't support DELETE", args[0])
}

resourceURL := resource.DeleteEntityInfo.Url

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

if err != nil {
return nil, err
Expand Down
26 changes: 12 additions & 14 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ var get = &cobra.Command{
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

idCount, err := resources.GetNumberOfVariablesNeeded(resourceURL)
idCount, err := resources.GetNumberOfVariablesNeeded(resourceURL.Url)

if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

if len(args) > 0 && len(args) < 1+idCount {
// Must be for a resource completion
types, err := resources.GetTypesOfVariablesNeeded(resourceURL)
types, err := resources.GetTypesOfVariablesNeeded(resourceURL.Url)

if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
Expand Down Expand Up @@ -104,22 +104,21 @@ var get = &cobra.Command{
},
}

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

if resource.GetCollectionInfo == nil && resource.GetEntityInfo == nil {
return "", fmt.Errorf("resource %s doesn't support GET", args[0])
return nil, fmt.Errorf("resource %s doesn't support GET", args[0])
} else if resource.GetCollectionInfo != nil && resource.GetEntityInfo == nil {
resourceURL = resource.GetCollectionInfo.Url
return resource.GetCollectionInfo, nil
} else if resource.GetCollectionInfo == nil && resource.GetEntityInfo != nil {
resourceURL = resource.GetEntityInfo.Url
return resource.GetEntityInfo, nil
} else {
if _, ok := resources.GetPluralResources()[args[0]]; ok {
resourceURL = resource.GetCollectionInfo.Url
return resource.GetCollectionInfo, nil
} else {
resourceURL = resource.GetEntityInfo.Url
return resource.GetEntityInfo, nil
}
}
return resourceURL, nil
}

func getResource(args []string) (*http.Response, error) {
Expand All @@ -129,22 +128,21 @@ func getResource(args []string) (*http.Response, error) {
return nil, fmt.Errorf("could not find resource %s", args[0])
}

var resourceURL string
var idCount int

resourceURL, err2 := getUrl(resource, args)
resourceUrlInfo, err2 := getUrl(resource, args)
if err2 != nil {
return nil, err2
}

idCount, err := resources.GetNumberOfVariablesNeeded(resourceURL)
idCount, err := resources.GetNumberOfVariablesNeeded(resourceUrlInfo.Url)

if err != nil {
return nil, err
}

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

if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion cmd/reset-store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var ResetStore = &cobra.Command{
return fmt.Errorf("could not find resource %s, we need it to determine the store id.", args[0])
}

resourceURL, err := resources.GenerateUrl(resource, resource.GetCollectionInfo.Url, make([]string, 0))
resourceURL, err := resources.GenerateUrl(resource.GetCollectionInfo, make([]string, 0))

if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func init() {
RootCmd.PersistentFlags().Uint16VarP(&rateLimit, "rate-limit", "", 10, "Request limit per second")

aliasesCmd.AddCommand(aliasListCmd, aliasClearCmd)

}

var persistentPreRunFuncs []func(cmd *cobra.Command, args []string) error
Expand All @@ -84,6 +85,7 @@ Environment Variables
- EPCC_CLIENT_SECRET - The client secret (available in Commerce Manager)
- EPCC_BETA_API_FEATURES - Beta features in the API we want to enable.
- EPCC_CLI_HTTP_HEADER_[0,1,...] - An additional HTTP header to set with all requests, the format should be "HeaderName: value"
- EPCC_PROFILE - The name of the profile we will use (isolates namespace, credentials, etc...)

`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
Expand Down
6 changes: 3 additions & 3 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ var update = &cobra.Command{
}

// Count ids in UpdateEntity
resourceURL := resource.UpdateEntityInfo.Url
idCount, err := resources.GetNumberOfVariablesNeeded(resourceURL)
resourceUrlInfo := resource.UpdateEntityInfo
idCount, err := resources.GetNumberOfVariablesNeeded(resourceUrlInfo.Url)
if err != nil {
return err
}

// Replace ids with args in resourceURL
resourceURL, err = resources.GenerateUrl(resource, resourceURL, args[1:])
resourceURL, err := resources.GenerateUrl(resourceUrlInfo, args[1:])
if err != nil {
return err
}
Expand Down
Loading