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
37 changes: 27 additions & 10 deletions cmd/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,25 @@ var aliasesCmd = &cobra.Command{
}

var aliasListCmd = &cobra.Command{
Use: "list <resource>",
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])

resourcesToPrint := args

if len(resourcesToPrint) == 0 {
resourcesToPrint = resources.GetSingularResourceNames()
}

sort.Strings(resourcesToPrint)
if len(resourcesToPrint) != 1 {
fmt.Printf("%45s || %100s || Values\n", "Resource Type", "Alias Name")
} else {
fmt.Printf("%45s || Values\n", "Alias Name")
}

for _, resourceName := range resourcesToPrint {
resource, ok := resources.GetResourceByName(resourceName)
if !ok {
return fmt.Errorf("could not find resource information for resource: %s", args[0])
}
Expand All @@ -37,10 +50,14 @@ var aliasListCmd = &cobra.Command{

sort.Strings(sortedAliasNames)

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

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

if len(resourcesToPrint) != 1 {
fmt.Printf("%45s %100s => ID: %s", resourceName, alias, aliases[alias].Id)
} else {
fmt.Printf("%45s => ID: %s", alias, aliases[alias].Id)
}

if aliases[alias].Sku != "" {
fmt.Printf(" Sku: %10s", aliases[alias].Sku)
}
Expand All @@ -51,10 +68,10 @@ var aliasListCmd = &cobra.Command{

fmt.Println()
}

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

return nil

},

ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
53 changes: 47 additions & 6 deletions external/aliases/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -42,6 +43,12 @@ func ClearAllAliasesForJsonApiType(jsonApiType string) error {

}

// Used to determine index of element in array.
var arrayPathPattern = regexp.MustCompile("^\\.data\\[([0-9]+)]$")

// Used to determine name of relationship
var relationshipPattern = regexp.MustCompile("^\\.data(?:\\[[0-9]+])?\\.relationships\\.([^.]+)\\.data")

func GetAliasesForJsonApiType(jsonApiType string) map[string]*id.IdableAttributes {
profileDirectory := getAliasDataDirectory()
aliasFile := getAliasFileForJsonApiType(profileDirectory, jsonApiType)
Expand Down Expand Up @@ -90,7 +97,7 @@ func SaveAliasesForResources(jsonTxt string) {
}

results := map[string]map[string]*id.IdableAttributes{}
visitResources(jsonStruct, "", results)
visitResources(jsonStruct, "", "", map[string]*id.IdableAttributes{}, results)

log.Tracef("All aliases: %v", results)

Expand Down Expand Up @@ -201,12 +208,16 @@ func saveAliasesForResource(jsonApiType string, newAliases map[string]*id.Idable
})
}

func visitResources(data map[string]interface{}, prefix string, results map[string]map[string]*id.IdableAttributes) {
func visitResources(data map[string]interface{}, prefix string, parentAliasType string, parentAliases map[string]*id.IdableAttributes, results map[string]map[string]*id.IdableAttributes) {
aliases := map[string]*id.IdableAttributes{}
myType := ""
if typeObj, typeKeyExists := data["type"]; typeKeyExists {
if idObj, idKeyExists := data["id"]; idKeyExists {
if typeKeyValue, typeKeyIsString := typeObj.(string); typeKeyIsString {
if idKeyValue, idKeyIsString := idObj.(string); idKeyIsString {
aliases := generateAliasesForStruct(typeKeyValue, idKeyValue, data)

myType = typeKeyValue
aliases = generateAliasesForStruct(prefix, parentAliasType, parentAliases, typeKeyValue, idKeyValue, data)

log.Tracef("Found a type and id pair %s => %s under prefix %s, aliases %v", typeKeyValue, idKeyValue, prefix, aliases)

Expand All @@ -222,16 +233,26 @@ func visitResources(data map[string]interface{}, prefix string, results map[stri
}
}

parentAliasesToUse := parentAliases

if len(aliases) > 0 {
parentAliasesToUse = aliases
}

parentAliasTypeToUse := parentAliasType
if myType != "" {
parentAliasTypeToUse = myType
}
// Recursively descend over each element
for key, val := range data {
if mapType, ok := val.(map[string]interface{}); ok {
visitResources(mapType, prefix+"."+key, results)
visitResources(mapType, prefix+"."+key, parentAliasTypeToUse, parentAliasesToUse, results)
}

if arrayType, ok := val.([]interface{}); ok {
for idx, value := range arrayType {
if mapValue, ok := value.(map[string]interface{}); ok {
visitResources(mapValue, prefix+"."+key+"["+strconv.Itoa(idx)+"]", results)
visitResources(mapValue, prefix+"."+key+"["+strconv.Itoa(idx)+"]", parentAliasTypeToUse, parentAliasesToUse, results)
}

}
Expand All @@ -242,7 +263,7 @@ func visitResources(data map[string]interface{}, prefix string, results map[stri
return
}

func generateAliasesForStruct(typeKey string, idKey string, data map[string]interface{}) map[string]*id.IdableAttributes {
func generateAliasesForStruct(prefix string, parentAliasType string, parentAliases map[string]*id.IdableAttributes, typeKey string, idKey string, data map[string]interface{}) map[string]*id.IdableAttributes {
result := id.IdableAttributes{
Id: idKey,
}
Expand All @@ -252,6 +273,26 @@ func generateAliasesForStruct(typeKey string, idKey string, data map[string]inte
"id=" + idKey: &result,
}

if prefix == ".data" {
results["last_read=entity"] = &result
}

if arrayPathPattern.MatchString(prefix) {
matches := arrayPathPattern.FindStringSubmatch(prefix)
results["last_read=array["+matches[1]+"]"] = &result
}

if relationshipPattern.MatchString(prefix) {
matches := relationshipPattern.FindStringSubmatch(prefix)
//related_buz_for_foo_id_123
keyPrefix := "related_" + matches[1] + "_for_" + parentAliasType + "_"

for k := range parentAliases {
results[keyPrefix+k] = &result
}

}

jsonObjectsToInspect := make([]map[string]interface{}, 0)
jsonObjectsToInspect = append(jsonObjectsToInspect, data)

Expand Down
Loading