glide
项目地址
- 主页
下面多是从 github 的 readme 中翻译的。
原理
扫描项目或库的源码来检测需要的依赖。为了检测版本和位置,Glide需要一个glide.yaml
文件。 依赖的包会统计imports来扫描它的依赖。如果依赖的项目也包含glide.yaml
文件,将使用该文件帮助检测依赖规则。Godep, GB, GOM, GPM的配置也导进来了。 依赖会被导出到vendor/
目录,这样go工具能够找到并使用它们。 glide.lock
文件中包含所有的依赖,包括依赖的依赖。 glide init
命令用来设置一个新项目。glide update
通过扫描和规则重新生成依赖版本。glide install
会安装列举在glide.lock
中的版本而不进行扫描,除非glide.lock
文件没找到。
安装
在 Mac 或者 Linux 上最简单的安装最新版的脚本是:
curl https://glide.sh/get | sh
在 Mac 上也可以通过 Homebrew 安装:
$ brew install glide
在 Ubuntu Precise(12.04), Trusty (14.04), Wily (15.10) 或 Xenial (16.04) 上:
sudo add-apt-repository ppa:masterminds/glide && sudo apt-get updatesudo apt-get install glide
适用于 Mac, Linux, Windows。
开发版通过 go get github.com/Masterminds/glide
获得源码。
使用
$ glide create # Start a new workspace$ open glide.yaml # and edit away!$ glide get github.com/Masterminds/cookoo # Get a package and add to glide.yaml$ glide install # Install packages and dependencies# work, work, work$ go build # Go tools work normally$ glide up # Update to newest versions of the package
glide create(又名init)
初始化一个新的工作区。创建一个glide.yaml
文件并写入猜测的包和版本。例如,如果你的项目用的是Godep,那就用Godep指定的版本。Glide足够聪明,能够扫描你的代码库并检测导入的包,不管你是不是用其他包管理工具指定的。
$ glide create[INFO] Generating a YAML configuration file and guessing the dependencies[INFO] Attempting to import from other package managers (use --skip-import to skip)[INFO] Scanning code to look for dependencies[INFO] --> Found reference to github.com/Masterminds/semver[INFO] --> Found reference to github.com/Masterminds/vcs[INFO] --> Found reference to github.com/codegangsta/cli[INFO] --> Found reference to gopkg.in/yaml.v2[INFO] Writing configuration file (glide.yaml)[INFO] Would you like Glide to help you find ways to improve your glide.yaml configuration?[INFO] If you want to revisit this step you can use the config-wizard command at any time.[INFO] Yes (Y) or No (N)?n[INFO] You can now edit the glide.yaml file. Consider:[INFO] --> Using versions and ranges. See https://glide.sh/docs/versions/[INFO] --> Adding additional metadata. See https://glide.sh/docs/glide.yaml/[INFO] --> Running the config-wizard command to improve the versions in your configuration
这里说的 config-wizard
(配置向导)可以现在运行,也可以以后运行,它能帮助你指出你可以使用的依赖包的版本范围。
glide config-wizard
这个命令运行一个向导,它会扫描你的依赖并从中检索信息,提出建议,你可以交互式的选择。例如,它可以发现一个依赖包使用了语义版本,你可以选择要使用的版本范围。
glide get [package name]
你可以通过 glide get
下载一个或多个包到你的 vendor
目录,并添加到你的 glide.yaml
文件中:
glide get github.com/Masterminds/cookoo
这个命令会深入列出的包并通过Godep, GPM, Gom, 和 GB 的配置文件获取它的依赖。 可以在get时指定版本或分支或版本范围:
glide get github.com/a/b#masterglide get github.com/a/b#^1.2.3
关于版本号的说明可以参考:
glide update(又名up)
下载或更新所有列在 glide.yaml
文件中的库,并放到 vendor
目录下。它会递归遍历依赖的包并拉取所需要的和从配置中读取的任何东西。
$ glide up
它会递归遍历其他包,从中寻找使用Glide, Godep, gb, gom, 和 GPM 管理的其他项目,一旦找到,这些包将会按需安装。 当依赖使用了特定的版本时, glide.lock
文件会被创建或更新。例如,如果在 glide.yaml
中一个版本号使用的是一个范围(比如 ^1.2.3
),那么在 glide.lock
中就会被设置为特定的 commit id。这允许可重现的安装(见 glide install
)。 从拉取的包中移除任何递归的 vendor
目录的话见 -v
标记。
glide install
当你想从 glide.lock
文件中安装指定版本时,使用 glide install
。
$ glide install
这会读取 glide.lock
文件,然后安装 commit id 指定的版本。 当 glide.lock
和 glide.yaml
不同步时,比如有一个更改,这个命令会发出警告。 更改依赖树时,运行 glide up
会重建 glide.lock
文件。 如果没有 glide.lock
文件,那么 glide install
会表现的像 update
,生成一个 lock 文件。 从拉取的包中移除任何递归的 vendor
目录的话见 -v
标记。
glide novendor (别名 nv)
当你运行 go test ./...
这样的命令时它会遍历所有的子目录包括 vendor。你可能只想测试你自己的项目,而不是依赖的项目。这就是 novendor
的意义所在,它可以列举出除了 vendor 的其他所有目录。
$ go test $(glide novendor)
这个命令可以在你项目的所有目录上运行 go test
除了 vendor 目录。
glide name
当你使用glide进行脚本编程时,有种场景是你需要知道你工作的包的名称。glide name
返回列举在 glide.yaml
中的包名。
glide tree
Glide 中包含一些检查代码并给出导入详情的命令,glide tree
就是其中之一,运行它给出的数据像这样:
$ glide treegithub.com/Masterminds/glide github.com/Masterminds/cookoo (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/cookoo) github.com/Masterminds/cookoo/io (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/cookoo/io) github.com/Masterminds/glide/cmd (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/cmd) github.com/Masterminds/cookoo (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/cookoo) github.com/Masterminds/cookoo/io (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/cookoo/io) github.com/Masterminds/glide/gb (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/gb) github.com/Masterminds/glide/util (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/util) github.com/Masterminds/vcs (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/vcs) github.com/Masterminds/glide/yaml (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/yaml) github.com/Masterminds/glide/util (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/util) github.com/Masterminds/vcs (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/vcs) github.com/Masterminds/vcs (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/vcs) gopkg.in/yaml.v2 (/Users/mfarina/Code/go/src/gopkg.in/yaml.v2) github.com/Masterminds/semver (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/semver) github.com/Masterminds/vcs (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/vcs) github.com/codegangsta/cli (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/codegangsta/cli) github.com/codegangsta/cli (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/codegangsta/cli) github.com/Masterminds/cookoo (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/cookoo) github.com/Masterminds/cookoo/io (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/cookoo/io) github.com/Masterminds/glide/gb (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/gb) github.com/Masterminds/glide/util (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/util) github.com/Masterminds/vcs (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/vcs) github.com/Masterminds/glide/yaml (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/yaml) github.com/Masterminds/glide/util (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/util) github.com/Masterminds/vcs (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/vcs) github.com/Masterminds/vcs (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/vcs) gopkg.in/yaml.v2 (/Users/mfarina/Code/go/src/gopkg.in/yaml.v2) github.com/Masterminds/semver (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/semver) github.com/Masterminds/vcs (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/Masterminds/vcs) github.com/codegangsta/cli (/Users/mfarina/Code/go/src/github.com/Masterminds/glide/vendor/github.com/codegangsta/cli)
它展示了一颗导入树,不包括核心库。 这个命令被废弃了,不久会删除
glide list
这个命令会展示项目导入的所有包的列表,以字母顺序。
$ glide listINSTALLED packages: vendor/github.com/Masterminds/cookoo vendor/github.com/Masterminds/cookoo/fmt vendor/github.com/Masterminds/cookoo/io vendor/github.com/Masterminds/cookoo/web vendor/github.com/Masterminds/semver vendor/github.com/Masterminds/vcs vendor/github.com/codegangsta/cli vendor/gopkg.in/yaml.v2
glide help
打印帮助信息。
$ glide help
glide --version
打印版本信息。
$ glide --versionglide version 0.12.0
glide.yaml
详细文档见