Debian Codegraph @

README.md

raw symbols SCIP
1 # bingo
2
3 [![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/bwplotka/bingo) [![Latest Release](https://img.shields.io/github/release/bwplotka/bingo.svg?style=flat-square)](https://github.com/bwplotka/bingo/releases/latest) [![CI](https://github.com/bwplotka/bingo/workflows/go/badge.svg)](https://github.com/bwplotka/bingo/actions?query=workflow%3Ago) [![Go Report Card](https://goreportcard.com/badge/github.com/bwplotka/bingo)](https://goreportcard.com/report/github.com/bwplotka/bingo) [![Slack](https://img.shields.io/badge/join%20slack-%23bingo-brightgreen.svg)](https://gophers.slack.com/)
4
5 `go get` like, simple CLI that allows automated versioning of Go package level binaries (e.g required as dev tools by your project!) built on top of Go Modules, allowing reproducible dev environments.
6
7 [![Demo](examples/bingo-demo.gif)](examples/)
8
9 ## Features
10
11 From our experience all repositories and projects require some tools and binaries to be present on the machine to be able to perform various development operations like building, formatting, releasing or static analysis. For smooth development all such tools should be pinned to a certain version and bounded to the code commits there were meant to be used against.
12
13 Go modules does not aim to solve this problem, and even if they will do at some point it will not be on the package level, which makes it impossible to e.g pin minor version `X.Y.0` of package `module1/cmd/abc` and version `X.Z.0` of `module1/cmd/def`.
14
15 At the end `bingo`, has following features:
16
17 * It allows maintaining separate, hidden, nested Go modules for Go buildable packages you need **without obfuscating your own module or worrying with tool's cross dependencies**!
18 * Package level versioning, which allows versioning different (or the same!) package multiple times from a single module in different versions.
19 * Works also for non-Go projects. It only requires the tools to be written in Go.
20 * No need to install `bingo` in order to **use** pinned tools. This avoids the "chicken & egg" problem. You only need `go build`.
21 * Easy upgrade, downgrade, addition, and removal of the needed binary's version, with no risk of dependency conflicts.
22 * NOTE: Tools are **often** not following semantic versioning, so `bingo` allows to pin by commit ID.
23 * Immutable binary names. This creates a reliable way for users and CIs to use expected version of the binaries, reinstalling on-demand only if needed.
24 * Works with all buildable Go projects, including pre Go modules and complex projects with complex directives like `replace`, `retract` or `exclude` statements. (e.g Prometheus)
25 * Optional, automatic integration with Makefiles.
26
27 You can read full a story behind `bingo` [in this blog post](https://www.bwplotka.dev/2020/bingo/).
28
29 ## Requirements
30
31 * Go 1.24+
32 * Linux or MacOS (Want Windows support? [Helps us out](https://github.com/bwplotka/bingo/issues/26))
33 * All tools that you wish to "pin" have to be built in Go (they don't need to use Go modules at all).
34
35 ## Installing
36
37 In your repository (does not need to be a Go project)
38
39 ```shell
40 go install github.com/bwplotka/bingo@latest
41 ```
42
43 > For [go version before 1.17](https://go.dev/doc/go-get-install-deprecation) use `go get github.com/bwplotka/bingo` instead.
44
45 Recommended: Ideally you want to pin `bingo` tool to the single version too (inception!). Do it via:
46
47 ```shell
48 bingo get -l github.com/bwplotka/bingo
49 ```
50
51 ## Usage
52
53 ### `go get` but for binaries!
54
55 The key idea is that you can manage your tools similar to your Go dependencies via `go get`:
56
57 ```shell
58 bingo get [<package or binary>[@version1 or none,version2,version3...]]
59 ```
60
61 For example:
62
63 * `bingo get github.com/fatih/faillint`
64 * `bingo get github.com/fatih/faillint@latest`
65 * `bingo get github.com/fatih/[email protected]`
66 * `bingo get github.com/fatih/[email protected],v1.5.0`
67
68 After this, make sure to commit `.bingo` directory in git repository, so the tools will stay versioned! Once pinned, anyone can install correct version of the tool with correct dependencies by either doing:
69
70 ```bash
71 bingo get <tool>
72 ```
73
74 For example `bingo get faillint`
75
76 ... or without `bingo`:
77
78 ```bash
79 go build -mod=mod -modfile .bingo/<tool>.mod -o=$GOBIN/<tool>-<version>
80 ```
81
82 For example `go build -mod=mod -modfile .bingo/faillint.mod -o=$GOBIN/faillint-v1.5.0`
83
84 `bingo` allows to easily maintain a separate, nested Go Module for each binary. By default, it will keep it `.bingo/<tool>.mod` This allows to correctly pin the binary without polluting the main go module or other's tool module.
85
86 ### Using Installed Tools
87
88 `bingo get` builds pinned tool or tools in your `$GOBIN` path. Binaries have a name following `<provided-tool-name>-<version>` pattern. So after installation you can do:
89
90 * From shell:
91
92 ```bash
93 ${GOBIN}/<tool>-<version> <args>
94 ```
95
96 For example: `${GOBIN}/faillint-v1.5.0`
97
98 While it's not the easiest for humans to read or type, it's essential to ensure your scripts use pinned version instead of some non-deterministic "latest version".
99
100 > NOTE: If you use `-l` option, bingo creates symlink to <tool> . Use it with care as it's easy to have side effects by having another binary with same name e.g on CI.
101
102 `bingo` does not have `run` command [(for a reason)](https://github.com/bwplotka/bingo/issues/52), it provides useful helper variables for script or adhoc use:
103
104 > NOTE: Below helpers makes it super easy to install or use pinned binaries without even installing `bingo` (it will use just `go build`!) 💖
105
106 * From shell:
107
108 ```bash
109 source .bingo/variables.env
110 ${<PROVIDED_TOOL_NAME>} <args>
111 ```
112
113 * From Makefile:
114
115 ```Makefile
116 include .bingo/Variables.mk
117 run:
118 $(<PROVIDED_TOOL_NAME>) <args>
119 ```
120
121 ### Real life examples!
122
123 Let's show a few, real, sometimes novel examples showcasing `bingo` capabilities:
124
125 1. [`golangci-lint`](https://github.com/golangci/golangci-lint) is all-in-one lint framework. It's important to pin it on CI so CI runs are reproducible no matter what new linters are added, removed or changed in new release. Let's pin it to `v1.35.2` and use path recommended by [golangci-lint install docs](https://golangci-lint.run/welcome/install/#install-from-source): `github.com/golangci/golangci-lint/cmd/golangci-lint` (funny enough they discourage `go get` exactly because of the lack of pinning features `bingo` have!)
126
127 ```shell
128 bingo get github.com/golangci/golangci-lint/cmd/[email protected]
129 ```
130
131 This will pin to that commit and install `${GOBIN}/golangci-lint-v1.35.2`
132
133 2. It's very common in Go world to use `goimports`, popular `gofmt` replacement which formats Go code including imports. However, not many know that it's breaking compatibility a lot between versions (there are no releases). If you want to assert certain formatting of the Go code in the CI etc your only option is to pin `goimports` version. You can do it via `bingo get`:
134
135 ```shell
136 bingo get golang.org/x/tools/cmd/goimports@latest
137 ```
138
139 This will install (at the time of writing) latest binary: `${GOBIN}/goimports-v0.0.0-20210112230658-8b4aab62c064`
140
141 3. You rather like older formatting? No issue, let's downgrade. Since `goimports` was already installed you can reference it by just `goimports`. Let's pick the commit we want e.g `e64124511800702a4d8d79e04cf6f1af32e7bef2`:
142
143 ```shell
144 bingo get goimports@e64124511800702a4d8d79e04cf6f1af32e7bef2
145 ```
146
147 This will pin to that commit and install `${GOBIN}/goimports-v0.0.0-20200519204825-e64124511800`
148
149 4. Installing (and pinning) multiple versions:
150
151 ```shell
152 bingo get goimports@e64124511800702a4d8d79e04cf6f1af32e7bef2,v0.0.0-20200601175630-2caf76543d99,af9456bb636557bdc2b14301a9d48500fdecc053
153 ```
154
155 This will pin and install three versions of goimports. Very useful to compatibility testing.
156
157 5. Updating to the current latest:
158
159 ```shell
160 bingo get goimports@latest
161 ```
162
163 This will find the latest module version, pin and install it.
164
165 6. Listing binaries you have pinned:
166
167 ```shell
168 bingo list
169 ```
170
171 7. Unpinning `goimports` totally from the project:
172
173 ```shell
174 bingo get goimports@none
175 ```
176
177 > PS: `go get` also allows `@none` suffix! Did you know? I didn't (:*
178
179 8. Installing all tools:
180
181 ```shell
182 bingo get
183 ```
184
185 9. **Bonus**: Have you ever dreamed to pin command from bigger project like... `thanos`? I was. Can you even install it using Go tooling? Let's try:
186
187 ```shell
188 go get github.com/thanos-io/thanos/cmd/[email protected]
189 # Output: go: cannot use path@version syntax in GOPATH mode
190 ```
191
192 Ups you cannot use this in non-Go project at all... (: Let's create setup go mod and retry:
193
194 ```shell
195 go mod init _
196 # Output: go: creating new go.mod: module
197 go get github.com/thanos-io/thanos/cmd/[email protected]
198 # go get github.com/thanos-io/thanos/cmd/[email protected]
199 # go: downloading github.com/thanos-io/thanos v0.17.2
200 # go: found github.com/thanos-io/thanos/cmd/thanos in github.com/thanos-io/thanos v0.17.2
201 # go get: github.com/thanos-io/[email protected] requires
202 # github.com/cortexproject/[email protected] requires
203 # github.com/thanos-io/[email protected] requires
204 # github.com/cortexproject/[email protected] requires
205 # github.com/thanos-io/[email protected] requires
206 # github.com/cortexproject/[email protected] requires
207 # github.com/thanos-io/[email protected] requires
208 # github.com/cortexproject/[email protected] requires
209 # github.com/thanos-io/[email protected] requires
210 # github.com/cortexproject/[email protected] requires
211 # github.com/prometheus/[email protected] requires
212 # github.com/prometheus/[email protected] requires
213 # k8s.io/[email protected]+incompatible: reading https://proxy.golang.org/k8s.io/client-go/@v/v12.0.0+incompatible.mod: 410 Gone
214 # server response: not found: k8s.io/[email protected]+incompatible: invalid version: +incompatible suffix not allowed: module contains a go.mod file, so semantic import versioning is required
215 ```
216
217 The reasoning is complex but [TL;DR: Go Modules are just sometimes hard to be properly used for some projects](https://twitter.com/bwplotka/status/1347104281120403458). This is why bigger projects like `Kubernetes`, `Prometheus` or `Thanos` has to use `replace` statements (plus others like `exclude` or `retract`). To make this `go get` work we would need to manually craft `replace` statements in our own go `mod` file. But what if we don't want to do that or don't know how or simply we want to install pinned version of Thanos locally without having Go project? Just use bingo:
218
219 ```shell
220 bingo get github.com/thanos-io/thanos/cmd/[email protected]
221 ${GOBIN}/thanos-v0.17.2 --help
222 ```
223
224 ## Advanced Techniques
225
226 * Using advanced go build flags and environment variables.
227
228 To tell bingo to use certain env vars and tags during build time, just add them as a comment to the go.mod file manually and do `bingo get`. Done!
229
230 NOTE: Order of comment matters. First bingo expects relative package name (optional), then environment variables, then flags. All space delimited.
231
232 Real example from production project that relies on extended Hugo.
233
234 ```text
235 module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT
236
237 go 1.16
238
239 require github.com/gohugoio/hugo v0.83.1 // CGO_ENABLED=1 -tags=extended
240 ```
241
242 Run `bingo list` to see if build options are parsed correctly. Run `bingo get` to install all binaries including the modified one with new build flags.
243
244 ## Production Usage
245
246 To see production example see:
247
248 * [bingo's own tools](https://github.com/bwplotka/bingo/tree/master/.bingo)
249 * [Thanos's tools](https://github.com/thanos-io/thanos/tree/7bf3b0f8f3af57ac3aef033f6efb58860f273c78/.bingo)
250 * [go-grpc-middleware's tools](https://github.com/grpc-ecosystem/go-grpc-middleware/tree/5b83c99199db53d4258b05646007b48e4658b3af/.bingo)
251
252 ## Contributing
253
254 Any contributions are welcome! Just use GitHub Issues and Pull Requests as usual. We follow [Thanos Go coding style](https://thanos.io/tip/contributing/coding-style-guide.md/) guide.
255
256 See an extensive and up-to-date description of the `bingo` usage below:
257
258 ## Command Help
259
260 ```bash mdox-exec="bingo --help" mdox-expect-exit-code=2
261 bingo: 'go get' like, simple CLI that allows automated versioning of
262 Go package level binaries (e.g required as dev tools by your project!)
263
264 built on top of Go Modules, allowing reproducible dev environments.
265 'bingo' allows to easily maintain a separate, nested Go Module for each binary.
266
267 For detailed examples and documentation see: https://github.com/bwplotka/bingo
268
269 Usage:
270 bingo [command]
271
272 Commands:
273 completion Generate the autocompletion script for the specified shell
274 get add development tools to the current project (e.g: bingo get github.com/fatih/faillint@latest)
275 list List enumerates all or one binary that are/is currently pinned in this project.
276 version Prints bingo Version.
277
278 Options:
279 -h, --help help for bingo
280 -m, --moddir string Directory where separate modules for each binary will be maintained.
281 Feel free to commit this directory to your VCS to bond binary versions to your project code.
282 If the directory does not exist bingo logs and assumes a fresh project. (default ".bingo")
283 -v, --verbose Print more
284
285 Use "bingo [command] --help" for more information about a command.
286 ```
287
288 ## Initial Author
289
290 [@bwplotka](https://bwplotka.dev) inspired by [Paul's](https://github.com/myitcv) research and with a bit of help from [Duco](https://github.com/Helcaraxan) (: