You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{% data reusables.cli.cli-extensions %} For more information about how to use {% data variables.product.prodname_cli %} extensions, see "[Using {% data variables.product.prodname_cli %} extensions](/github-cli/github-cli/using-github-cli-extensions)."
16
16
17
-
You need a repository for each extension that you create. The repository name must start with `gh-`. The rest of the repository name is the name of the extension. At the root of the repository, there must be an executable file with the same name as the repository. This file will be executed when the extension is invoked.
17
+
You need a repository for each extension that you create. The repository name must start with `gh-`. The rest of the repository name is the name of the extension. The repositorymust have an executable file at its root with the same name as the repository or a set of precompiled binary executables attached to a release.
18
18
19
19
{% note %}
20
20
21
-
**Note**: We recommend that the executable file is a bash script because bash is a widely available interpreter. You may use non-bash scripts, but the user must have the necessary interpreter installed in order to use the extension.
21
+
**Note**: When relying on an executable script, we recommend using a bash script because bash is a widely available interpreter. You may use non-bash scripts, but the user must have the necessary interpreter installed in order to use the extension. If you would prefer to not rely on users having interpreters installed, consider a precompiled extension.
22
22
23
23
{% endnote %}
24
24
25
-
## Creating an extension with `gh extension create`
25
+
## Creating an interpreted extension with `gh extension create`
26
+
27
+
{% note %}
28
+
29
+
**Note**: Running `gh extension create` with no arguments will start an interactive wizard.
30
+
31
+
{% endnote %}
26
32
27
33
You can use the `gh extension create` command to create a project for your extension, including a bash script that contains some starter code.
28
34
@@ -34,7 +40,35 @@ You can use the `gh extension create` command to create a project for your exten
34
40
35
41
1. Follow the printed instructions to finalize and optionally publish your extension.
36
42
37
-
## Creating an extension manually
43
+
## Creating a precompiled extension in Go with `gh extension create`
44
+
45
+
You can use the `--precompiled=go` argument to create a Go-based project for your extension, including Go scaffolding, workflow scaffolding, and starter code.
46
+
47
+
1. Set up a new extension by using the `gh extension create` subcommand. Replace `EXTENSION-NAME` with the name of your extension and specify `--precompiled=go`.
1. Follow the printed instructions to finalize and optionally publish your extension.
54
+
55
+
## Creating a non-Go precompiled extension with `gh extension create`
56
+
57
+
You can use the `--precompiled=other` argument to create a project for your non-Go precompiled extension, including workflow scaffolding.
58
+
59
+
1. Set up a new extension by using the `gh extension create` subcommand. Replace `EXTENSION-NAME` with the name of your extension and specify `--precompiled=other`.
1. Add some initial code foryour extensionin your compiled language of choice.
66
+
67
+
1. Fill in`script/build.sh` with code to build your extension to ensure that your extension can be built automatically.
68
+
69
+
1. Follow the printed instructions to finalize and optionally publish your extension.
70
+
71
+
## Creating an interpreted extension manually
38
72
39
73
1. Create a local directory called `gh-EXTENSION-NAME`for your extension. Replace `EXTENSION-NAME` with the name of your extension. For example, `gh-whoami`.
40
74
@@ -56,7 +90,7 @@ You can use the `gh extension create` command to create a project for your exten
56
90
57
91
1. From your directory, install the extension as a local extension.
58
92
59
-
```bash
93
+
```shell
60
94
gh extension install .
61
95
```
62
96
@@ -70,13 +104,13 @@ You can use the `gh extension create` command to create a project for your exten
1. Optionally, to help other users discover your extension, add the repository topic `gh-extension`. This will make the extension appear on the [`gh-extension` topic page](https://github.com/topics/gh-extension). For more information about how to add a repository topic, see "[Classifying your repository with topics](/github/administering-a-repository/managing-repository-settings/classifying-your-repository-with-topics)."
78
112
79
-
## Tips for writing {% data variables.product.prodname_cli %} extensions
113
+
## Tips for writing interpreted {% data variables.product.prodname_cli %} extensions
80
114
81
115
### Handling arguments and flags
82
116
@@ -120,34 +154,128 @@ fi
120
154
121
155
### Calling core commands in non-interactive mode
122
156
123
-
Some {% data variables.product.prodname_cli %} core commands will prompt the user for input. When scripting with those commands, a prompt is often undesirable. To avoid prompting, supply the necessary information explicitly via arguments.
157
+
Some {% data variables.product.prodname_cli %} core commands will prompt the user for input. When scripting with those commands, a prompt is often undesirable. To avoid prompting, supply the necessary information explicitly via arguments.
124
158
125
159
For example, to create an issue programmatically, specify the title and body:
126
160
127
-
```bash
161
+
```shell
128
162
gh issue create --title "My Title" --body "Issue description"
129
163
```
130
164
131
165
### Fetching data programatically
132
166
133
167
Many core commands support the `--json` flag for fetching data programatically. For example, to return a JSON object listing the number, title, and mergeability status of pull requests:
134
-
```bash
168
+
169
+
```shell
135
170
gh pr list --json number,title,mergeStateStatus
136
171
```
137
172
138
173
If there is not a core command to fetch specific data from GitHub, you can use the [`gh api`](https://cli.github.com/manual/gh_api) command to access the GitHub API. For example, to fetch information about the current user:
139
-
```bash
174
+
175
+
```shell
140
176
gh api user
141
177
```
142
178
143
179
All commands that output JSON data also have options to filter that data into something more immediately usable by scripts. For example, to get the current user's name:
144
180
145
-
```bash
181
+
```shell
146
182
gh api user --jq '.name'
147
183
```
148
184
149
185
For more information, see [`gh help formatting`](https://cli.github.com/manual/gh_help_formatting).
150
186
187
+
## Creating a precompiled extension manually
188
+
189
+
1. Create a local directory called `gh-EXTENSION-NAME` for your extension. Replace `EXTENSION-NAME` with the name of your extension. For example, `gh-whoami`.
190
+
191
+
1. In the directory you created, add some source code. For example:
192
+
193
+
```go
194
+
package main
195
+
import (
196
+
"github.com/cli/go-gh"
197
+
"fmt"
198
+
)
199
+
200
+
func main() {
201
+
args := []string{"api", "user", "--jq", `"You are @\(.login) (\(.name))"` }
202
+
stdOut, _, err := gh.Exec(args...)
203
+
if err != nil {
204
+
fmt.Println(err)
205
+
return
206
+
}
207
+
fmt.Println(stdOut.String())
208
+
}
209
+
```
210
+
211
+
1. From your directory, install the extension as a local extension.
212
+
213
+
```shell
214
+
gh extension install .
215
+
```
216
+
217
+
1. Build your code. For example, with Go, replacing `YOUR-USERNAME` with your GitHub username:
218
+
219
+
```shell
220
+
go mod init github.com/<em>YOUR-USERNAME</em>/gh-whoami
221
+
go mod tidy
222
+
go build
223
+
```
224
+
225
+
1. Verify that your extension works. Replace `EXTENSION-NAME` with the name of your extension. For example, `whoami`.
226
+
227
+
```shell
228
+
gh <em>EXTENSION-NAME</em>
229
+
```
230
+
231
+
1. From your directory, create a repository to publish your extension. Replace `EXTENSION-NAME` with the name of your extension.
232
+
233
+
{% note %}
234
+
235
+
**Note:** Be careful not to commit the binary produced by your compilation step to version control.
1. Create a release to share your precompiled extension with others. Compile for each platform you want to support, attaching each binary to a release as an asset. Binary executables attached to releases must follow a naming convention and have a suffix of <em>OS-ARCHITECTURE\[EXTENSION\]</em>.
247
+
248
+
For example, an extension named `whoami` compiled for Windows 64bit would have the name `gh-whoami-windows-amd64.exe` while the same extension compiled for Linux 32bit would have the name `gh-whoami-linux-386`. To see an exhaustive list of OS and architecture combinations recognized by `gh`, see [this source code](https://github.com/cli/cli/blob/14f704fd0da58cc01413ee4ba16f13f27e33d15e/pkg/cmd/extension/manager.go#L696).
249
+
250
+
{% note %}
251
+
252
+
**Note:** For your extension to run properly on Windows, its asset file must have a `.exe` extension. No extension is needed for other operating systems.
253
+
254
+
{% endnote %}
255
+
256
+
Releases can be created from the command line. For example:
257
+
258
+
```shell
259
+
git tag v1.0.0
260
+
git push origin v1.0.0
261
+
GOOS=windows GOARCH=amd64 go build -o gh-<em>EXTENSION-NAME</em>-windows-amd64.exe
262
+
GOOS=linux GOARCH=amd64 go build -o gh-<em>EXTENSION-NAME</em>-linux-amd64
263
+
GOOS=darwin GOARCH=amd64 go build -o gh-<em>EXTENSION-NAME</em>-darwin-amd64
264
+
gh release create v1.0.0 ./*amd64*
265
+
266
+
1. Optionally, to help other users discover your extension, add the repository topic `gh-extension`. This will make the extension appear on the [`gh-extension` topic page](https://github.com/topics/gh-extension). For more information about how to add a repository topic, see "[Classifying your repository with topics](/github/administering-a-repository/managing-repository-settings/classifying-your-repository-with-topics)."
267
+
268
+
269
+
## Tips for writing precompiled {% data variables.product.prodname_cli %} extensions
270
+
271
+
### Automating releases
272
+
273
+
Consider adding the [gh-extension-precompile](https://github.com/cli/gh-extension-precompile) action to a workflow in your project. This action will automatically produce cross-compiled Go binaries for your extension and supplies build scaffolding for non-Go precompiled extensions.
274
+
275
+
### Using {% data variables.product.prodname_cli %} features from Go-based extensions
276
+
277
+
Consider using [go-gh](https://github.com/cli/go-gh), a Go library that exposes pieces of `gh` functionality for use in extensions.
278
+
151
279
## Next steps
152
280
153
281
To see more examples of {% data variables.product.prodname_cli %} extensions, look at [repositories with the `gh-extension` topic](https://github.com/topics/gh-extension).
0 commit comments