Skip to content

Commit

Permalink
Resolves #382 - Improve auto-completion performance
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west committed Oct 2, 2023
1 parent 6f9dd91 commit c8355fe
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
"sync"
)

var DisableLongOutput = false
var DisableExampleOutput = false

func GetSingularTypeNames(types []string) []string {
var ret []string

Expand Down Expand Up @@ -242,6 +245,10 @@ func GetDeleteAllShort(resource resources.Resource) string {

func GetGetLong(resourceName string, resourceUrl string, usageGetType string, completionVerb int, urlInfo *resources.CrudEntityInfo, resource resources.Resource) string {

if DisableLongOutput {
return ""
}

types, err := resources.GetTypesOfVariablesNeeded(resourceUrl)

if err != nil {
Expand Down Expand Up @@ -322,6 +329,9 @@ func toJsonExample(in []string, resource resources.Resource) string {
}

func GetCreateLong(resource resources.Resource) string {
if DisableLongOutput {
return ""
}
resourceName := resource.SingularName

singularTypeNames, err := resources.GetSingularTypesOfVariablesNeeded(resource.CreateEntityInfo.Url)
Expand Down Expand Up @@ -354,6 +364,9 @@ Documentation:
}

func GetUpdateLong(resource resources.Resource) string {
if DisableLongOutput {
return ""
}
resourceName := resource.SingularName

singularTypeNames, err := resources.GetSingularTypesOfVariablesNeeded(resource.UpdateEntityInfo.Url)
Expand Down Expand Up @@ -386,6 +399,9 @@ Documentation:
}

func GetDeleteLong(resource resources.Resource) string {
if DisableLongOutput {
return ""
}
resourceName := resource.SingularName

singularTypeNames, err := resources.GetSingularTypesOfVariablesNeeded(resource.DeleteEntityInfo.Url)
Expand Down Expand Up @@ -499,6 +515,10 @@ var getExampleCache sync.Map

func GetGetExample(resourceName string, resourceUrl string, usageGetType string, completionVerb int, urlInfo *resources.CrudEntityInfo, resource resources.Resource) string {

if DisableExampleOutput {
return ""
}

cacheKey := fmt.Sprintf("%s-%d", resourceName, completionVerb)
if example, ok := getExampleCache.Load(cacheKey); ok {
return example.(string)
Expand Down Expand Up @@ -605,6 +625,10 @@ func GetGetExample(resourceName string, resourceUrl string, usageGetType string,
var createExampleCache sync.Map

func GetCreateExample(resource resources.Resource) string {
if DisableExampleOutput {
return ""
}

resourceName := resource.SingularName

if v, ok := createExampleCache.Load(resourceName); ok {
Expand Down Expand Up @@ -685,6 +709,9 @@ func GetCreateExample(resource resources.Resource) string {
var updateExampleCache sync.Map

func GetUpdateExample(resource resources.Resource) string {
if DisableExampleOutput {
return ""
}
resourceName := resource.SingularName

if v, ok := updateExampleCache.Load(resourceName); ok {
Expand Down Expand Up @@ -754,6 +781,10 @@ func GetUpdateExample(resource resources.Resource) string {
var deleteExampleCache sync.Map

func GetDeleteExample(resource resources.Resource) string {
if DisableExampleOutput {
return ""
}

resourceName := resource.SingularName
if v, ok := deleteExampleCache.Load(resourceName); ok {
return v.(string)
Expand Down
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ var jqCompletionFunc = func(cmd *cobra.Command, args []string, toComplete string
var profileNameFromCommandLine = ""

func InitializeCmd() {
if os.Args[1] == "__complete" {
DisableLongOutput = true
DisableExampleOutput = true
}

cobra.OnInitialize(initConfig)
initConfig()
Expand Down
8 changes: 8 additions & 0 deletions cmd/runbooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ type CommandAndReset struct {
}

func generateRunbookCmd() *CommandAndReset {
DisableLongOutput = true
DisableExampleOutput = true

defer func() {
DisableLongOutput = false
DisableExampleOutput = false
}()

root := &cobra.Command{
Use: "epcc",
SilenceUsage: true,
Expand Down

0 comments on commit c8355fe

Please sign in to comment.