golang依賴管理 glide

目錄

 

一、gilde簡介

二、glide命令簡介

三、gilde安裝配置

step1:下載安裝gilde

step2:使用gilde

step3:根據gilde安裝依賴到vendor

四、gilde官方bug

bug1:windows使用gilde get報錯

bug2:gilde install 或者 gilde get失敗

五、gilde常用命令

初始化gilde

升級依賴

安裝依賴到vendor

清理緩存

鏡像操作


一、gilde簡介

  1. glide是繼vendor之後的包管理工具,它的出現爲了解決vendor的不足,vendor作爲目錄管理不具備主動下載的功能,只能通過go get配合,並且不能指定版本以及不能區分版本。
  2. gilde的原理:掃描項目的依賴解析其依賴關係,並與gilde.yaml文件進行匹配,如果相匹配優先使用指定版本。
  3. gilde的特性:針對vendor進行了功能擴展,它提供了依賴解析和傳遞,並提供依賴下載,增加了依賴版本控制,鏡像源管理。
  4. gilde可以設置緩存,類似本地倉庫,位置在%userdir%/.gilde/cache/src 最外部有mirror.yaml來指定鏡像,gilde命令在獲取依賴包時,通過這個緩存進行加速。

二、glide命令簡介

##安裝gilde後

gilde --help
命令 描述 功能
create, init Initialize a new project, creating a glide.yaml file 初始化,生成glide.yaml文件
config-wizard, cw Wizard that makes optional suggestions to improve config in a glide.yaml file. 配置嚮導,提供glide.yaml的依賴可選項
get Install one or more packages into `vendor/` and add dependency to glide.yaml. 安裝一個依賴包到項目vendor文件夾並添加到gilde.yaml文件
remove, rm Remove a package from the glide.yaml file, and regenerate the lock file. 從gilde.yaml文件刪除一個依賴並且重新生成lock文件
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. 列出gilde.yaml的所有非vendor路徑。
rebuild Rebuild ('go build') the dependencies 重新build項目
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. 清除glide緩存。
about Learn about Glide 關於gilde
mirror Manage mirrors 鏡像管理
help, h Shows a list of commands or help for one command 幫助命令

 

三、gilde安裝配置

step1:下載安裝gilde

go get github.com/Masterminds/glide

ps:go get 相當於git clone與 go install 
其執行文件在GOPATH/bin/glide.exe 

等效:
使用git clone 將 github.com/Masterminds/glide 下載到GOPATH/src下
再進入GOPATH/src/github.com/Masterminds/glide 使用go install main.go

step2:使用gilde

%GOPATH%/src/gilde-demo           ##在此目錄下建立項目叫gilde-demo
%GOPATH%/src/gilde-demo/main.go   ##在項目目錄新建src文件夾並創建main.go
%GOPATH%/src/gilde-demo/vendor    ##在項目目錄新建vendor文件夾

##########################項目結構#########################
gilde-demo    
 ├─vendor
 └─main.go

##########################main.go ########################
package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	router := gin.Default()
	router.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Hello World")
	})
	router.Run(":8000")
}
cd /d %GOPATH%/src/glide-demo        ##切換項目根目錄
gilde init                           ##gilde.yaml文件

ps:初始化過程中會提示需不需要配置嚮導,可以使用N快速默認初始化


#####################gilde.yaml#####################################
package: glide-demo
import:
- package: github.com/gin-gonic/gin
  version: v1.4.0

gilde.yaml語言詳解

其中重要的
import:用來標記所有引用
package:引用的包名
version:包的版本


其中版本定義爲  va.b.c命名方式  a不兼容大版本 b功能迭代版本  cbug修復或小版本
版本約束規則如下:
=: equal (aliased to no operator)
!=: not equal
>: greater than
<: less than
>=: greater than or equal to
<=: less than or equal to

-標識範圍
*標識所有
~標識末位升級若干版本
^標識升級倒數第二位位若干版本
x標識x位不做要求

eg:
~1.2.x     等價  version>=1.2.0, <1.3.0
^1.2.3     等價  vserion>=1.2.3, <2.0.0
1.2 -1.3.4 等價 version>=1.2,<=1.3.4

step3:根據gilde安裝依賴到vendor

cd /d %GOPATH%/src/glide-demo        ##切換項目根目錄
gilde install                        ##根據gilde.yaml文件安裝依賴
ps:首次安裝依賴會檢查gilde.lock,下載gilde.yaml後生成gilde.lock

[INFO]  Lock file (glide.lock) does not exist. Performing update.
[INFO]  Downloading dependencies. Please wait...
[INFO]  --> Fetching github.com/gin-gonic/gin
[INFO]  --> Setting version for github.com/gin-gonic/gin to v1.4.0.
[INFO]  Resolving imports
[INFO]  --> Fetching github.com/gin-contrib/sse
[INFO]  --> Fetching github.com/mattn/go-isatty
[INFO]  --> Fetching github.com/golang/protobuf
[INFO]  --> Fetching github.com/ugorji/go
...安裝依賴時,每次都會下載到vendor目錄中,時間較長...

安裝結束後,vendor目錄有項目的所有依賴包

四、gilde官方bug

bug1:windows使用gilde get報錯

[ERROR] Unable to export dependencies to vendor directory: Error15      
moving files: exit status 1. output: Access is denied. 0 dir(s) moved.
原因:此bug由於windows操作系統與其他系統命令不一樣,官方bug,需要修改gilde源碼,重新編譯


step1:修改%GOPATH%\src\github.com\Masterminds\glide\path\winbug.go
====================================================================================
func CustomRename(o, n string) error {
	// Handking windows cases first
	if runtime.GOOS == "windows" {
		msg.Debug("Detected Windows. Moving files using windows command")
		//此處修改 cmd := exec.Command("cmd.exe", "/c", "move", o, n)
		cmd := exec.Command("cmd.exe", "/c", "xcopy /s/y", o, n+"\\")
		output, err := cmd.CombinedOutput()
		if err != nil {
			return fmt.Errorf("Error moving files: %s. output: %s", err, output)
		}
		return nil
	} else if detectWsl() {
		cmd := exec.Command("mv", o, n)
		output, err2 := cmd.CombinedOutput()
		msg.Debug("Detected Windows Subsystem for Linux. Removing files using subsystem command")
		if err2 != nil {
			return fmt.Errorf("Error moving files: %s. output: %s", err2, output)
		}
		return nil
	}
	return os.Rename(o, n)
}
=====================================================================================
step2:編譯安裝gilde

cd /d %GOPATH%\src\github.com\Masterminds\glide
go install glide.go

bug2:gilde install 或者 gilde get失敗

[INFO]  --> Fetching golang.org/x/sys/unix
[WARN]  Unable to checkout golang.org/x/sys/unix
[ERROR] Error looking for golang.org/x/sys/unix: Cannot detect VCS
[INFO]  --> Fetching updates for github.com/modern-go/concurrent
[INFO]  --> Fetching updates for github.com/modern-go/reflect2
[ERROR] Failed to retrieve a list of dependencies: Error resolving imports
原因:個別golang.org官網包被牆,需要設置代理

方法1:通過 glide mirror set https://golang.org/x/sys https://github.com/golang/sys --vcs git 設置鏡像


方法2: 設置代理 go evn -w GOPROXY=https://goproxy.io 此方法經常失效


方法3:(推薦使用)全局添加鏡像
step1:找到%GLIDE_HOME%,默認在C:\\Users\\mechrevo\\.glide
step2:修改mirror.yaml,添加鏡像:常用的crypto,net,sync,sys等

===========================================================================
repos:
- original: https://golang.org/x/crypto
  repo: https://github.com/golang/crypto
- original: https://golang.org/x/crypto/acme/autocert
  repo: https://github.com/golang/crypto
  base: golang.org/x/crypto
- original: https://golang.org/x/sys/unix
  repo: https://github.com/golang/sys
  base: golang.org/x/sys
- original: https://golang.org/x/net
  repo: https://github.com/golang/net
- original: https://golang.org/x/sync
  repo: https://github.com/golang/sync
- original: https://golang.org/x/tools
  repo: https://github.com/golang/tools
- original: https://golang.org/x/grpc
  repo: https://github.com/golang/grpc
- original: https://golang.org/x/time
  repo: https://github.com/golang/time
============================================================================

 

五、gilde常用命令

初始化gilde

cd /d %GOPATH%/src/glide-demo        ##切換項目根目錄
glide create  or glide init  

升級依賴

cd /d %GOPATH%/src/glide-demo        ##切換項目根目錄
glide update or glide up

安裝依賴到vendor

cd /d %GOPATH%/src/glide-demo        ##切換項目根目錄
glide install                        ##項目根目錄必須有vendor文件夾

清理緩存

glide cc          #清理C:\\Users\\mechrevo\\.glide\cache

鏡像操作

設置鏡像
glide mirror set [original] [replacement] 
glide mirror set [original] [replacement] --vcs [type]

#此操作等同修改 C:\\Users\\mechrevo\\.glide\mirror.yaml文件

    
移除鏡像
glide mirror remove [original]
     
獲取包的鏡像列表
glide mirror list

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章