func findAutocompleteAPI()

in cli/completer.go [199:271]


func findAutocompleteAPI(arg *config.APIArg, apiFound *config.API, apiMap map[string][]*config.API) *config.API {
	if arg.Type == "map" {
		return nil
	}

	var autocompleteAPI *config.API
	argName := strings.Replace(arg.Name, "=", "", -1)
	relatedNoun := argName
	switch {
	case argName == "id" || argName == "ids":
		// Heuristic: user is trying to autocomplete for id/ids arg for a list API
		relatedNoun = apiFound.Noun
		if apiFound.Verb != "list" {
			relatedNoun += "s"
		}
	case argName == "account":
		// Heuristic: user is trying to autocomplete for accounts
		relatedNoun = "accounts"
	case argName == "ipaddressid":
		// Heuristic: user is trying to autocomplete for ip addresses
		relatedNoun = "publicipaddresses"
	case argName == "storageid":
		relatedNoun = "storagepools"
	case argName == "associatednetworkid":
		relatedNoun = "networks"
	default:
		// Heuristic: autocomplete for the arg for which a list<Arg without id/ids>s API exists
		// For example, for zoneid arg, listZones API exists
		cutIdx := len(argName)
		if strings.HasSuffix(argName, "id") {
			cutIdx -= 2
		} else if strings.HasSuffix(argName, "ids") {
			cutIdx -= 3
		} else {
		}
		relatedNoun = argName[:cutIdx] + "s"
	}

	config.Debug("Possible related noun for the arg: ", relatedNoun, " and type: ", arg.Type)
	autocompleteAPI = findAPI(apiMap, relatedNoun)

	if autocompleteAPI == nil {
		if strings.Contains(strings.ToLower(relatedNoun), "storage") {
			relatedNoun = "storagepools"
			autocompleteAPI = findAPI(apiMap, relatedNoun)
		}
	}

	if autocompleteAPI != nil {
		config.Debug("Autocomplete: API found using heuristics: ", autocompleteAPI.Name)
	}

	if strings.HasSuffix(relatedNoun, "s") {
		relatedNoun = relatedNoun[:len(relatedNoun)-1]
	}

	// Heuristic: find any list API that contains the arg name
	if autocompleteAPI == nil {
		config.Debug("Finding possible API that have: ", argName, " related APIs: ", arg.Related)
		possibleAPIs := []*config.API{}
		for _, listAPI := range apiMap["list"] {
			if strings.Contains(listAPI.Noun, argName) {
				config.Debug("Found possible API: ", listAPI.Name)
				possibleAPIs = append(possibleAPIs, listAPI)
			}
		}
		if len(possibleAPIs) == 1 {
			autocompleteAPI = possibleAPIs[0]
		}
	}

	return autocompleteAPI
}