Visitar URL original
gh alias set by vilmibm · Pull Request #970 · cli/cli · GitHub
The Wayback Machine - https://web.archive.org/web/20200531231834/https://github.com/cli/cli/pull/970
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

gh alias set #970

Open
wants to merge 19 commits into
base: trunk
from
Open

gh alias set #970

wants to merge 19 commits into from

Conversation

@vilmibm
Copy link
Member

vilmibm commented May 19, 2020 •

This PR adds gh alias set.

what this PR does

  • allows creating aliases, including ones with placeholder arguments (e.g. $1)
  • validates that you can't set an alias for a non-existent command
  • adds a new top level aliases section to our config which is created on first use of alias set

what this PR doesn't do

  • allow for composition of commands (see #941 )
  • allow for deleting, listing, importing, nor exporting of aliases (see #936)

considerations

I did two things I'm nervous about in this PR.

  1. modified main.go to initialize a Context, check the config, and rewrite argv if an alias is found.
  2. disabled flag parsing for alias set. I encountered what I think is a bug where cobra would treat flags inside of single quoted strings as something it should parse (eg alias set il 'issue list --label="bug"'). Disabling flag processing for this command fixed the problem but if we ever want alias set to accept flags we'll need to revisit this.

I'm open to feedback on those things.

demo

(right click view image to make more legible)

aliasdemo

todo todid

  • add command
  • process args properly
  • expose aliases through config
  • error if alias already exist
  • initialize aliases on disk on first set
  • actual alias dispatching
  • error if expand to illegal command
  • documentation
  • finish tests
    • arg processing
    • already exists
    • illegal command
    • proper dispatching
    • lazy initialization on first set
  • clean up hacks
    • context handling (maybe it's fine?? initContext seems sufficiently idempotent)
    • disabling of flag processing on alias set (though it might be fine to ship initially)
    • more resilient expansion code

closes #938

@vilmibm vilmibm added this to Backlog 🗒 in The GitHub CLI via automation May 19, 2020
@vilmibm vilmibm moved this from Backlog 🗒 to In progress 🚧 in The GitHub CLI May 19, 2020
@vilmibm vilmibm force-pushed the alias-set branch from d6e3e3c to d5b7f05 May 22, 2020
@vilmibm vilmibm marked this pull request as ready for review May 22, 2020
@vilmibm vilmibm moved this from In progress 🚧 to Needs review 🤔 in The GitHub CLI May 22, 2020
@vilmibm vilmibm changed the title [WIP] gh alias set gh alias set May 22, 2020
@vilmibm vilmibm mentioned this pull request May 22, 2020
Copy link
Member

mislav left a comment •

I like the general approach taken here, but there are many edge cases that I feel should be addressed before shipping.

An idea: should we print information about alias expansion when DEBUG is active? This seems to me like a good use of debug mode; e.g.

$ DEBUG=1 gh pl foo
[pl foo] -> [pr list foo]
(...rest of output)

Additionally, I think that we should prevent recording and expanding an alias of the same name as a built-in CLI command. This is primarily for two reasons:

  1. Ease of debugging user problem reports: someone could report to us an error they are getting with gh pr create and we could spend time going back and forth with that user until we find out that the user has pr configured among their aliases that expands to something unexpected.

  2. Allowing our future commands to override people's aliases: it's possible that it would get popular that CLI users would share an alias named open within our community, but if ship a top-level command named open in a future release, we probably want our command to take precedence over the alias. Otherwise, our release notes are advertising gh open behavior that some users would not be getting because they have an active alias (and they might have forgotten about it).

cmd/gh/main.go Show resolved Hide resolved
command/root.go Show resolved Hide resolved
command/alias.go Outdated Show resolved Hide resolved
command/alias.go Outdated Show resolved Hide resolved
// TODO HACK: even when inside of a single-quoted string, cobra was noticing and parsing any flags
// used in an alias expansion string argument. Since this command needs no flags, I disabled their
// parsing. If we ever want to add flags to alias set we'll have to figure this out. I haven't
// checked if this is fixed in a new cobra.
Comment on lines +25 to +28

This comment has been minimized.

Copy link
@mislav

mislav May 25, 2020

Member

Cobra will only ever try to parse arguments as flags if an argument begins with a -- or -. With default Cobra config, this will work:

gh alias set pc 'pr create --assignee mona'

i.e. Cobra will not try to parse flags within the pr create --assignee mona. However, the quote-free version will fail:

gh alias set pc pr create --assignee mona

This is because all "words" are now passed as distinct arguments to alias set and one of them looks like a flag. To enable this use, I think that DisableFlagParsing is a good approach, but you are right that we will have to revisit this should we ever want to enable flags specifically for alias set.

This comment has been minimized.

Copy link
@vilmibm

vilmibm May 26, 2020

Author Member

gh alias set pc 'pr create --assignee mona' is exactly what didn't work, hence my saying "even when inside of a single-quoted string" and saying it seemed like a bug.

I understand how argument passing works :)

This comment has been minimized.

Copy link
@mislav

mislav May 27, 2020 •

Member

gh alias set pc 'pr create --assignee mona' is exactly what didn't work

I only brought up my points because I have first tried to reproduce this with DisableFlagParsing: false, but was not able to.

This comment has been minimized.

Copy link
@vilmibm

vilmibm May 27, 2020

Author Member

this is my experience with DisableFlagParsing unset:

image

This comment has been minimized.

Copy link
@probablycorey

probablycorey May 27, 2020

Collaborator

I had trouble reproducing this too. It might be that you’re trying to use the --author flag on pr list but it surprisingly doesn’t exist. I tried out these alias for issues and they worked:

ghd alias set foo ‘issue list —author=“probablycorey”’
ghd alias set foo2 ‘issue list —author=probablycorey’

This comment has been minimized.

Copy link
@vilmibm

vilmibm May 27, 2020 •

Author Member

the error is from alias set; not pr list. i get the same result with issue list:

image

I thought it was possibly something weird about my shell but I'm getting the same behavior in bash and zsh.

I noticed that your paste, corey, isn't using actual single quotes but rather the ‘ character. I tried that but it still fails for me; I wonder, however, if that character is treated different on macos?

command/root.go Outdated Show resolved Hide resolved
command/root.go Show resolved Hide resolved
command/root.go Outdated Show resolved Hide resolved
command/root.go Outdated Show resolved Hide resolved

func (c *fileConfig) parseAliasConfig(aliasesEntry *yaml.Node) (*AliasConfig, error) {
if aliasesEntry.Kind != yaml.MappingNode {
return nil, errors.New("expected aliases to be a map")

This comment has been minimized.

Copy link
@mislav

mislav May 25, 2020

Member

Could we also interpret a YAML null essentially as an empty mapping?

When experimenting with aliases, I've manually deleted alias entries in my config file, resulting in only this:

aliases:

the current alias-parsing code now aborts because aliases is not a map, but I think we should be permissive about this and interpret null as an empty map.

This comment has been minimized.

Copy link
@vilmibm

vilmibm May 27, 2020

Author Member

oops yeah, good catch. that shouldn't explode a config parsing.

@mislav mislav changed the base branch from master to trunk May 27, 2020
Copy link
Collaborator

probablycorey left a comment

This is great! I can't wait to use it for real!

An idea: should we print information about alias expansion when DEBUG is active? This seems to me like a good use of debug mode; e.g.

I really like this idea. I ran into a situation where I couldn’t figure out why an alias wouldn’t work and this would have helped!

One thing that surprised me: If I try to change an alias I got “alias foo already exists” I was expecting it to say something like “alias foo was ghd xyz but is now god abc

// TODO HACK: even when inside of a single-quoted string, cobra was noticing and parsing any flags
// used in an alias expansion string argument. Since this command needs no flags, I disabled their
// parsing. If we ever want to add flags to alias set we'll have to figure this out. I haven't
// checked if this is fixed in a new cobra.

This comment has been minimized.

Copy link
@probablycorey

probablycorey May 27, 2020

Collaborator

I had trouble reproducing this too. It might be that you’re trying to use the --author flag on pr list but it surprisingly doesn’t exist. I tried out these alias for issues and they worked:

ghd alias set foo ‘issue list —author=“probablycorey”’
ghd alias set foo2 ‘issue list —author=probablycorey’

}

if !validCommand(expansion) {
return fmt.Errorf("could not create alias: %s does not correspond to a gh command", utils.Bold(expansion))

This comment has been minimized.

Copy link
@probablycorey

probablycorey May 27, 2020

Collaborator

I like the "error on valid command" idea. I think @vilmibm's idea of warning on import would be an acceptable fix for the potential import problem.

func validCommand(expansion string) bool {
split, _ := shlex.Split(expansion)
cmd, _, err := RootCmd.Traverse(split)
return err == nil && cmd != RootCmd
}
Comment on lines +83 to +87

This comment has been minimized.

Copy link
@probablycorey

probablycorey May 27, 2020

Collaborator

This is easier than I thought it would be!

vilmibm added 14 commits May 27, 2020
@vilmibm
Copy link
Member Author

vilmibm commented May 28, 2020

I've caught up on all the feedback. The only thing that is giving me pause is I'm not pleased with my handling of the empty aliases key. I'll revisit that on Monday but it's possible that just leaving my kind of gross hack in place is enough for now.

@vilmibm vilmibm requested review from mislav and probablycorey May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
The GitHub CLI
  
Needs review 🤔
Linked issues

Successfully merging this pull request may close these issues.

3 participants
You can’t perform that action at this time.