X Tutup
The Wayback Machine - https://web.archive.org/web/20200531231643/https://github.com/cli/cli/pull/1037
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

Add support for command extensions #1037

Draft
wants to merge 2 commits into
base: bump-dependencies
from
Draft

Conversation

@mislav
Copy link
Member

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
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
Member 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

1 participant
You can’t perform that action at this time.
X Tutup