Skip to content

Commit 8e98d5e

Browse files
authored
Merge pull request #17 from lixiaojun629/develop
optimize & bugfix
2 parents 3adff9d + a5f6825 commit 8e98d5e

15 files changed

Lines changed: 475 additions & 330 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export VERSION=0.1.8
1+
export VERSION=0.1.9
22

33
.PHONY : build
44
build:

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,33 @@ $ make install
4242

4343
## Command Completion
4444

45-
The ucloud-cli include command completion feature and need configure it manually. Add following scripts to ~/.bash_profile or ~/.bashrc
45+
The ucloud-cli include command completion feature and need configure it manually.
46+
47+
**Bash shell** Add following scripts to ~/.bash_profile or ~/.bashrc
4648

4749
```
48-
complete -C /usr/local/bin/ucloud ucloud
50+
complete -C $(which ucloud) ucloud
4951
```
5052

5153
**Zsh shell** please add following scripts to ~/.zshrc
5254

5355
```
5456
autoload -U +X bashcompinit && bashcompinit
55-
complete -F /usr/local/bin/ucloud ucloud
57+
complete -F $(which ucloud) ucloud
58+
```
59+
Zsh builtin command bashcompinit may not work on some platform. If the scripts don't work on your OS, try following scripts
5660
```
61+
_ucloud() {
62+
read -l;
63+
local cl="$REPLY";
64+
read -ln;
65+
local cp="$REPLY";
66+
reply=(`COMP_SHELL=zsh COMP_LINE="$cl" COMP_POINT="$cp" ucloud`)
67+
}
68+
69+
compctl -K _ucloud ucloud
70+
```
71+
5772

5873
## Getting Started
5974

@@ -106,10 +121,10 @@ Secondly, we're going to allocate an EIP and bind to the instance created above.
106121

107122
```
108123
$ ucloud eip allocate --line International --bandwidth 1
109-
EIPId:eip-xov13b,IP:152.32.140.92,Line:International
124+
allocate EIP[eip-xxx] IP:106.75.xx.xx Line:BGP
110125
111-
$ ucloud eip bind --eip-id eip-xov13b --resource-id uhost-tr1eau
112-
EIP: [eip-xov13b] bind with uhost:[uhost-tr1eau] successfully
126+
$ ucloud eip bind --eip-id eip-xxx --resource-id uhost-xxx
127+
bind EIP[eip-xxx] with uhost[uhost-xxx]
113128
```
114129

115130
Configure the GlobalSSH to the uhost instance and login the instance via GlobalSSH

base/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
2929
const DefaultProfile = "default"
3030

3131
//Version 版本号
32-
const Version = "0.1.8"
32+
const Version = "0.1.9"
3333

3434
//ConfigIns 配置实例, 程序加载时生成
3535
var ConfigIns = &AggConfig{}

base/util.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,42 @@ func WriteJSONFile(list interface{}, fileName string) error {
490490
}
491491
return nil
492492
}
493+
494+
//GetFileList 补全文件名
495+
func GetFileList(suffix string) []string {
496+
cmdLine := strings.TrimSpace(os.Getenv("COMP_LINE"))
497+
words := strings.Split(cmdLine, " ")
498+
last := words[len(words)-1]
499+
pathPrefix := "."
500+
501+
if !strings.HasPrefix(last, "-") {
502+
pathPrefix = last
503+
}
504+
hasTilde := false
505+
//https://tiswww.case.edu/php/chet/bash/bashref.html#Tilde-Expansion
506+
if strings.HasPrefix(pathPrefix, "~") {
507+
pathPrefix = strings.Replace(pathPrefix, "~", GetHomePath(), 1)
508+
hasTilde = true
509+
}
510+
files, err := ioutil.ReadDir(pathPrefix)
511+
if err != nil {
512+
log.Fatal(err)
513+
return nil
514+
}
515+
names := []string{}
516+
for _, f := range files {
517+
name := f.Name()
518+
if !strings.HasSuffix(name, suffix) {
519+
continue
520+
}
521+
if hasTilde {
522+
pathPrefix = strings.Replace(pathPrefix, GetHomePath(), "~", 1)
523+
}
524+
if strings.HasSuffix(pathPrefix, "/") {
525+
names = append(names, pathPrefix+name)
526+
} else {
527+
names = append(names, pathPrefix+"/"+name)
528+
}
529+
}
530+
return names
531+
}

cmd/share_bw.go renamed to cmd/bandwidth.go

Lines changed: 182 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,32 @@ import (
1818
"fmt"
1919
"strconv"
2020
"strings"
21+
"time"
2122

2223
"github.com/spf13/cobra"
2324

2425
sdk "github.com/ucloud/ucloud-sdk-go/ucloud"
2526

2627
"github.com/ucloud/ucloud-cli/base"
28+
"github.com/ucloud/ucloud-cli/model/status"
2729
)
2830

31+
//NewCmdBandwidth ucloud bw
32+
func NewCmdBandwidth() *cobra.Command {
33+
cmd := &cobra.Command{
34+
Use: "bw",
35+
Short: "Manipulate bandwidth package and shared bandwidth",
36+
Long: "Manipulate bandwidth package and shared bandwidth",
37+
}
38+
cmd.AddCommand(NewCmdBandwidthPkg())
39+
cmd.AddCommand(NewCmdSharedBW())
40+
return cmd
41+
}
42+
2943
//NewCmdSharedBW ucloud shared-bw
3044
func NewCmdSharedBW() *cobra.Command {
3145
cmd := &cobra.Command{
32-
Use: "shared-bw",
46+
Use: "shared",
3347
Short: "Create and manipulate shared bandwidth instances",
3448
Long: "Create and manipulate shared bandwidth instances",
3549
}
@@ -201,14 +215,14 @@ func NewCmdSharedBWDelete() *cobra.Command {
201215

202216
flags.StringSliceVar(&ids, "shared-bw-id", nil, "Required. Resource ID of shared bandwidth instances to delete")
203217
req.EIPBandwidth = flags.Int("eip-bandwidth-mb", 1, "Optional. Bandwidth of the joined EIPs,after deleting the shared bandwidth instance")
204-
req.PayMode = flags.String("charge-mode", "", "Optional. Charge mode of joined EIPs,after deleting the shared bandwidth")
218+
req.PayMode = flags.String("traffic-mode", "", "Optional. The charge mode of joined EIPs after deleting the shared bandwidth. Accept values:Bandwidth,Traffic")
205219
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
206220
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")
207221
flags.SetFlagValuesFunc("shared-bw-id", func() []string {
208222
list, _ := getAllSharedBW(*req.ProjectId, *req.Region)
209223
return list
210224
})
211-
flags.SetFlagValues("charge-mode", "Bandwidth", "Traffic")
225+
flags.SetFlagValues("traffic-mode", "Bandwidth", "Traffic")
212226

213227
cmd.MarkFlagRequired("shared-bw-id")
214228

@@ -229,3 +243,168 @@ func getAllSharedBW(project, region string) ([]string, error) {
229243
}
230244
return list, nil
231245
}
246+
247+
//NewCmdBandwidthPkg ucloud bw-pkg
248+
func NewCmdBandwidthPkg() *cobra.Command {
249+
cmd := &cobra.Command{
250+
Use: "pkg",
251+
Short: "List, create and delete bandwidth package instances",
252+
Long: "List, create and delete bandwidth package instances",
253+
}
254+
cmd.AddCommand(NewCmdBandwidthPkgCreate())
255+
cmd.AddCommand(NewCmdBandwidthPkgList())
256+
cmd.AddCommand(NewCmdBandwidthPkgDelete())
257+
return cmd
258+
}
259+
260+
//NewCmdBandwidthPkgCreate ucloud bw-pkg create
261+
func NewCmdBandwidthPkgCreate() *cobra.Command {
262+
var start, end *string
263+
timeLayout := "2006-01-02/15:04:05"
264+
ids := []string{}
265+
req := base.BizClient.NewCreateBandwidthPackageRequest()
266+
loc, _ := time.LoadLocation("Local")
267+
cmd := &cobra.Command{
268+
Use: "create",
269+
Short: "Create bandwidth package",
270+
Long: "Create bandwidth package",
271+
Example: "ucloud bw pkg create --eip-id eip-xxx --bandwidth-mb 20 --start-time 2018-12-15/09:20:00 --end-time 2018-12-16/09:20:00",
272+
Run: func(c *cobra.Command, args []string) {
273+
st, err := time.ParseInLocation(timeLayout, *start, loc)
274+
if err != nil {
275+
base.HandleError(err)
276+
return
277+
}
278+
et, err := time.ParseInLocation(timeLayout, *end, loc)
279+
if err != nil {
280+
base.HandleError(err)
281+
return
282+
}
283+
if st.Sub(time.Now()) < 0 {
284+
base.Cxt.Println("start-time must be after the current time")
285+
return
286+
}
287+
du := et.Unix() - st.Unix()
288+
if du <= 0 {
289+
base.Cxt.Println("end-time must be after the start-time")
290+
return
291+
}
292+
req.EnableTime = sdk.Int(int(st.Unix()))
293+
req.TimeRange = sdk.Int(int(du))
294+
295+
for _, id := range ids {
296+
id = base.PickResourceID(id)
297+
req.EIPId = &id
298+
resp, err := base.BizClient.CreateBandwidthPackage(req)
299+
if err != nil {
300+
base.HandleError(err)
301+
continue
302+
}
303+
base.Cxt.Printf("bandwidth package[%s] created for eip[%s]\n", resp.BandwidthPackageId, id)
304+
}
305+
},
306+
}
307+
flags := cmd.Flags()
308+
flags.SortFlags = false
309+
flags.StringSliceVar(&ids, "eip-id", nil, "Required. Resource ID of eip to be bound with created bandwidth package")
310+
start = flags.String("start-time", "", "Required. The time to enable bandwidth package. Local time, for example '2018-12-25/08:30:00'")
311+
end = flags.String("end-time", "", "Required. The time to disable bandwidth package. Local time, for example '2018-12-26/08:30:00'")
312+
req.Bandwidth = flags.Int("bandwidth-mb", 0, "Required. bandwidth of the bandwidth package to create.Range [1,800]. Unit:'Mb'.")
313+
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
314+
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")
315+
316+
cmd.Flags().SetFlagValuesFunc("eip-id", func() []string {
317+
return getAllEip(*req.ProjectId, *req.Region, []string{status.EIP_USED}, []string{status.EIP_CHARGE_BANDWIDTH})
318+
})
319+
320+
cmd.MarkFlagRequired("eip-id")
321+
cmd.MarkFlagRequired("start-time")
322+
cmd.MarkFlagRequired("end-time")
323+
cmd.MarkFlagRequired("bandwidth-mb")
324+
return cmd
325+
}
326+
327+
//BandwidthPkgRow 表格行
328+
type BandwidthPkgRow struct {
329+
ResourceID string
330+
EIP string
331+
Bandwidth string
332+
StartTime string
333+
EndTime string
334+
}
335+
336+
//NewCmdBandwidthPkgList ucloud bw-pkg list
337+
func NewCmdBandwidthPkgList() *cobra.Command {
338+
req := base.BizClient.NewDescribeBandwidthPackageRequest()
339+
cmd := &cobra.Command{
340+
Use: "list",
341+
Short: "List bandwidth packages",
342+
Long: "List bandwidth packages",
343+
Run: func(c *cobra.Command, args []string) {
344+
resp, err := base.BizClient.DescribeBandwidthPackage(req)
345+
if err != nil {
346+
base.HandleError(err)
347+
return
348+
}
349+
list := []BandwidthPkgRow{}
350+
for _, bp := range resp.DataSets {
351+
row := BandwidthPkgRow{
352+
ResourceID: bp.BandwidthPackageId,
353+
Bandwidth: strconv.Itoa(bp.Bandwidth) + "MB",
354+
StartTime: base.FormatDateTime(bp.EnableTime),
355+
EndTime: base.FormatDateTime(bp.DisableTime),
356+
}
357+
eip := bp.EIPId
358+
for _, addr := range bp.EIPAddr {
359+
eip += "/" + addr.IP + "/" + addr.OperatorName
360+
}
361+
row.EIP = eip
362+
list = append(list, row)
363+
}
364+
if global.json {
365+
base.PrintJSON(list)
366+
} else {
367+
base.PrintTableS(list)
368+
}
369+
},
370+
}
371+
flags := cmd.Flags()
372+
flags.SortFlags = false
373+
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
374+
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")
375+
req.Offset = cmd.Flags().Int("offset", 0, "Optional. Offset")
376+
req.Limit = cmd.Flags().Int("limit", 50, "Optional. Limit range [0,10000000]")
377+
378+
return cmd
379+
}
380+
381+
//NewCmdBandwidthPkgDelete ucloud bw-pkg delete
382+
func NewCmdBandwidthPkgDelete() *cobra.Command {
383+
ids := []string{}
384+
req := base.BizClient.NewDeleteBandwidthPackageRequest()
385+
cmd := &cobra.Command{
386+
Use: "delete",
387+
Short: "Delete bandwidth packages",
388+
Long: "Delete bandwidth packages",
389+
Example: "ucloud bw pkg delete --resource-id bwpack-xxx",
390+
Run: func(c *cobra.Command, args []string) {
391+
for _, id := range ids {
392+
id := base.PickResourceID(id)
393+
req.BandwidthPackageId = &id
394+
_, err := base.BizClient.DeleteBandwidthPackage(req)
395+
if err != nil {
396+
base.HandleError(err)
397+
return
398+
}
399+
base.Cxt.Printf("bandwidth package[%s] deleted\n", id)
400+
}
401+
},
402+
}
403+
flags := cmd.Flags()
404+
flags.SortFlags = false
405+
flags.StringSliceVar(&ids, "resource-id", nil, "Required, Resource ID of bandwidth package to delete")
406+
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
407+
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")
408+
409+
return cmd
410+
}

cmd/completion.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func NewCmdCompletion() *cobra.Command {
4141
} else if strings.HasSuffix(shell, "zsh") {
4242
zshCompletion(cmd)
4343
} else {
44-
fmt.Println("Unknow shell: %", shell)
44+
fmt.Printf("So far, shell %s is not supported\n", shell)
4545
}
4646
} else {
4747
fmt.Println("Lookup shell failed")
@@ -54,33 +54,18 @@ func NewCmdCompletion() *cobra.Command {
5454
func bashCompletion(cmd *cobra.Command) {
5555
platform := runtime.GOOS
5656
if platform == "darwin" {
57-
fmt.Println(`Please append 'complete -C /usr/local/bin/ucloud ucloud' to file '~/.bash_profile'
58-
If the following scripts are included in '~/.bash_profile', please remove it. Those scripts used to auto complete words before ucloud cli v0.1.3"
59-
60-
if [ -f $(brew --prefix)/etc/bash_completion ]; then
61-
. $(brew --prefix)/etc/bash_completion
62-
fi
63-
source ~/.ucloud/ucloud.sh`)
57+
fmt.Println(`Please append 'complete -C $(which ucloud) ucloud' to file '~/.bash_profile'`)
6458

6559
} else if platform == "linux" {
66-
fmt.Println(`Please append 'complete -C /usr/local/bin/ucloud ucloud' to file '~/.bashrc'
67-
If the following scripts are included in '~/.bashrc', please remove it. Those scripts used to auto complete words before ucloud cli v0.1.3"
68-
69-
if [ -f /etc/bash_completion ]; then
70-
. /etc/bash_completion
71-
fi
72-
73-
source ~/.ucloud/ucloud.sh`)
60+
fmt.Println(`Please append 'complete -C $(which ucloud) ucloud' to file '~/.bashrc'`)
7461
}
7562
}
7663

7764
func zshCompletion(cmd *cobra.Command) {
7865
fmt.Println(`Please append the following scripts to file '~/.zshrc'.
66+
7967
autoload -U +X bashcompinit && bashcompinit
80-
complete -F /usr/local/bin/ucloud ucloud`)
81-
fmt.Println("If the following scripts are included in '~/.bash_profile' or '~/.bashrc', please remove it. The scripts used to auto complete words before ucloud cli v0.1.3")
82-
fmt.Printf("fpath=(~/%s $fpath)\n", base.ConfigPath)
83-
fmt.Println("autoload -U +X compinit && compinit")
68+
complete -F $(which ucloud) ucloud`)
8469
}
8570

8671
func getBashVersion() (version string, err error) {

0 commit comments

Comments
 (0)