当前位置: 首页 > 工具软件 > Cobra > 使用案例 >

mac下安装cobra

邴墨竹
2023-12-01

1.代理配置(可选)

记录一下操作, 我这边自己设置了一下代理,主要是为了访问github方便,注意这里是我自己的代理哈:

git config --global http.proxy http://127.0.0.1:7890 
git config --global https.proxy https://127.0.0.1:7890

这样我这边可以正常创建一个空库:

git clone https://github.com/spf13/cobra.git --bare

bare % git clone https://github.com/spf13/cobra.git --bare  


Cloning into bare repository 'cobra.git'...
remote: Enumerating objects: 3927, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 3927 (delta 1), reused 1 (delta 0), pack-reused 3922
Receiving objects: 100% (3927/3927), 1.78 MiB | 4.07 MiB/s, done.
Resolving deltas: 100% (2533/2533), done.

2.安装cobra

直接使用go get 命令无法正常执行:

go get -u github.com/spf13/cobra/cobra

效果如下:

go get -u github.com/spf13/cobra/cobra

go: go.mod file not found in current directory or any parent directory.
	'go get' is no longer supported outside a module.
	To build and install a command, use 'go install' with a version,
	like 'go install example.com/cmd@latest'
	For more information, see https://golang.org/doc/go-get-install-deprecation
	or run 'go help get' or 'go help install'.

接着查其他的文档如何安装cobra,首先是go install 不支持现有的版本了,这个老版本支持。

那么切换命令:

go install github.com/spf13/cobra-cli@latest

执行这个操作时出现下面的问题:

go install github.com/spf13/cobra-cli@latest


go: downloading github.com/spf13/cobra-cli v1.3.0
go: downloading github.com/spf13/cobra v1.3.0
go: downloading github.com/spf13/viper v1.10.1
go: downloading github.com/mitchellh/mapstructure v1.4.3
go: downloading github.com/spf13/afero v1.6.0
go: downloading github.com/magiconair/properties v1.8.5
go: downloading github.com/fsnotify/fsnotify v1.5.1
go: downloading github.com/spf13/cast v1.4.1
go: downloading github.com/spf13/jwalterweatherman v1.1.0
go: downloading github.com/spf13/pflag v1.0.5
go: downloading github.com/subosito/gotenv v1.2.0
go: downloading gopkg.in/ini.v1 v1.66.2
go: downloading github.com/hashicorp/hcl v1.0.0
go: downloading github.com/pelletier/go-toml v1.9.4
go: downloading gopkg.in/yaml.v2 v2.4.0
go: downloading golang.org/x/sys v0.0.0-20211210111614-af8b64212486
go: downloading golang.org/x/text v0.3.7
go install github.com/spf13/cobra-cli: copying /var/folders/1j/tfrm4pts3f5_qggpmb1p677m0000gn/T/go-build1230213418/b001/exe/a.out: open /usr/local/go/bin/cobra-cli: permission denied

open /usr/local/go/bin/cobra-cli: permission denied 说明没有权限,于是修改权限;

执行下面的命令,找到当前的登陆用户名称:

who am i 

 接着执行下面命令:


sudo chown -R -v 用户名:admin /usr/local/go

这里执行完成后,重新执行:go install github.com/spf13/cobra-cli@latest 这样就正常安装了。

注意:

1. 我的 .bash_profile 文件,如下:

export GO111MODULE=on

export GOPROXY=https://goproxy.cn

export GOPATH=/Users/用户名称/go

export GOBIN=/usr/local/go/bin

export M2_HOME=/Users/用户名称/install/apache-maven-3.8.1

export PATH=$PATH:$M2_HOME/bin:$GOROOT/bin:${GOPATH//://bin:}/bin

PATH=$PATH:/usr/local/mysql/bin:$MAVEN_HOME/bin

2. 通过go env 查看环境信息

3.生成程序框架

使用命令:cobra-cli,正常安装后如下

cobra-cli


Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.

Usage:
  cobra-cli [command]

Available Commands:
  add         Add a command to a Cobra Application
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  init        Initialize a Cobra Application

Flags:
  -a, --author string    author name for copyright attribution (default "YOUR NAME")
      --config string    config file (default is $HOME/.cobra.yaml)
  -h, --help             help for cobra-cli
  -l, --license string   name of license for the project
      --viper            use Viper for configuration

Use "cobra-cli [command] --help" for more information about a command.

使用cobra-cli来创建一个工程:

#创建一个目录
mkdir my_cobra  

#跳转到my_cobra
cd my_cobra 


#创建mod文件
go mod init my_cobra

#初始化
cobra-cli init

执行完成后,如下:

# 查看文件信息
ls

LICENSE	cmd	go.mod	go.sum	main.go

执行命令:

# 这里添加一个image.go的文件,生成文件位置: my_cobra/cmd 这个目录下面

cobra-cli add image

将文件导入到mscode 或者 goland中,执行下面命令:

go mod init

go mod tidy

接着就可以自己开发和执行命令

go run main.go 
go run main.go image

 类似资料: