golang依赖管理 glide

目录

一、glide简介

二、glide命令简介

三、glide安装配置

step1:下载安装glide

step2:   使用glide

step3:根据glide安装依赖到vendor

四、gilde官方bug

bug1:windows使用glide get报错

bug2:glide install 或者 glide get失败

五、glide常用命令

初始化glide

升级依赖

安装依赖到vendor

清理缓存

镜像操作


一、glide简介

  1. glide是继vendor之后的包管理工具,它的出现为了解决vendor的不足,vendor作为目录管理不具备主动下载的功能,只能通过go get配合,并且不能指定版本以及不能区分版本。
  2. glide的原理:扫描项目的依赖解析其依赖关系,并与gilde.yaml文件进行匹配,如果相匹配优先使用指定版本。
  3. glide的特性:针对vendor进行了功能扩展,它提供了依赖解析和传递,并提供依赖下载,增加了依赖版本控制,镜像源管理。
  4. glide可以设置缓存,类似本地仓库,位置在%userdir%/.glide/cache/src 最外部有mirror.yaml来指定镜像,glide命令在获取依赖包时,通过这个缓存进行加速。

二、glide命令简介

##安装glide后

glide --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文件夹并添加到glide.yaml文件
remove, rm Remove a package from the glide.yaml file, and regenerate the lock file. 从glide.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. 列出glide.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 关于glide
mirror Manage mirrors 镜像管理
help, h Shows a list of commands or help for one command 帮助命令

三、glide安装配置

step1:下载安装glide

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:使用glide

%GOPATH%/src/glide-demo           ##在此目录下建立项目叫glide-demo
%GOPATH%/src/glide-demo/main.go   ##在项目目录新建src文件夹并创建main.go
%GOPATH%/src/glide-demo/vendor    ##在项目目录新建vendor文件夹

##########################项目结构#########################
glide-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        ##切换项目根目录
glide init                           ##glide.yaml文件

ps:初始化过程中会提示需不需要配置向导,可以使用N快速默认初始化


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

glide.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:根据glide安装依赖到vendor

cd /d %GOPATH%/src/glide-demo        ##切换项目根目录
glide install                        ##根据glide.yaml文件安装依赖
ps:首次安装依赖会检查glide.lock,下载gilde.yaml后生成glide.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目录有项目的所有依赖包

四、glide官方bug

bug1:windows使用glide 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:glide install 或者 glide 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
============================================================================

五、glide常用命令

初始化glide

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

猜你喜欢

转载自blog.csdn.net/qq_22211217/article/details/102027358