Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completions for aliases #1128

Closed
vilmibm opened this issue Jun 8, 2020 · 4 comments
Closed

completions for aliases #1128

vilmibm opened this issue Jun 8, 2020 · 4 comments
Labels
core This issue is not accepting PRs from outside contributors enhancement a request to improve CLI

Comments

@vilmibm
Copy link
Contributor

vilmibm commented Jun 8, 2020

a user's aliases should be added to tab completions

@vilmibm vilmibm added the aliases label Jun 8, 2020
@mislav mislav added this to To do 📝 in The GitHub CLI Jun 10, 2020
@vilmibm vilmibm added the enhancement a request to improve CLI label Jun 12, 2020
@ampinsk ampinsk moved this from To do 📝 to Backlog 🗒 in The GitHub CLI Jun 29, 2020
@vilmibm vilmibm removed the aliases label Sep 29, 2020
@vilmibm vilmibm added the core This issue is not accepting PRs from outside contributors label Oct 7, 2020
@rsteube
Copy link
Contributor

rsteube commented Nov 6, 2020

Go this working in my fork by simply adding the aliases as subcommands before the completion generation:
https://github.com/rsteube/gh/blob/master/pkg/cmdutil/completion.go#L23

func Aliases() (map[string]string, error) {
	if config, err := config.ParseDefaultConfig(); err != nil {
		return nil, errors.New("failed to parse DefaultConfig:" + err.Error())
	} else {
		if aliasCfg, err := config.Aliases(); err != nil {
			return nil, errors.New("failed to load AliasCfg:" + err.Error())
		} else {
			aliases := make(map[string]string)
			for key, value := range aliasCfg.All() {
				aliases[key] = value
			}
			return aliases, nil
		}
	}
}

c.PreRun = func(cmd *cobra.Command, args []string) {
			if aliases, err := action.Aliases(); err == nil {
				for key, value := range aliases {
					cmd.Root().AddCommand(&cobra.Command{Use: key, Short: value, Run: func(cmd *cobra.Command, args []string) {}})
				}
			}
		}

@hjdivad
Copy link

hjdivad commented Nov 12, 2020

FWIW a very simple bash script to wrap the existing completion:

#!/usr/bin/env bash

_gh_completion_with_aliases() {
  # if gh call super and add extra
  # assumes gh completion has already been sourced
  __start_gh
  aliases=$(gh alias list | cut -d: -f 1)
  matching_aliases=$(compgen -W "$aliases" "${COMP_WORDS[1]}")
  COMPREPLY+=($matching_aliases)
}

complete -F _gh_completion_with_aliases gh

@lbesnard
Copy link

lbesnard commented Jul 1, 2021

@hjdivad

  aliases=$(gh alias list | cut -d: -f 1)

this is only good if aliases are one liner. Something along these lines is working better (but not perfect)

$(gh alias list | grep -o -e '^[a-z].*:' | cut -d : -f1)

@mislav
Copy link
Contributor

mislav commented Aug 12, 2021

Fixed in #3870

@mislav mislav closed this as completed Aug 12, 2021
The GitHub CLI automation moved this from Backlog 🗒 to Done 💤 Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core This issue is not accepting PRs from outside contributors enhancement a request to improve CLI
Projects
No open projects
The GitHub CLI
  
Done 💤
Development

No branches or pull requests

7 participants
@mislav @hjdivad @vilmibm @lbesnard @rsteube and others