golang执行并缓存go.mod中二进制文件的工具插件gomodrun的使用
golang执行并缓存go.mod中二进制文件的工具插件gomodrun的使用
简介
gomodrun是一个可以执行并缓存go.mod文件中二进制文件的工具。它使得在项目中版本化CLI工具(如golangci-lint
和ginkgo
)变得容易,这些工具的版本会锁定在go.mod文件中指定的版本。二进制文件会根据Go版本和包版本进行缓存。
示例
# 运行一个linter
gomodrun golangci-lint run
# 将JSON对象转换为Go结构体,正确地传递stdin
echo example.json | gomodrun gojson > example.go
# 指定包含go.mod和工具文件的替代根目录
gomodrun -r ./alternative-tools-dir golangci-lint run
# 清理.gomodrun文件夹中未使用的二进制文件
gomodrun --tidy
安装
从源码安装
curl -L "https://github.com/dustinblackman/gomodrun/archive/refs/heads/master.tar.gz" | tar zxvf - -C /tmp
cd /tmp/gomodrun-master/cmd/gomodrun
go install .
使用方法
gomodrun通过使用位于项目根目录的tools.go
文件(或其他任何名称)来工作,该文件包含你想要捆绑到go.mod
中的所有CLI依赖项。注意文件顶部需要// +build tools
,这允许你为工具文件命名任何你喜欢的名称。
tools.go
// +build tools
package myapp
import (
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
_ "github.com/onsi/ginkgo/ginkgo"
)
运行go build tools.go
将依赖项添加到你的go.mod
中。build预期会失败。
CLI
你可以通过在命令前加上gomodrun
来运行你的工具。二进制文件将被构建并缓存在项目根目录的.gomodrun
中,使第一次运行后的所有后续运行都变得快速。
gomodrun golangci-lint run
编程方式
你也可以将gomodrun作为库使用。
package main
import (
"os"
"github.com/dustinblackman/gomodrun"
)
func main() {
exitCode, err := gomodrun.Run("golangci-lint", []string{"run"}, &gomodrun.Options{
Stdin: os.Stdin,
Stdout: os.Stout,
Stderr: os.Stderr,
Env: os.Environ(),
PkgRoot: "",
})
}
许可证
MIT
更多关于golang执行并缓存go.mod中二进制文件的工具插件gomodrun的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang执行并缓存go.mod中二进制文件的工具插件gomodrun的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
使用gomodrun执行并缓存Go模块中的二进制工具
gomodrun是一个实用的Go工具,它允许你直接从项目的go.mod文件中运行二进制依赖项,而不需要手动安装这些工具到全局环境。它会自动下载并缓存这些工具,非常适合于项目特定的开发工具链管理。
安装gomodrun
首先需要全局安装gomodrun:
go install github.com/dustinblackman/gomodrun@latest
安装后,你可以直接使用gomodrun
命令,或者使用它的别名gmr
。
基本用法
假设你的go.mod文件中包含以下工具依赖:
require (
github.com/golangci/golangci-lint v1.50.1
github.com/securego/gosec/v2 v2.14.0
)
你可以这样运行这些工具:
# 运行golangci-lint
gomodrun golangci-lint run
# 运行gosec
gomodrun gosec ./...
gomodrun会自动下载这些工具(如果尚未缓存)并执行它们。
高级用法
1. 查看已安装的工具
gomodrun --list
这会显示go.mod中所有可用的二进制工具及其版本。
2. 清理缓存
gomodrun --clean
这会删除所有缓存的二进制文件。
3. 指定特定版本
如果你想运行特定版本的工具(而不是go.mod中指定的版本):
gomodrun golangci-lint@v1.40.0 run
4. 在Makefile中使用
lint:
gomodrun golangci-lint run
5. 在脚本中使用
#!/bin/bash
# 使用gomodrun运行工具
gomodrun gosec ./...
实际示例
假设我们有一个项目,需要使用以下工具:
- golangci-lint 用于代码检查
- gosec 用于安全检查
- mockgen 用于生成mock
go.mod文件部分内容:
require (
github.com/golangci/golangci-lint v1.50.1
github.com/securego/gosec/v2 v2.14.0
github.com/golang/mock v1.6.0
)
我们可以创建这样的Makefile:
.PHONY: lint test security mocks
lint:
gomodrun golangci-lint run
security:
gomodrun gosec ./...
generate-mocks:
gomodrun mockgen -destination=mocks/service.go -package=mocks github.com/your/pkg Service
为什么使用gomodrun
- 项目隔离:每个项目可以使用不同版本的工具,不会污染全局环境
- 版本控制:工具版本通过go.mod管理,与项目代码一起版本化
- 团队协作:确保团队成员使用完全相同的工具版本
- CI/CD友好:在CI环境中无需额外安装工具
替代方案
如果你不想使用gomodrun,也可以考虑:
- 直接使用
go run
:go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.1 run
- 使用
go install
安装工具到全局环境 - 使用类似工具如
task
或make
管理工具链
但是gomodrun提供了更简洁的语法和缓存机制,使得工作流程更加高效。
总结
gomodrun是一个简单但强大的工具,特别适合管理项目特定的开发工具链。它减少了"它在我的机器上能工作"的问题,使开发环境更加一致和可重现。