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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bin

.env
dist/
epcc-cli
epcc
56 changes: 56 additions & 0 deletions cmd/aliases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"fmt"
"github.com/elasticpath/epcc-cli/external/aliases"
"github.com/elasticpath/epcc-cli/external/completion"
"github.com/elasticpath/epcc-cli/external/resources"
"github.com/spf13/cobra"
"sort"
)

var aliasesCmd = &cobra.Command{
Comment thread
steve-r-west marked this conversation as resolved.
Use: "aliases",
SilenceUsage: false,
}

var aliasListCmd = &cobra.Command{
Use: "list <resource>",
Short: "Lists all aliases for a resource",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
resource, ok := resources.GetResourceByName(args[0])
if !ok {
return fmt.Errorf("Could not find resource information for resource: %s", args[0])
}

aliases := aliases.GetAliasesForJsonApiType(resource.JsonApiType)

sortedAliasNames := make([]string, 0, len(aliases))

for i := range aliases {
sortedAliasNames = append(sortedAliasNames, i)
}

sort.Strings(sortedAliasNames)

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

return nil
}
return fmt.Errorf("You must supply a resource type to the aliases command")
},

ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return completion.Complete(completion.Request{
Type: completion.CompletePluralResource,
})
}

return []string{}, cobra.ShellCompDirectiveNoFileComp
},
}
31 changes: 27 additions & 4 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"github.com/elasticpath/epcc-cli/external/aliases"
"github.com/elasticpath/epcc-cli/external/completion"
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/json"
Expand Down Expand Up @@ -38,15 +39,17 @@ var create = &cobra.Command{
}

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

if err != nil {
return err
}

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

if err != nil {
return err
Expand All @@ -72,6 +75,8 @@ var create = &cobra.Command{
return fmt.Errorf(resp.Status)
}

aliases.SaveAliasesForResources(string(resBody))

return json.PrintJson(string(resBody))
},

Expand All @@ -87,7 +92,8 @@ var create = &cobra.Command{
resource, ok := resources.GetResourceByName(args[0])
if ok {
if resource.CreateEntityInfo != nil {
idCount, _ := resources.GetNumberOfVariablesNeeded(resource.CreateEntityInfo.Url)
resourceURL := resource.CreateEntityInfo.Url
idCount, _ := resources.GetNumberOfVariablesNeeded(resourceURL)
if len(args)-idCount >= 1 { // Arg is after IDs
if (len(args)-idCount)%2 == 1 { // This is an attribute key
usedAttributes := make(map[string]int)
Expand All @@ -108,6 +114,23 @@ var create = &cobra.Command{
Attribute: args[len(args)-1],
})
}
} else {
// Arg is in IDS
// Must be for a resource completion
types, err := resources.GetTypesOfVariablesNeeded(resourceURL)

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

typeIdxNeeded := len(args) - 1

if completionResource, ok := resources.GetResourceByName(types[typeIdxNeeded]); ok {
return completion.Complete(completion.Request{
Type: completion.CompleteAlias,
Resource: completionResource,
})
}
}
}
}
Expand Down
35 changes: 34 additions & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var delete = &cobra.Command{
resourceURL := resource.DeleteEntityInfo.Url

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

if err != nil {
return err
Expand Down Expand Up @@ -62,6 +62,39 @@ var delete = &cobra.Command{
Type: completion.CompleteSingularResource,
Verb: completion.Delete,
})
} else if resource, ok := resources.GetResourceByName(args[0]); ok {
// len(args) == 0 means complete resource
// len(args) == 1 means first id
// lens(args) == 2 means second id.

resourceURL, err := getUrl(resource, args)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

idCount, err := resources.GetNumberOfVariablesNeeded(resourceURL)

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)

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

typeIdxNeeded := len(args) - 1

if completionResource, ok := resources.GetResourceByName(types[typeIdxNeeded]); ok {
return completion.Complete(completion.Request{
Type: completion.CompleteAlias,
Resource: completionResource,
})
}
}
}

return []string{}, cobra.ShellCompDirectiveNoFileComp
Expand Down
71 changes: 58 additions & 13 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"github.com/elasticpath/epcc-cli/external/aliases"
"github.com/elasticpath/epcc-cli/external/completion"
"github.com/elasticpath/epcc-cli/external/httpclient"
"github.com/elasticpath/epcc-cli/external/json"
Expand All @@ -27,18 +28,9 @@ var get = &cobra.Command{
var resourceURL string
var idCount int

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

idCount, err := resources.GetNumberOfVariablesNeeded(resourceURL)
Expand All @@ -48,7 +40,7 @@ var get = &cobra.Command{
}

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

if err != nil {
return err
Expand Down Expand Up @@ -85,6 +77,7 @@ var get = &cobra.Command{
return fmt.Errorf(resp.Status)
}

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

Expand All @@ -94,8 +87,60 @@ var get = &cobra.Command{
Type: completion.CompleteSingularResource | completion.CompletePluralResource,
Verb: completion.Get,
})
} else if resource, ok := resources.GetResourceByName(args[0]); ok {
// len(args) == 0 means complete resource
// len(args) == 1 means first id
// lens(args) == 2 means second id.

resourceURL, err := getUrl(resource, args)
Comment thread
steve-r-west marked this conversation as resolved.
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

idCount, err := resources.GetNumberOfVariablesNeeded(resourceURL)

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)

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

typeIdxNeeded := len(args) - 1

if completionResource, ok := resources.GetResourceByName(types[typeIdxNeeded]); ok {
return completion.Complete(completion.Request{
Type: completion.CompleteAlias,
Resource: completionResource,
})
}

}
}

return []string{}, cobra.ShellCompDirectiveNoFileComp
},
}

func getUrl(resource resources.Resource, args []string) (string, error) {
resourceURL := ""
if resource.GetCollectionInfo == nil && resource.GetEntityInfo == nil {
return "", fmt.Errorf("Resource %s doesn't support GET", args[0])
} else if resource.GetCollectionInfo != nil && resource.GetEntityInfo == nil {
resourceURL = resource.GetCollectionInfo.Url
} else if resource.GetCollectionInfo == nil && resource.GetEntityInfo != nil {
resourceURL = resource.GetEntityInfo.Url
} else {
if _, ok := resources.GetPluralResources()[args[0]]; ok {
resourceURL = resource.GetCollectionInfo.Url
} else {
resourceURL = resource.GetEntityInfo.Url
}
}
return resourceURL, nil
}
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func init() {
delete,
update,
resourceListCommand,
aliasesCmd,
)

testJson.Flags().BoolVarP(&noWrapping, "no-wrapping", "", false, "if set, we won't wrap the output the json in a data tag")
Expand All @@ -42,6 +43,8 @@ func init() {
"sets logging level; can be 'trace', 'debug', 'info', 'warn', 'error', 'fatal', 'panic'")
rootCmd.PersistentFlags().BoolVarP(&json.MonochromeOutput, "monochrome-output", "M", false, "By default, epcc will output using colors if the terminal supports this. Use this option to disable it.")
rootCmd.PersistentFlags().StringSliceVarP(&globals.RawHeaders, "header", "H", []string{}, "Extra headers and values to include in the request when sending HTTP to a server. You may specify any number of extra headers.")

aliasesCmd.AddCommand(aliasListCmd)
}

var rootCmd = &cobra.Command{
Expand Down
3 changes: 2 additions & 1 deletion cmd/test-json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/elasticpath/epcc-cli/external/json"
"github.com/elasticpath/epcc-cli/external/resources"
"github.com/spf13/cobra"
)

Expand All @@ -11,7 +12,7 @@ var testJson = &cobra.Command{
Use: "test-json [KEY_1] [VAL_1] [KEY_2] [VAL_2] ...",
Short: "Prints the resulting json for what a command will look like",
RunE: func(cmd *cobra.Command, args []string) error {
res, err := json.ToJson(args, noWrapping, compliant)
res, err := json.ToJson(args, noWrapping, compliant, map[string]*resources.CrudEntityAttribute{})

if res != "" {
json.PrintJson(res)
Expand Down
24 changes: 21 additions & 3 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ var update = &cobra.Command{
}

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

args = append(args, "type", resource.JsonApiType)
// Create the body from remaining args
body, err := json.ToJson(args[(idCount+1):], false, resource.JsonApiFormat == "compliant")
body, err := json.ToJson(args[(idCount+1):], false, resource.JsonApiFormat == "compliant", resource.Attributes)
if err != nil {
return err
}
Expand Down Expand Up @@ -82,7 +82,8 @@ var update = &cobra.Command{
resource, ok := resources.GetResourceByName(args[0])
if ok {
if resource.UpdateEntityInfo != nil {
idCount, _ := resources.GetNumberOfVariablesNeeded(resource.UpdateEntityInfo.Url)
resourceURL := resource.UpdateEntityInfo.Url
idCount, _ := resources.GetNumberOfVariablesNeeded(resourceURL)
if len(args)-idCount >= 1 { // Arg is after IDs
if (len(args)-idCount)%2 == 1 { // This is an attribute key
usedAttributes := make(map[string]int)
Expand All @@ -103,6 +104,23 @@ var update = &cobra.Command{
Attribute: args[len(args)-1],
})
}
} else {
// Arg is in IDS
// Must be for a resource completion
types, err := resources.GetTypesOfVariablesNeeded(resourceURL)

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

typeIdxNeeded := len(args) - 1

if completionResource, ok := resources.GetResourceByName(types[typeIdxNeeded]); ok {
return completion.Complete(completion.Request{
Type: completion.CompleteAlias,
Resource: completionResource,
})
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Env struct {
EPCC_CLIENT_ID string `env:"EPCC_CLIENT_ID"`
EPCC_CLIENT_SECRET string `env:"EPCC_CLIENT_SECRET"`
EPCC_BETA_API_FEATURES string `env:"EPCC_BETA_API_FEATURES"`
EPCC_PROFILE string `env:"EPCC_PROFILE"`
}

var Envs = &Env{}
Empty file added docs/get-started-with-pcm.md
Empty file.
Loading