Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upgh alias set #970
gh alias set #970
Conversation
|
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=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:
|
| // 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.
This comment has been minimized.
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 monaThis 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.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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.
This comment has been minimized.
vilmibm
May 27, 2020
•
Author
Member
the error is from alias set; not pr list. i get the same result with issue list:
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?
|
|
||
| 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.
This comment has been minimized.
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.
This comment has been minimized.
|
This is great! I can't wait to use it for real!
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 |
| // 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.
This comment has been minimized.
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.
This comment has been minimized.
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 | ||
| } |
This comment has been minimized.
This comment has been minimized.
|
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 |




vilmibm commentedMay 19, 2020
•
edited
This PR adds
gh alias set.what this PR does
$1)aliasessection to our config which is created on first use ofalias setwhat this PR doesn't do
considerations
I did two things I'm nervous about in this PR.
main.goto initialize aContext, check the config, and rewriteargvif an alias is found.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 (egalias set il 'issue list --label="bug"'). Disabling flag processing for this command fixed the problem but if we ever wantalias setto 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)
todotodidalias set(though it might be fine to ship initially)closes #938