LoginSignup
262
223

More than 5 years have passed since last update.

glide - パッケージ管理のお困りの方へ -

Last updated at Posted at 2015-12-07
注意:Goがversion1.5以上じゃないと使えません

glideってなんぞ?

glideはGo1.5以降で使用可能なvendoring機能を利用した。
パッケージ管理ツールです。
(正確にはvendor管理)

glideを使うと下記のようにvendorにプロジェクトに必要なパッケージがインストールされ、そのプロジェクト内でのみ利用が可能となります。
(通常のgo get, go installのようにグローバル管理ではない)

- $GOPATH/src/myProject (プロジェクトのディレクトリ)
  |
  |-- glide.yaml
  |
  |-- main.go
  |
  |-- mySubpackage (サブパッケージ)
  |    |
  |    |-- foo.go
  |
  |-- vendor (この配下がglideで作成されるもの)
       |-- github.com
            |
            |-- Masterminds
                  |
                  |-- ... etc.

使いはじめた理由

実は、使いはじめた理由はパッケージ管理ではなく、プライベートリポジトリをきちんと管理したかったから。
同じようにgithubとかstackoverflowとか見てるとどうも皆さんお困りの様子。
中には黒魔術的な方法で回避してたりと...
でいろいろ探してたら、glideの情報を見つけ、これで解決できるのでは!!
と、実際やってみたら見事にできました。
っていうのが 出会い です。

導入方法

Mac

1. glideをインストール

$ brew install glide

2. 環境変数を変更

環境変数でGO15VENDOREXPERIMENT=1になっていなかったら
以下を.bashrcなどに追加して、反映させる

export GO15VENDOREXPERIMENT=1

※1.6でつかわなくてOKってことだけど、buildでコケることがあるらしいので、おまじない的に残しておいたほうがいいのかも...
http://yoru9zine.hatenablog.com/entry/2016/02/02/054922

Macじゃない

1. glideをインストール

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

2. 環境変数を変更

以下を.bashrcなどに追加して、反映させる

export GO15VENDOREXPERIMENT=1

使い方

1. glide.ymlを作成する

glide create

2. glide.ymlを更新する

下記の様な感じで、記載していきます。
package で管理したいパッケージを指定し、
repoを同じ階層で指定するとリポジトリをgo getの取得ルールに従わずに指定のリポジトリから取得するようにできます。
repo指定するときはvcsも指定しないと怒られます。
versionもいっしょに記載するとバージョン指定もできます。

glide.ymlの例
package: github.com/Masterminds/glide
import:
  - package: github.com/Masterminds/semver
  - package: github.com/Masterminds/cookoo
    vcs: git
    version: ^1.2.0
    repo: git@github.com:Masterminds/cookoo.git

3. パッケージをインストールする

installコマンドとupdateコマンドがありますが、
updateだけで事足ります。

install
$ glide install
update
$ glide up

きっかけはプライベートリポジトリの問題解決

プライベートリポジトリを解決するには?

bitbucketやstashなど、go getのルールにマッチしていないURLのサービスがあり、特にプライベートリポジトリなんかは特に厄介です。

glideでこれを解決するにはどうするか?
簡単です!! ( ー`дー´)キリッ

リポジトリを指定するだけです。

へっ? ( ゚д゚)ポカーン

リポジトリを指定するだけです。

package: main
import:
  - package: bitbucket.org/foo/bar
    vcs: git
    repo: git@bitbucket.org:foo/bar.git

これでglide upしたら入ります。

。゚(゚´Д`゚)゚。 (全gopherが泣いた)

Glide使ってる場合の全階層Unitテスト

Golangで全階層でUnitテストかける場合、 go test ./... っていうことをしますが、
glideが入ってる場合、これだとvendorの中までかかってしまい散々なことになります。
なので、みんなコマンド作ってaliasにしたり、shell作ったりと必死こいてるわけですが、
実は下記のようにしたらvendor以外をテストできます。
(いつの間にやら公式に載ってた)

go test $(glide novendor)

novendor はvendorいがいのパッケージを一覧表示するものです。

邪道かも?でも便利な使い方

リポジトリを変えた時に 一時的にですが!! 影響範囲がわかんないから、パッケージ名をそのままにしたい時ありますよね?

そんな時もパッケージ指定で解決します。

package: main
import:
  - package: bitbucket.org/foo/bar
    vcs: git
    repo: git@github.com:foo/bar.git

もう、glide使ってない人が不憫でしょうがない...

あってよかった、list表示

プロジェクトで参照しているパッケージを一覧を表示することができます。
しかも、vendorgopathかも判定してくれる。(嬉しい)

$ glide list
github.com/Masterminds/cookoo (Location: vendored)
github.com/Masterminds/cookoo/io (Location: vendored)
github.com/Masterminds/glide/cmd (Location: gopath)
github.com/Masterminds/glide/gb (Location: gopath)
github.com/Masterminds/glide/util (Location: gopath)
github.com/Masterminds/glide/yaml (Location: gopath)
github.com/Masterminds/semver (Location: vendored)
github.com/Masterminds/vcs (Location: vendored)
github.com/codegangsta/cli (Location: vendored)
gopkg.in/yaml.v2 (Location: vendored)

意外と便利、tree表示

tree コマンドなくなるっぽい (´;ω;`)ブワッ
=> 残留決まったらしいですヽ(=´▽`=)ノ

プロジェクトの依存パッケージを見たい時ってありますよね?
えっ、ない?
いや、あるって!!

そんな時に便利なtree表示のコマンドがglideにはあります。

$ glide tree
github.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使っていないっていうのは2016年までだよね?
っていう記事が出回る前に皆さん使いましょう。

262
223
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
262
223