目录
一、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后
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 | 帮助命令 |
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
%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
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目录有项目的所有依赖包
[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
[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
============================================================================
cd /d %GOPATH%/src/glide-demo ##切换项目根目录
glide create or glide init
cd /d %GOPATH%/src/glide-demo ##切换项目根目录
glide update or glide up
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