Skip to content

Commit 44e89fd

Browse files
authored
Merge pull request #2 from lixiaojun629/master
simplify config and completion
2 parents 18782d2 + 20f8586 commit 44e89fd

16 files changed

Lines changed: 235 additions & 105 deletions

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export VERSION=0.1.1
1+
export VERSION=0.1.2
22

33
.PHONY : build
44
build:
@@ -8,16 +8,22 @@ build:
88
build_mac:
99
GOOS=darwin GOARCH=amd64 go build -o out/ucloud main.go
1010
tar zcvf out/ucloud-cli-macosx-${VERSION}-amd64.tgz -C out ucloud
11+
shasum -a 256 out/ucloud-cli-macosx-${VERSION}-amd64.tgz
1112

1213
.PHONY : build_linux
1314
build_linux:
1415
GOOS=linux GOARCH=amd64 go build -o out/ucloud main.go
1516
tar zcvf out/ucloud-cli-linux-${VERSION}-amd64.tgz -C out ucloud
17+
shasum -a 256 out/ucloud-cli-linux-${VERSION}-amd64.tgz
1618

1719
.PHONY : build_windows
1820
build_windows:
1921
GOOS=windows GOARCH=amd64 go build -o out/ucloud.exe main.go
2022
zip -r out/ucloud-cli-windows-${VERSION}-amd64.zip out/ucloud.exe
23+
shasum -a 256 out/ucloud-cli-windows-${VERSION}-amd64.zip
24+
25+
.PHONY : build_all
26+
build_all: build_mac build_linux build_windows
2127

2228
.PHONY : install
2329
install:

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ You can install UCloud CLI by downloading executable binary file or building fro
88

99
##### Download binary file
1010
Archive links:
11-
[Mac](http://ucloud-sdk.ufile.ucloud.com.cn/ucloud-cli-macosx-0.1.1-amd64.tgz)
12-
[Linux](http://ucloud-sdk.ufile.ucloud.com.cn/ucloud-cli-linux-0.1.1-amd64.tgz)
13-
[Windows](http://ucloud-sdk.ufile.ucloud.com.cn/ucloud-cli-windows-0.1.1-amd64.zip)
11+
[Mac](http://ucloud-sdk.ufile.ucloud.com.cn/ucloud-cli-macosx-0.1.2-amd64.tgz)
12+
[Linux](http://ucloud-sdk.ufile.ucloud.com.cn/ucloud-cli-linux-0.1.2-amd64.tgz)
13+
[Windows](http://ucloud-sdk.ufile.ucloud.com.cn/ucloud-cli-windows-0.1.2-amd64.zip)
1414

15-
SHA-256 hashcode
15+
SHA-256 checksum
1616
```
17-
165f1ce4d413bf92e2794efe2722678eb80990602b81fd6e501d0c5f6bbf30bb ucloud-cli-linux-0.1.1-amd64.tgz
18-
e174c2ef268f4b653062d0e1331bf642134a0fafbb745b407969a194d7c1bc0c ucloud-cli-macosx-0.1.1-amd64.tgz
19-
75ff8741d9348881b3d992701590bc27f9278f207a3fb9a12ef0edfab19058d2 ucloud-cli-windows-0.1.1-amd64.zip
17+
19b7a0803fc41ee689797a36fd67b288e993c383edf6087f56825a4d5bb17875 ucloud-cli-linux-0.1.2-amd64.tgz
18+
ecc787f4045ea14d583801cd0cfa746be357d50756c2cf0ba879e405c2325d1c ucloud-cli-macosx-0.1.2-amd64.tgz
19+
f48058ac96bb0283b18c660f0350eedba49d03a753775b0a2773b2081698b3f3 ucloud-cli-windows-0.1.2-amd64.zip
2020
```
2121

2222
Download the binary file and extract to /usr/local/bin directory or add it to the $PATH. Take macOS as an example.
2323
```
24-
$ curl -o ucloud-cli.tgz http://ucloud-sdk.ufile.ucloud.com.cn/ucloud-cli-macosx-0.1.1-amd64.tgz
25-
$ echo "e174c2ef268f4b653062d0e1331bf642134a0fafbb745b407969a194d7c1bc0c *ucloud-cli-macosx-0.1.1-amd64.tgz" | shasum -a 256 -c
24+
$ curl -o ucloud-cli.tgz http://ucloud-sdk.ufile.ucloud.com.cn/ucloud-cli-macosx-0.1.2-amd64.tgz
25+
$ echo "ecc787f4045ea14d583801cd0cfa746be357d50756c2cf0ba879e405c2325d1c *ucloud-cli-macosx-0.1.2-amd64.tgz" | shasum -a 256 -c
2626
$ tar -zxf ucloud-cli.tgz
2727
$ cp ucloud /usr/local/bin
2828
```
@@ -33,14 +33,16 @@ If you have installed golang, run the following commands to install the UCloud C
3333
```
3434
$ mkdir -p $GOPATH/src/github.com/ucloud
3535
$ cd $GOPATH/src/github.com/ucloud
36-
$ git clone http://github.com/ucloud/ucloud-cli.git
36+
$ git clone https://github.com/ucloud/ucloud-cli.git
3737
$ cd ucloud-cli
3838
$ make install
3939
```
4040

4141
### Config UCloud CLI
4242

4343
After install the cli, run 'ucloud config' to complete the cli configuration following the tips. Local settings will be saved in directory $HOME/.ucloud
44+
Command 'ucloud ls --object [region|project]' display all the regions and projects. You can change the default region and prject by runing 'ucloud config set [region|project] xxx'.
45+
Execute 'ucloud config --help' for more information.
4446

4547
### Uninstall UCloud CLI
4648

cmd/completion.go

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import (
1919
"fmt"
2020
"io"
2121
"os"
22+
"os/exec"
23+
"regexp"
24+
"runtime"
2225
"strings"
2326

2427
"github.com/spf13/cobra"
@@ -31,20 +34,17 @@ func NewCmdCompletion() *cobra.Command {
3134
On macOS, using bash
3235
3336
On macOS, you will need to install bash-completion support via Homebrew first:
34-
If running Bash 3.2 included with macOS
35-
> brew install bash-completion
36-
or, if running Bash 4.1+
37-
> brew install bash-completion@2
37+
$ ucloud completion
38+
$ brew install bash-completion
3839
Follow the “caveats” section of brew’s output to add the appropriate bash completion path to your local .bash_profile.
3940
and then generate bash completion scripts for ucloud
40-
> ucloud completion
4141
4242
On Linux, using bash
4343
44-
On CentOS Linux, you may need to install the bash-completion package which is not installed by default.
45-
> yum install bash-completion -y
44+
On Linux, you may need to install the bash-completion package which is not installed by default.
45+
$ ucloud completion
46+
$ yum install bash-completion or apt-get install bash-completion
4647
and then genreate bash completion scripts for ucloud
47-
> ucloud completion
4848
4949
Using zsh
5050
> ucloud completion
@@ -73,21 +73,65 @@ func NewCmdCompletion() *cobra.Command {
7373
return completionCmd
7474
}
7575

76+
var darwinBash = `
77+
Install bash-completion with command 'brew install bash-completion', and then append the following scripts to ~/.bash_profile
78+
79+
if [ -f $(brew --prefix)/etc/bash_completion ]; then
80+
. $(brew --prefix)/etc/bash_completion
81+
fi
82+
83+
source ~/.ucloud/ucloud.sh
84+
`
85+
86+
var linuxBash = `
87+
Ensure your have installed bash-completion, and then append the following scripts to ~/.bashrc
88+
89+
if [ -f /etc/bash_completion ]; then
90+
. /etc/bash_completion
91+
fi
92+
93+
source ~/.ucloud/ucloud.sh
94+
`
95+
96+
func getBashVersion() (version string, err error) {
97+
lookupBashVersion := exec.Command("bash", "-version")
98+
out, err := lookupBashVersion.Output()
99+
if err != nil {
100+
context.AppendError(err)
101+
fmt.Println(err)
102+
}
103+
104+
// Example
105+
// $ bash -version
106+
// GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
107+
// Copyright (C) 2007 Free Software Foundation, Inc.
108+
versionStr := string(out)
109+
re := regexp.MustCompile("(\\d)\\.\\d\\.")
110+
strs := re.FindAllStringSubmatch(versionStr, -1)
111+
if len(strs) >= 1 {
112+
result := strs[0]
113+
if len(result) >= 2 {
114+
version = result[1]
115+
}
116+
}
117+
if version == "" {
118+
err = fmt.Errorf("lookup bash version failed")
119+
}
120+
return
121+
}
122+
76123
func bashCompletion(cmd *cobra.Command) {
77124
home := util.GetHomePath()
78125
shellPath := home + "/" + util.ConfigPath + "/ucloud.sh"
79126
cmd.GenBashCompletionFile(shellPath)
80-
for _, rc := range [...]string{".bashrc", ".bash_profile", ".bash_login", ".profile"} {
81-
rcPath := home + "/" + rc
82-
if _, err := os.Stat(rcPath); err == nil {
83-
cmd := "source " + shellPath
84-
if util.LineInFile(rcPath, cmd) == false {
85-
util.AppendToFile(rcPath, cmd)
86-
fmt.Println("Auto completion is on. Please install bash-completion on your platform using brew,yum or apt-get. ucloud completion --help for more information")
87-
} else {
88-
fmt.Println("Auto completion update. Restart session")
89-
}
90-
}
127+
fmt.Printf("Completion scripts has been written to '~/%s/ucloud.sh'\n", util.ConfigPath)
128+
129+
platform := runtime.GOOS
130+
131+
if platform == "darwin" {
132+
fmt.Println(darwinBash)
133+
} else if platform == "linux" {
134+
fmt.Println(linuxBash)
91135
}
92136
}
93137

@@ -97,22 +141,17 @@ func zshCompletion(cmd *cobra.Command) {
97141
file, err := os.Create(shellPath)
98142
if err != nil {
99143
fmt.Println(err)
144+
context.AppendError(err)
100145
return
101146
}
102147
defer file.Close()
148+
103149
runCompletionZsh(file, cmd)
150+
fmt.Printf("Completion scripts was written to '~/%s/_ucloud'\n", util.ConfigPath)
104151

105-
rcPath := home + "/.zshrc"
106-
if _, err = os.Stat(rcPath); err == nil {
107-
cmd := fmt.Sprintf("fpath=(%s/%s $fpath);", home, util.ConfigPath)
108-
cmd += "autoload -U +X compinit && compinit"
109-
if util.LineInFile(rcPath, cmd) == false {
110-
util.AppendToFile(rcPath, cmd)
111-
fmt.Println("Auto completion is on")
112-
} else {
113-
fmt.Println("Auto completion update. Restart session")
114-
}
115-
}
152+
scripts := fmt.Sprintf("fpath=(~/%s $fpath)\n", util.ConfigPath)
153+
scripts += "autoload -U +X compinit && compinit"
154+
fmt.Printf("Please append the following scripts to your ~/.zshrc\n%s\n", scripts)
116155
}
117156

118157
//参考自 k8s.io/kubernetes/pkg/kubectl/cmd/completion.go

cmd/configure.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,7 @@ var config = model.ConfigInstance
2727

2828
//NewCmdConfig ucloud config
2929
func NewCmdConfig() *cobra.Command {
30-
31-
var configDesc = `Command 'ucloud config' is used to configure public-key,private-key and other settings.
32-
33-
Public-key and private-key could be acquired from https://console.ucloud.cn/uapi/apikey.
34-
35-
If you don’t have an UCloud account yet, run 'ucloud sign-up', and authenticate the account with your valid documentation.
36-
37-
If you just want to configure default region or project, please run 'ucloud config set region/project xxx'. Run 'ucloud config --help' for more infomation.
38-
39-
`
30+
var configDesc = `Public-key and private-key could be acquired from https://console.ucloud.cn/uapi/apikey.`
4031
var helloUcloud = `
4132
_ _ _ _ _ _ _____ _ _
4233
| | | | | | | | | | / __ \ | | |
@@ -49,10 +40,10 @@ If you just want to configure default region or project, please run 'ucloud conf
4940
var configCmd = &cobra.Command{
5041
Use: "config",
5142
Short: "Config UCloud CLI options",
52-
Long: `Config UCloud CLI options such as credentials and other settings.`,
53-
Example: "ucloud config; ucloud config set region cn-bj2",
43+
Long: `Config UCloud CLI options such as private-key,public-key,default region and default project-id.`,
44+
Example: "ucloud config; ucloud config set region cn-bj2; ucloud config set project org-xxx",
5445
Run: func(cmd *cobra.Command, args []string) {
55-
fmt.Printf(configDesc)
46+
fmt.Println(configDesc)
5647
if len(config.PrivateKey) != 0 && len(config.PublicKey) != 0 {
5748
fmt.Printf("Your have already configured public-key and private-key. Do you want to overwrite it? (y/n):")
5849
var overwrite string
@@ -69,24 +60,32 @@ If you just want to configure default region or project, please run 'ucloud conf
6960
}
7061
config.ConfigPublicKey()
7162
config.ConfigPrivateKey()
72-
fmt.Println("Fetching regions...")
73-
err := listRegion()
63+
64+
region, err := getDefaultRegion()
7465
if err != nil {
66+
context.AppendError(err)
7567
fmt.Println(err)
76-
return
68+
} else {
69+
config.Region = region
70+
fmt.Printf("Configured default region:%s\n", region)
7771
}
78-
config.ConfigRegion()
7972

80-
fmt.Println("Fetching projects...")
81-
err = listProject()
73+
project, err := getDefaultProject()
8274
if err != nil {
75+
context.AppendError(err)
8376
fmt.Println(err)
84-
return
77+
} else {
78+
config.ProjectID = project
79+
fmt.Printf("Configured default project:%s\n", project)
8580
}
8681

87-
config.ConfigProjectID()
8882
config.SaveConfig()
89-
certified, err := isUserCertified()
83+
84+
userInfo, err := getUserInfo()
85+
86+
fmt.Printf("You are logged in as: [%s]\n", userInfo.UserEmail)
87+
88+
certified := isUserCertified(userInfo)
9089
if err != nil {
9190
fmt.Println(err)
9291
} else if certified == false {
@@ -144,7 +143,7 @@ func NewCmdConfigSet() *cobra.Command {
144143
Use: "set",
145144
Short: "Set a config value",
146145
Long: "Set a config value, including private-key public-key region and project-id.",
147-
Example: "ucloud configure set region cn-bj2",
146+
Example: "ucloud config set region cn-bj2",
148147
Run: func(cmd *cobra.Command, args []string) {
149148
if len(args) != 2 {
150149
fmt.Printf("Error: accepts 2 arg(s), received %d\n", len(args))

cmd/eip.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
func NewCmdEIP() *cobra.Command {
2525
var cmd = &cobra.Command{
2626
Use: "eip",
27-
Short: "EIP managment",
28-
Long: `EIP managment, such as list,allocate and release`,
27+
Short: "List,allocate and release EIP",
28+
Long: `Manipulate EIP, such as list,allocate and release`,
2929
Args: cobra.NoArgs,
3030
}
3131
cmd.AddCommand(NewCmdEIPList())

cmd/globalssh.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
func NewCmdGssh() *cobra.Command {
2626
var cmd = &cobra.Command{
2727
Use: "gssh",
28-
Short: "GlobalSSH management",
29-
Long: `GlobalSSH management, such as create,modify,list and delete`,
28+
Short: "Create and manage globalssh instance",
29+
Long: `Create and manage globalssh instance, such as create,modify,list and delete`,
3030
}
3131
cmd.AddCommand(NewCmdGsshList())
3232
cmd.AddCommand(NewCmdGsshCreate())
@@ -86,7 +86,7 @@ func NewCmdGsshCreate() *cobra.Command {
8686
fmt.Println("Error:", err)
8787
return
8888
}
89-
if port <= 1 || port >= 65535 || port == 80 || port == 443 {
89+
if port < 1 || port > 65535 || port == 80 || port == 443 {
9090
fmt.Println("The port number should be between 1 and 65535, and cannot be equal to 80 or 443")
9191
return
9292
}

0 commit comments

Comments
 (0)