rsapi/commands.go
func (a *API) ShowActions(cmd, hrefPrefix string, values ActionCommands) error {
	res, _, err := a.parseResource(cmd, hrefPrefix, values)
	resource := ""
	if err == nil {
		resource = res.Name
	}
	if _, ok := a.Metadata[hrefPrefix]; ok {
		resource = hrefPrefix
	}
	resNames := make([]string, len(a.Metadata))
	actionsByRes := make(map[string][][]string)
	idx := 0
	for n := range a.Metadata {
		resNames[idx] = n
		idx++
	}
	sort.Strings(resNames)
	for _, resName := range resNames {
		actionsByRes[resName] = make([][]string, 0)
		res := a.Metadata[resName]
		if resource != "" && resName != resource {
			continue
		}
		for _, action := range res.Actions {
			for _, pattern := range action.PathPatterns {
				vars := pattern.Variables
				ivars := make([]interface{}, len(vars))
				for i, v := range vars {
					ivars[i] = interface{}(":" + v)
				}
				subPattern := pattern.Pattern
				pat := fmt.Sprintf(subPattern, ivars...)
				var ok bool
				pat, ok = shortenPattern(res, pat, "/actions/"+action.Name)
				if !ok {
					pat, _ = shortenPattern(res, pat, "/"+action.Name)
				}
				actionsByRes[resName] = append(actionsByRes[resName], []string{action.Name, pat})
			}
		}
	}
	var lines []string
	actionTitle := "Action"
	hrefTitle := "Href"
	resourceTitle := "Resource"
	maxActionLen := len(actionTitle)
	maxHrefLen := len(hrefTitle)
	maxResourceLen := len(resourceTitle)
	for res, actions := range actionsByRes {
		if len(res) > maxResourceLen {
			maxResourceLen = len(res)
		}
		for _, action := range actions {
			if len(action[0]) > maxActionLen {
				maxActionLen = len(action[0])
			}
			if len(action[1]) > maxHrefLen {
				maxHrefLen = len(action[1])
			}

		}
	}
	for idx, resName := range resNames {
		as := actionsByRes[resName]
		for i, action := range as {
			title := ""
			if i == 0 {
				title = resName
				if idx > 0 {
					lines = append(lines, fmt.Sprintf("%s\t%s\t%s",
						strings.Repeat("-", maxResourceLen),
						strings.Repeat("-", maxActionLen),
						strings.Repeat("-", maxHrefLen)))
				}
			}
			lines = append(lines, fmt.Sprintf("%s\t%s\t%s", title, action[0], action[1]))
		}
	}
	w := tabwriter.NewWriter(os.Stdout, 0, 4, 1, ' ', 0)
	w.Write([]byte(fmt.Sprintf("%s\t%s\t%s\n", resourceTitle, actionTitle, hrefTitle)))
	w.Write([]byte(fmt.Sprintf("%s\t%s\t%s\n", strings.Repeat("=", maxResourceLen),
		strings.Repeat("=", maxActionLen),
		strings.Repeat("=", maxHrefLen))))
	w.Write([]byte(strings.Join(lines, "\n")))
	w.Write([]byte("\n"))
	return w.Flush()
}
func (a *API) ShowActions(cmd, hrefPrefix string, values ActionCommands) error {
	res, _, err := a.parseResource(cmd, hrefPrefix, values)
	resource := ""
	if err == nil {
		resource = res.Name
	}
	if _, ok := a.Metadata[hrefPrefix]; ok {
		resource = hrefPrefix
	}
	resNames := make([]string, len(a.Metadata))
	actionsByRes := make(map[string][][]string)
	idx := 0
	for n := range a.Metadata {
		resNames[idx] = n
		idx++
	}
	sort.Strings(resNames)
	for _, resName := range resNames {
		actionsByRes[resName] = make([][]string, 0)
		res := a.Metadata[resName]
		if resource != "" && resName != resource {
			continue
		}
		for _, action := range res.Actions {
			for _, pattern := range action.PathPatterns {
				vars := pattern.Variables
				ivars := make([]interface{}, len(vars))
				for i, v := range vars {
					ivars[i] = interface{}(":" + v)
				}
				subPattern := pattern.Pattern
				pat := fmt.Sprintf(subPattern, ivars...)
				var ok bool
				pat, ok = shortenPattern(res, pat, "/actions/"+action.Name)
				if !ok {
					pat, _ = shortenPattern(res, pat, "/"+action.Name)
				}
				actionsByRes[resName] = append(actionsByRes[resName], []string{action.Name, pat})
			}
		}
	}
	var lines []string
	actionTitle := "Action"
	hrefTitle := "Href"
	resourceTitle := "Resource"
	maxActionLen := len(actionTitle)
	maxHrefLen := len(hrefTitle)
	maxResourceLen := len(resourceTitle)
	for res, actions := range actionsByRes {
		if len(res) > maxResourceLen {
			maxResourceLen = len(res)
		}
		for _, action := range actions {
			if len(action[0]) > maxActionLen {
				maxActionLen = len(action[0])
			}
			if len(action[1]) > maxHrefLen {
				maxHrefLen = len(action[1])
			}

		}
	}
	for idx, resName := range resNames {
		as := actionsByRes[resName]
		for i, action := range as {
			title := ""
			if i == 0 {
				title = resName
				if idx > 0 {
					lines = append(lines, fmt.Sprintf("%s\t%s\t%s",
						strings.Repeat("-", maxResourceLen),
						strings.Repeat("-", maxActionLen),
						strings.Repeat("-", maxHrefLen)))
				}
			}
			lines = append(lines, fmt.Sprintf("%s\t%s\t%s", title, action[0], action[1]))
		}
	}
	w := tabwriter.NewWriter(os.Stdout, 0, 4, 1, ' ', 0)
	w.Write([]byte(fmt.Sprintf("%s\t%s\t%s\n", resourceTitle, actionTitle, hrefTitle)))
	w.Write([]byte(fmt.Sprintf("%s\t%s\t%s\n", strings.Repeat("=", maxResourceLen),
		strings.Repeat("=", maxActionLen),
		strings.Repeat("=", maxHrefLen))))
	w.Write([]byte(strings.Join(lines, "\n")))
	w.Write([]byte("\n"))
	return w.Flush()
}
// ShowActions prints all known actions for an API or the selected resource if any.