glide從入門到使用教程

qiangmzsx發表於2017-07-19

介紹

不論是開發 Java 還是你正在學習的 Golang,都會遇到依賴管理問題。Java 有牛逼轟轟的 Maven 和 Gradle。 Golang 亦有 godep、govendor、glide、gvt、gopack 等等,本文主要給大家介紹gilde。 glide 是 Golang 的包管理工具,是為了解決 Golang 依賴問題的。 為什麼需要 glide? 原因很簡單,Go 語言原生包管理的缺陷。羅列一下 golang 的 get 子命令管理依賴有很多大缺陷:

  • 能拉取原始碼的平臺很有限,絕大多數依賴的是 github.com
  • 不能區分版本,以至於令開發者以最後一項包名作為版本劃分
  • 依賴 列表/關係 無法持久化到本地,需要找出所有依賴包然後一個個 go get
  • 只能依賴本地全域性倉庫(GOPATH/GOROOT),無法將庫放置於區域性倉庫($PROJECT_HOME/vendor)

安裝

Golang 環境設定

採用 vendor 目錄特性,Go 1.5 做為試驗特性加入(需要指定 GO15VENDOREXPERIMENT=1 環境變數),並在 Go 1.6 正式引入的一個概念。多數 go 依賴解決方案都基於它的。GO15VENDOREXPERIMENT 是 Go 1.5 版本新增的一個環境變數,如果將值改為 1 則表示啟用。它可以將專案根目錄名為 vendor 的目錄新增到 Go 的庫搜尋路徑中,實現一個區域性依賴的效果。
特性在 1.5 版本作為實驗特性被新增,1.6 中預設被啟用,1.7 移除變數加入標準中。
Go 提供了原始的 go get ,讓第三方包管理可以基於 go get 做擴充套件。GO15VENDOREXPERIMENT 特性讓區域性依賴成為現實。Go 官方在給第三方包管理營造條件以及引導開發者使用者至所推薦的方向,促進社群的繁榮。證明了一個語言技術的生態不僅僅只能靠官方或者取決於官方的完善程度。

//設定環境變數 使用vendor目錄
GO15VENDOREXPERIMENT=1

為什麼要選擇 glide? Glide 是眾多實現 GO15VENDOREXPERIMENT 特性的包管理工具之一,但它是本文最為推薦的,具體為什麼推薦它,原因很簡單,因為它目前最受關注。 幾大主要功能:

  • 持久化依賴列表至配置檔案中,包括依賴版本(支援範圍限定)以及私人倉庫等
  • 持久化關係樹至 lock 檔案中(類似於 yarn 和 cargo),以重複拉取相同版本依賴
  • 相容 go get 所支援的版本控制系統:Git, Bzr, HG, and SVN
  • 支援 GO15VENDOREXPERIMENT 特性,使得不同專案可以依賴相同專案的不同版本
  • 可以匯入其他工具配置,例如: Godep, GPM, Gom, and GB

安裝 glide

$ go get github.com/Masterminds/glide
$ go install github.com/Masterminds/glide

驗證

$ glide
NAME:
   glide - Vendor Package Management for your Go projects.

   Each project should have a 'glide.yaml' file in the project directory. Files
   look something like this:

       package: github.com/Masterminds/glide
       imports:
       - package: github.com/Masterminds/cookoo
         version: 1.1.0
       - package: github.com/kylelemons/go-gypsy
         subpackages:
         - yaml

   For more details on the 'glide.yaml' files see the documentation at
   https://glide.sh/docs/glide.yaml


USAGE:
   glide [global options] command [command options] [arguments...]

VERSION:
   0.13.0-dev

COMMANDS:
     create, init       Initialize a new project, creating a glide.yaml file
     config-wizard, cw  Wizard that makes optional suggestions to improve config in a glide.yaml file.
     get                Install one or more packages into `vendor/` and add dependency to glide.yaml.
     remove, rm         Remove a package from the glide.yaml file, and regenerate the lock file.
     import             Import files from other dependency management systems.
     name               Print the name of this project.
     novendor, nv       List all non-vendor paths in a directory.
     rebuild            Rebuild ('go build') the dependencies
     install, i         Install a project's dependencies
     update, up         Update a project's dependencies
     tree               (Deprecated) Tree prints the dependencies of this project as a tree.
     list               List prints all dependencies that the present code references.
     info               Info prints information about this project
     cache-clear, cc    Clears the Glide cache.
     about              Learn about Glide
     mirror             Manage mirrors
     help, h            Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --yaml value, -y value  Set a YAML configuration file. (default: "glide.yaml")
   --quiet, -q             Quiet (no info or debug messages)
   --debug                 Print debug verbose informational messages
   --home value            The location of Glide files (default: "/home/users/qiangmzsx/.glide") [$GLIDE_HOME]
   --tmp value             The temp directory to use. Defaults to systems temp [$GLIDE_TMP]
   --no-color              Turn off colored output for log messages
   --help, -h              show help
   --version, -v           print the version

看到這樣,那就恭喜你,已經安裝成功了!!!

使用

篇幅有限,我只介紹經常使用到的。 先進入在 GOPATH 的一個專案中。

cd $GOPATH/src/foor

初始化 (glide init)

$ glide init
[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/urfave/cli
[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)?
Y
[INFO]  Loading mirrors from mirrors.yaml file
[INFO]  Looking for dependencies to make suggestions on
[INFO]  --> Scanning for dependencies not using version ranges
[INFO]  --> Scanning for dependencies using commit ids
[INFO]  Gathering information on each dependency
[INFO]  --> This may take a moment. Especially on a codebase with many dependencies
[INFO]  --> Gathering release information for dependencies
[INFO]  --> Looking for dependency imports where versions are commit ids
[INFO]  Here are some suggestions...
[INFO]  The package github.com/urfave/cli appears to have Semantic Version releases (http://semver.org).
[INFO]  The latest release is v1.19.1. You are currently not using a release. Would you like
[INFO]  to use this release? Yes (Y) or No (N)
Y
[INFO]  Would you like to remember the previous decision and apply it to future
[INFO]  dependencies? Yes (Y) or No (N)
Y
[INFO]  Updating github.com/urfave/cli to use the release v1.19.1 instead of no release
[INFO]  The package github.com/urfave/cli appears to use semantic versions (http://semver.org).
[INFO]  Would you like to track the latest minor or patch releases (major.minor.patch)?
[INFO]  Tracking minor version releases would use '>= 1.19.1, < 2.0.0' ('^1.19.1'). Tracking patch version
[INFO]  releases would use '>= 1.19.1, < 1.20.0' ('~1.19.1'). For more information on Glide versions
[INFO]  and ranges see https://glide.sh/docs/versions
[INFO]  Minor (M), Patch (P), or Skip Ranges (S)?
P
[INFO]  Would you like to remember the previous decision and apply it to future
[INFO]  dependencies? Yes (Y) or No (N)
Y
[INFO]  Updating github.com/urfave/cli to use the range ~1.19.1 instead of commit id v1.19.1
[INFO]  Configuration changes have been made. Would you like to write these
[INFO]  changes to your configuration file? Yes (Y) or No (N)
Y
[INFO]  Writing updates to configuration file (glide.yaml)
[INFO]  You can now edit the glide.yaml file.:
[INFO]  --> For more information on versions and ranges see https://glide.sh/docs/versions/
[INFO]  --> For details on additional metadata see https://glide.sh/docs/glide.yaml/
$ ll
glide.yaml
$ cat glide.yaml 
package: foor
import: []

在初始化過程中, glide 會詢問一些問題。 glide.yaml 記載了依賴包的列表及其更新規則,每次執行 glide up 時,都會按照指定的規則(如只下載補丁 (patch) 不下載升級 (minor))下載新版。

一個完整的 gilde.yaml

package: foor
homepage: https://github.com/qiangmzsx
license: MIT
owners: 
- name: qiangmzsx
  email: qiangmzsx@hotmail.com
  homepage: https://github.com/qiangmzsx
# 去除包
ignore:
- appengine
- golang.org/x/net
# 排除目錄
excludeDirs:
- node_modules
# 匯入包
import:
- package: github.com/astaxie/beego
  version: 1.8.0
- package: github.com/coocood/freecache
- package: github.com/garyburd/redigo/redis
- package: github.com/go-sql-driver/mysql
- package: github.com/bitly/go-simplejson
- package: git.oschina.net/qiangmzsx/beegofreecache
testImport:
- package: github.com/smartystreets/goconvey
  subpackages:
  - convey

很多人看著 yaml 很不習慣,沒事,我轉一下 json 給大家看看。

{
  "excludeDirs": [
    "node_modules"
  ], 
  "owners": [
    {
      "homepage": "https://github.com/qiangmzsx", 
      "name": "qiangmzsx", 
      "email": "qiangmzsx@hotmail.com"
    }
  ], 

  "license": "MIT", 
  "package": "foor", 
  "ignore": [
    "appengine", 
    "golang.org/x/net"
  ], 
  "import": [
    {
      "version": "1.8.0", 
      "package": "github.com/astaxie/beego"
    }, 
    {
      "package": "github.com/coocood/freecache"
    }, 
    {
      "package": "github.com/garyburd/redigo/redis"
    }, 
    {
      "package": "github.com/go-sql-driver/mysql"
    }, 
    {
      "package": "github.com/bitly/go-simplejson"
    }, 
    {
      "package": "git.oschina.net/qiangmzsx/beegofreecache"
    }
  ], 
  "testImport": [
    {
      "subpackages": [
        "convey"
      ], 
      "package": "github.com/smartystreets/goconvey"
    }
  ], 
  "homepage": "https://github.com/qiangmzsx"
}

版本號指定規則

=: equal (aliased to no operator)
!=: not equal
>: greater than
<: less than
>=: greater than or equal to
<=: less than or equal to

1.2 - 1.4.5 which is equivalent to >= 1.2, <= 1.4.5
2.3.4 - 4.5 which is equivalent to >= 2.3.4, <= 4.5
1.2.x is equivalent to >= 1.2.0, < 1.3.0

>= 1.2.x is equivalent to >= 1.2.0
<= 2.x is equivalent to < 3
* is equivalent to >= 0.0.0

~1.2.3 is equivalent to >= 1.2.3, < 1.3.0
~1 is equivalent to >= 1, < 2
~2.3 is equivalent to >= 2.3, < 2.4
~1.2.x is equivalent to >= 1.2.0, < 1.3.0
~1.x is equivalent to >= 1, < 2

^1.2.3 is equivalent to >= 1.2.3, < 2.0.0
^1.2.x is equivalent to >= 1.2.0, < 2.0.0
^2.3 is equivalent to >= 2.3, < 3
^2.x is equivalent to >= 2.0.0, < 3

''指定版本報錯,需要用''指定的可以不填寫

安裝依賴 (glide install)

glide.yaml 我們已經準備好了,現在就改安裝一下試試。

$ glide install
[ERROR] Failed to parse /home/users/xxxx/golang/src/foor/glide.yaml: yaml: invalid leading UTF-8 octet

報錯了!別擔心看看你的 yaml 檔案是否為 utf-8 編碼,不是就轉換一下就好啦!

$ glide install
[INFO]  Lock file (glide.lock) does not exist. Performing update.
[INFO]  Downloading dependencies. Please wait...
[INFO]  --> Fetching updates for github.com/go-sql-driver/mysql
[INFO]  --> Fetching updates for github.com/astaxie/beego
[INFO]  --> Fetching updates for github.com/coocood/freecache
[INFO]  --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO]  --> Fetching updates for github.com/bitly/go-simplejson
[INFO]  --> Fetching updates for github.com/garyburd/redigo
[INFO]  --> Fetching updates for github.com/smartystreets/goconvey
[INFO]  --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.0
[INFO]  Resolving imports
[INFO]  Downloading dependencies. Please wait...
[INFO]  Setting references for remaining imports
[INFO]  Exporting resolved dependencies...
[INFO]  --> Exporting github.com/astaxie/beego
[INFO]  --> Exporting github.com/coocood/freecache
[INFO]  --> Exporting github.com/bitly/go-simplejson
[INFO]  --> Exporting github.com/go-sql-driver/mysql
[INFO]  --> Exporting github.com/garyburd/redigo
[INFO]  --> Exporting github.com/smartystreets/goconvey
[INFO]  --> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO]  Replacing existing vendor dependencies
[INFO]  Project relies on 6 dependencies.
$ ll
total 12
glide.lock
glide.yaml
vendor
$ ll vendor/
git.oschina.net
github.com

看到 glide.look 了嗎,這個檔案記載了依賴包確定的 revision, 下次再執行 glide install 時,會直接讀這個檔案下載確定的版本。

升級版本 (glide up)

glide up 會按照語義化版本規則更新依賴包程式碼,開發過程中如果需要使用新版程式碼,可以執行這個命令: 修改一下 glide.yaml 中的一個 Package.

- package: github.com/astaxie/beego
  version: 1.8.3

執行 glide up。

$ glide up
[INFO]  Downloading dependencies. Please wait...
[INFO]  --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO]  --> Fetching updates for github.com/garyburd/redigo
[INFO]  --> Fetching updates for github.com/go-sql-driver/mysql
[INFO]  --> Fetching updates for github.com/astaxie/beego
[INFO]  --> Fetching updates for github.com/bitly/go-simplejson
[INFO]  --> Fetching updates for github.com/coocood/freecache
[INFO]  --> Fetching updates for github.com/smartystreets/goconvey
[INFO]  --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3
[INFO]  Resolving imports
[INFO]  Downloading dependencies. Please wait...
[INFO]  Setting references for remaining imports
[INFO]  Exporting resolved dependencies...
[INFO]  --> Exporting github.com/astaxie/beego
[INFO]  --> Exporting github.com/bitly/go-simplejson
[INFO]  --> Exporting github.com/garyburd/redigo
[INFO]  --> Exporting github.com/go-sql-driver/mysql
[INFO]  --> Exporting github.com/coocood/freecache
[INFO]  --> Exporting github.com/smartystreets/goconvey
[INFO]  --> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO]  Replacing existing vendor dependencies
[INFO]  Project relies on 6 dependencies.

新增並下載依賴 (glide get)

除了自動從程式碼中解析 import 外,glide 還可以通過 glide get 直接下載程式碼中沒有的依賴,與 go get 的用法基本一致:

$ glide get github.com/orcaman/concurrent-map
[INFO]  Preparing to install 1 package.
[INFO]  Attempting to get package github.com/orcaman/concurrent-map
[INFO]  --> Gathering release information for github.com/orcaman/concurrent-map
[INFO]  --> Adding github.com/orcaman/concurrent-map to your configuration
[INFO]  Downloading dependencies. Please wait...
[INFO]  --> Fetching updates for github.com/garyburd/redigo
[INFO]  --> Fetching updates for github.com/astaxie/beego
[INFO]  --> Fetching updates for github.com/go-sql-driver/mysql
[INFO]  --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO]  --> Fetching updates for github.com/bitly/go-simplejson
[INFO]  --> Fetching github.com/orcaman/concurrent-map
[INFO]  --> Fetching updates for github.com/coocood/freecache
[INFO]  --> Fetching updates for github.com/smartystreets/goconvey
[INFO]  Resolving imports
[INFO]  Downloading dependencies. Please wait...
[INFO]  --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3
[INFO]  Exporting resolved dependencies...
[INFO]  --> Exporting github.com/smartystreets/goconvey
[INFO]  --> Exporting github.com/garyburd/redigo
[INFO]  --> Exporting github.com/go-sql-driver/mysql
[INFO]  --> Exporting github.com/orcaman/concurrent-map
[INFO]  --> Exporting github.com/astaxie/beego
[INFO]  --> Exporting github.com/bitly/go-simplejson
[INFO]  --> Exporting github.com/coocood/freecache
[INFO]  --> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO]  Replacing existing vendor dependencies

使用映象 (glide mirror)

[WARN]  Unable to checkout golang.org/x/crypto
[ERROR] Update failed for golang.org/x/crypto: Cannot detect VCS
[ERROR] Failed to do initial checkout of config: Cannot detect VCS

這幾行資訊估計很多人都是遇到過的。在我天朝或者在公司內部都可能不能訪問一些站點,導致很 Golang 的依賴包不能通過 go get 下載。此時也就是 glide 大發神威的時候到了,可以通過配置將牆了的版本庫 URL 對映到沒被牆的 URL,甚至也可以對映到本地版本庫。 將 golang.org 對映到 github: 修改 glide.yaml 加入

- package: golang.org/x/crypto

如果你的網路可以訪問就不需要使用 glide 映象功能,可以跳過。

$ glide mirror set golang.org/x/crypto github.com/golang/crypto
[INFO]  golang.org/x/crypto being set to github.com/golang/crypto
[INFO]  mirrors.yaml written with changes
$ glide up
[INFO]  Loading mirrors from mirrors.yaml file
[INFO]  Downloading dependencies. Please wait...
[INFO]  --> Fetching updates for github.com/orcaman/concurrent-map
[INFO]  --> Fetching golang.org/x/crypto
[INFO]  --> Fetching updates for github.com/astaxie/beego
[INFO]  --> Fetching updates for github.com/go-sql-driver/mysql
[INFO]  --> Fetching updates for github.com/garyburd/redigo
[INFO]  --> Fetching updates for github.com/coocood/freecache
[INFO]  --> Fetching updates for github.com/bitly/go-simplejson
[INFO]  --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO]  --> Fetching updates for github.com/smartystreets/goconvey
[INFO]  --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3
[INFO]  Resolving imports
[INFO]  Downloading dependencies. Please wait...
[INFO]  Setting references for remaining imports
[INFO]  Exporting resolved dependencies...
[INFO]  --> Exporting github.com/astaxie/beego
[INFO]  --> Exporting github.com/coocood/freecache
[INFO]  --> Exporting github.com/smartystreets/goconvey
[INFO]  --> Exporting github.com/garyburd/redigo
[INFO]  --> Exporting github.com/go-sql-driver/mysql
[INFO]  --> Exporting github.com/bitly/go-simplejson
[INFO]  --> Exporting github.com/orcaman/concurrent-map
[INFO]  --> Exporting golang.org/x/crypto
[INFO]  --> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO]  Replacing existing vendor dependencies
[INFO]  Project relies on 8 dependencies.
$ ll vendor/
git.oschina.net
github.com
golang.org

終於看到 golang.org 啦!!! 細心的你一定已經發現了

[INFO]  mirrors.yaml written with changes

說明執行glide mirror時候映象配置寫入到的是$HOME/.glide/mirrors.yaml中,開啟看看。

repos:
- original: golang.org/x/crypto
  repo: github.com/golang/crypto

還可以對映到本地目錄。 推薦大家可以去https://www.golangtc.com/download/package下載很多 Golang 類庫。 現在我去下載了:https://www.golangtc.com/static/download/packages/golang.org.x.text.tar.gz,解壓到本地目錄/home/users/qiangmzsx/var/golang/golang.org/x/text

$ glide mirror set golang.org/x/text /home/users/qiangmzsx/var/golang/golang.org/x/text
[INFO]  golang.org/x/text being set to /home/users/qiangmzsx/var/golang/golang.org/x/text
[INFO]  mirrors.yaml written with changes
$ glide up
[INFO]  Loading mirrors from mirrors.yaml file
[INFO]  Downloading dependencies. Please wait...
[INFO]  --> Fetching golang.org/x/text
[INFO]  --> Fetching updates for github.com/garyburd/redigo
[INFO]  --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO]  --> Fetching updates for github.com/astaxie/beego
[INFO]  --> Fetching updates for github.com/bitly/go-simplejson
[INFO]  --> Fetching updates for github.com/go-sql-driver/mysql
[INFO]  --> Fetching updates for github.com/coocood/freecache
[INFO]  --> Fetching updates for github.com/orcaman/concurrent-map
[INFO]  --> Fetching updates for golang.org/x/crypto
[INFO]  --> Fetching updates for github.com/smartystreets/goconvey
[INFO]  --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3
[INFO]  Resolving imports
[INFO]  Downloading dependencies. Please wait...
[INFO]  Setting references for remaining imports
[INFO]  Exporting resolved dependencies...
[INFO]  --> Exporting github.com/astaxie/beego
[INFO]  --> Exporting github.com/go-sql-driver/mysql
[INFO]  --> Exporting github.com/bitly/go-simplejson
[INFO]  --> Exporting github.com/coocood/freecache
[INFO]  --> Exporting github.com/smartystreets/goconvey
[INFO]  --> Exporting github.com/garyburd/redigo
[INFO]  --> Exporting github.com/orcaman/concurrent-map
[INFO]  --> Exporting golang.org/x/text
[INFO]  --> Exporting golang.org/x/crypto
[INFO]  --> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO]  Replacing existing vendor dependencies
[INFO]  Project relies on 9 dependencies.

全域性選項

執行 glide,在最後就可以看到

GLOBAL OPTIONS:
   --yaml value, -y value  Set a YAML configuration file. (default: "glide.yaml")
   --quiet, -q             Quiet (no info or debug messages)
   --debug                 Print debug verbose informational messages
   --home value            The location of Glide files (default: "/home/users/qiangmzsx/.glide") [$GLIDE_HOME]
   --tmp value             The temp directory to use. Defaults to systems temp [$GLIDE_TMP]
   --no-color              Turn off colored output for log messages
   --help, -h              show help
   --version, -v           print the version

如果大家想把 glide 的 yaml 檔案換別的預設名稱可以執行

$ glide -y qiangmzsx.yaml

在官網中會看到一個GLIDE_HOME變數,該變數就是/home/users/qiangmzsx/.glide。 這個目錄之前有提到過,除了包含有 mirrors.yaml 還有一個很重要的目錄cache本地 cache,每次更新程式碼時, glide 都會在本地儲存 cache,以備下次 glide install 使用 。

GLIDE_HOME可以通過如下命令修改。

$ glide  --home /home/glide 

總結

除了上述說到的功能,glide 還有很多好的功能,後續有機會在寫出來吧。
總結一下,glide 是一款功能豐富,完全滿足需求的依賴管理工具,強烈大家使用。

更多原創文章乾貨分享,請關注公眾號
  • glide從入門到使用教程
  • 加微信實戰群請加微信(註明:實戰群):gocnio

相關文章