X Tutup
Skip to content

Add support for command extensions#1037

Closed
mislav wants to merge 2 commits intotrunkfrom
custom-commands
Closed

Add support for command extensions#1037
mislav wants to merge 2 commits intotrunkfrom
custom-commands

Conversation

@mislav
Copy link
Copy Markdown
Contributor

@mislav mislav commented May 28, 2020

When executing gh <cmd>, if <cmd> is not a built-in command, this searches ~/.config/gh/gh-commands/* and in PATH for executables named gh-<cmd> and runs the first one found.

All arguments are forwarded to the executable and the following environment variables are set:

  • GITHUB_TOKEN for API calls;
  • GH_BASEREPO (if available) with the <owner>/<repo> pair determined from git remotes.

Example: command to identify the authenticated user

#!/bin/bash
exec gh api user | jq -r .login

If saved as gh-whoami somewhere in the user's PATH, the command is invoked like so:

$ gh whoami
mislav

All custom commands are listed in shell completions for the gh command; e.g. typing gh who<Tab> will expand to gh whoami.

Additionally, the executable may provide its own shell completions by respecting when the 1st argument is __complete and writing completion options as separate lines of output:

#!/bin/bash
set -e

if [ "${1:-}" = "__complete" ]; then
  echo --some-flag
  echo --another-flag
  exit 0
fi

# ...the rest of the implementation

TODO:

  • provide a command to easily install other person's command extensions with
  • provide a command to update previously downloaded extensions
  • handle case in which multiple commands of the same name are found under ~/.config/gh/gh-commands/*
  • avoid asking for shell completions if the executable doesn't look like it implements __complete
  • fix gh __complete <cmd> '' returning extra results
  • write more example commands

mislav added 2 commits May 28, 2020 18:37
When executing `gh <cmd>`, if `<cmd>` is not a built-in command, this
searches `~/.config/gh/gh-commands/*` and in PATH for executables named
`gh-<cmd>` and runs the first one found.

All arguments are forwarded to the executable and the following
environment variables are set:

- GITHUB_TOKEN for API calls;
- GH_BASEREPO with the `OWNER/REPO` pair determined from git remotes.

Additionally, the executable may provide shell completions by respecting
the mode in which the 1st argument is `__complete` and by writing
completion options as separate lines of output.
If multiple extensions provide a `gh-foo` command, invoking `gh foo`
will result in an error. The user will have to specify the extension
namespace explicitly, e.g.

    $ gh @user1/foo
    $ gh @user2/foo
@mislav
Copy link
Copy Markdown
Contributor Author

mislav commented May 29, 2020

Some example commands are now in https://github.com/mislav/gh-commands. Installing that repository per its README will grant you the gh extensions command that lets you manage extensions installed from other people. (Although, nobody except me will currently have any extensions to install. 🙈)

Commands provided there:

  • gh ci-status <pr> - show CI statuses for a pull request
  • gh comments <issue> - show the comment thread for an issue
  • gh secrets add <name> - add a repository secret via standard input

Base automatically changed from bump-dependencies to trunk June 2, 2020 08:36
@klits111
Copy link
Copy Markdown

hoop dit gaan goed en aie dankie.

@vilmibm vilmibm added the hack label Aug 19, 2020
@vilmibm
Copy link
Copy Markdown
Contributor

vilmibm commented Sep 8, 2020

this is ✨ but until we plan to merge and support I'm going to close this PR for book keeping purposes.

@vilmibm vilmibm closed this Sep 8, 2020
@mislav mislav deleted the custom-commands branch June 3, 2021 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

X Tutup