Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export VERSION=0.1.8
export VERSION=0.1.9

.PHONY : build
build:
Expand Down
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,33 @@ $ make install

## Command Completion

The ucloud-cli include command completion feature and need configure it manually. Add following scripts to ~/.bash_profile or ~/.bashrc
The ucloud-cli include command completion feature and need configure it manually.

**Bash shell** Add following scripts to ~/.bash_profile or ~/.bashrc

```
complete -C /usr/local/bin/ucloud ucloud
complete -C $(which ucloud) ucloud
```

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

```
autoload -U +X bashcompinit && bashcompinit
complete -F /usr/local/bin/ucloud ucloud
complete -F $(which ucloud) ucloud
```
Zsh builtin command bashcompinit may not work on some platform. If the scripts don't work on your OS, try following scripts
```
_ucloud() {
read -l;
local cl="$REPLY";
read -ln;
local cp="$REPLY";
reply=(`COMP_SHELL=zsh COMP_LINE="$cl" COMP_POINT="$cp" ucloud`)
}

compctl -K _ucloud ucloud
```


## Getting Started

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

```
$ ucloud eip allocate --line International --bandwidth 1
EIPId:eip-xov13b,IP:152.32.140.92,Line:International
allocate EIP[eip-xxx] IP:106.75.xx.xx Line:BGP

$ ucloud eip bind --eip-id eip-xov13b --resource-id uhost-tr1eau
EIP: [eip-xov13b] bind with uhost:[uhost-tr1eau] successfully
$ ucloud eip bind --eip-id eip-xxx --resource-id uhost-xxx
bind EIP[eip-xxx] with uhost[uhost-xxx]
```

Configure the GlobalSSH to the uhost instance and login the instance via GlobalSSH
Expand Down
2 changes: 1 addition & 1 deletion base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
const DefaultProfile = "default"

//Version 版本号
const Version = "0.1.8"
const Version = "0.1.9"

//ConfigIns 配置实例, 程序加载时生成
var ConfigIns = &AggConfig{}
Expand Down
39 changes: 39 additions & 0 deletions base/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,42 @@ func WriteJSONFile(list interface{}, fileName string) error {
}
return nil
}

//GetFileList 补全文件名
func GetFileList(suffix string) []string {
cmdLine := strings.TrimSpace(os.Getenv("COMP_LINE"))
words := strings.Split(cmdLine, " ")
last := words[len(words)-1]
pathPrefix := "."

if !strings.HasPrefix(last, "-") {
pathPrefix = last
}
hasTilde := false
//https://tiswww.case.edu/php/chet/bash/bashref.html#Tilde-Expansion
if strings.HasPrefix(pathPrefix, "~") {
pathPrefix = strings.Replace(pathPrefix, "~", GetHomePath(), 1)
hasTilde = true
}
files, err := ioutil.ReadDir(pathPrefix)
if err != nil {
log.Fatal(err)
return nil
}
names := []string{}
for _, f := range files {
name := f.Name()
if !strings.HasSuffix(name, suffix) {
continue
}
if hasTilde {
pathPrefix = strings.Replace(pathPrefix, GetHomePath(), "~", 1)
}
if strings.HasSuffix(pathPrefix, "/") {
names = append(names, pathPrefix+name)
} else {
names = append(names, pathPrefix+"/"+name)
}
}
return names
}
185 changes: 182 additions & 3 deletions cmd/share_bw.go → cmd/bandwidth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,32 @@ import (
"fmt"
"strconv"
"strings"
"time"

"github.com/spf13/cobra"

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

"github.com/ucloud/ucloud-cli/base"
"github.com/ucloud/ucloud-cli/model/status"
)

//NewCmdBandwidth ucloud bw
func NewCmdBandwidth() *cobra.Command {
cmd := &cobra.Command{
Use: "bw",
Short: "Manipulate bandwidth package and shared bandwidth",
Long: "Manipulate bandwidth package and shared bandwidth",
}
cmd.AddCommand(NewCmdBandwidthPkg())
cmd.AddCommand(NewCmdSharedBW())
return cmd
}

//NewCmdSharedBW ucloud shared-bw
func NewCmdSharedBW() *cobra.Command {
cmd := &cobra.Command{
Use: "shared-bw",
Use: "shared",
Short: "Create and manipulate shared bandwidth instances",
Long: "Create and manipulate shared bandwidth instances",
}
Expand Down Expand Up @@ -201,14 +215,14 @@ func NewCmdSharedBWDelete() *cobra.Command {

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

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

Expand All @@ -229,3 +243,168 @@ func getAllSharedBW(project, region string) ([]string, error) {
}
return list, nil
}

//NewCmdBandwidthPkg ucloud bw-pkg
func NewCmdBandwidthPkg() *cobra.Command {
cmd := &cobra.Command{
Use: "pkg",
Short: "List, create and delete bandwidth package instances",
Long: "List, create and delete bandwidth package instances",
}
cmd.AddCommand(NewCmdBandwidthPkgCreate())
cmd.AddCommand(NewCmdBandwidthPkgList())
cmd.AddCommand(NewCmdBandwidthPkgDelete())
return cmd
}

//NewCmdBandwidthPkgCreate ucloud bw-pkg create
func NewCmdBandwidthPkgCreate() *cobra.Command {
var start, end *string
timeLayout := "2006-01-02/15:04:05"
ids := []string{}
req := base.BizClient.NewCreateBandwidthPackageRequest()
loc, _ := time.LoadLocation("Local")
cmd := &cobra.Command{
Use: "create",
Short: "Create bandwidth package",
Long: "Create bandwidth package",
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",
Run: func(c *cobra.Command, args []string) {
st, err := time.ParseInLocation(timeLayout, *start, loc)
if err != nil {
base.HandleError(err)
return
}
et, err := time.ParseInLocation(timeLayout, *end, loc)
if err != nil {
base.HandleError(err)
return
}
if st.Sub(time.Now()) < 0 {
base.Cxt.Println("start-time must be after the current time")
return
}
du := et.Unix() - st.Unix()
if du <= 0 {
base.Cxt.Println("end-time must be after the start-time")
return
}
req.EnableTime = sdk.Int(int(st.Unix()))
req.TimeRange = sdk.Int(int(du))

for _, id := range ids {
id = base.PickResourceID(id)
req.EIPId = &id
resp, err := base.BizClient.CreateBandwidthPackage(req)
if err != nil {
base.HandleError(err)
continue
}
base.Cxt.Printf("bandwidth package[%s] created for eip[%s]\n", resp.BandwidthPackageId, id)
}
},
}
flags := cmd.Flags()
flags.SortFlags = false
flags.StringSliceVar(&ids, "eip-id", nil, "Required. Resource ID of eip to be bound with created bandwidth package")
start = flags.String("start-time", "", "Required. The time to enable bandwidth package. Local time, for example '2018-12-25/08:30:00'")
end = flags.String("end-time", "", "Required. The time to disable bandwidth package. Local time, for example '2018-12-26/08:30:00'")
req.Bandwidth = flags.Int("bandwidth-mb", 0, "Required. bandwidth of the bandwidth package to create.Range [1,800]. Unit:'Mb'.")
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")

cmd.Flags().SetFlagValuesFunc("eip-id", func() []string {
return getAllEip(*req.ProjectId, *req.Region, []string{status.EIP_USED}, []string{status.EIP_CHARGE_BANDWIDTH})
})

cmd.MarkFlagRequired("eip-id")
cmd.MarkFlagRequired("start-time")
cmd.MarkFlagRequired("end-time")
cmd.MarkFlagRequired("bandwidth-mb")
return cmd
}

//BandwidthPkgRow 表格行
type BandwidthPkgRow struct {
ResourceID string
EIP string
Bandwidth string
StartTime string
EndTime string
}

//NewCmdBandwidthPkgList ucloud bw-pkg list
func NewCmdBandwidthPkgList() *cobra.Command {
req := base.BizClient.NewDescribeBandwidthPackageRequest()
cmd := &cobra.Command{
Use: "list",
Short: "List bandwidth packages",
Long: "List bandwidth packages",
Run: func(c *cobra.Command, args []string) {
resp, err := base.BizClient.DescribeBandwidthPackage(req)
if err != nil {
base.HandleError(err)
return
}
list := []BandwidthPkgRow{}
for _, bp := range resp.DataSets {
row := BandwidthPkgRow{
ResourceID: bp.BandwidthPackageId,
Bandwidth: strconv.Itoa(bp.Bandwidth) + "MB",
StartTime: base.FormatDateTime(bp.EnableTime),
EndTime: base.FormatDateTime(bp.DisableTime),
}
eip := bp.EIPId
for _, addr := range bp.EIPAddr {
eip += "/" + addr.IP + "/" + addr.OperatorName
}
row.EIP = eip
list = append(list, row)
}
if global.json {
base.PrintJSON(list)
} else {
base.PrintTableS(list)
}
},
}
flags := cmd.Flags()
flags.SortFlags = false
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")
req.Offset = cmd.Flags().Int("offset", 0, "Optional. Offset")
req.Limit = cmd.Flags().Int("limit", 50, "Optional. Limit range [0,10000000]")

return cmd
}

//NewCmdBandwidthPkgDelete ucloud bw-pkg delete
func NewCmdBandwidthPkgDelete() *cobra.Command {
ids := []string{}
req := base.BizClient.NewDeleteBandwidthPackageRequest()
cmd := &cobra.Command{
Use: "delete",
Short: "Delete bandwidth packages",
Long: "Delete bandwidth packages",
Example: "ucloud bw pkg delete --resource-id bwpack-xxx",
Run: func(c *cobra.Command, args []string) {
for _, id := range ids {
id := base.PickResourceID(id)
req.BandwidthPackageId = &id
_, err := base.BizClient.DeleteBandwidthPackage(req)
if err != nil {
base.HandleError(err)
return
}
base.Cxt.Printf("bandwidth package[%s] deleted\n", id)
}
},
}
flags := cmd.Flags()
flags.SortFlags = false
flags.StringSliceVar(&ids, "resource-id", nil, "Required, Resource ID of bandwidth package to delete")
req.Region = flags.String("region", base.ConfigIns.Region, "Optional. Region, see 'ucloud region'")
req.ProjectId = flags.String("project-id", base.ConfigIns.ProjectID, "Optional. Project-id, see 'ucloud project list'")

return cmd
}
25 changes: 5 additions & 20 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewCmdCompletion() *cobra.Command {
} else if strings.HasSuffix(shell, "zsh") {
zshCompletion(cmd)
} else {
fmt.Println("Unknow shell: %", shell)
fmt.Printf("So far, shell %s is not supported\n", shell)
}
} else {
fmt.Println("Lookup shell failed")
Expand All @@ -54,33 +54,18 @@ func NewCmdCompletion() *cobra.Command {
func bashCompletion(cmd *cobra.Command) {
platform := runtime.GOOS
if platform == "darwin" {
fmt.Println(`Please append 'complete -C /usr/local/bin/ucloud ucloud' to file '~/.bash_profile'
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"

if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
source ~/.ucloud/ucloud.sh`)
fmt.Println(`Please append 'complete -C $(which ucloud) ucloud' to file '~/.bash_profile'`)

} else if platform == "linux" {
fmt.Println(`Please append 'complete -C /usr/local/bin/ucloud ucloud' to file '~/.bashrc'
If the following scripts are included in '~/.bashrc', please remove it. Those scripts used to auto complete words before ucloud cli v0.1.3"

if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi

source ~/.ucloud/ucloud.sh`)
fmt.Println(`Please append 'complete -C $(which ucloud) ucloud' to file '~/.bashrc'`)
}
}

func zshCompletion(cmd *cobra.Command) {
fmt.Println(`Please append the following scripts to file '~/.zshrc'.

autoload -U +X bashcompinit && bashcompinit
complete -F /usr/local/bin/ucloud ucloud`)
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")
fmt.Printf("fpath=(~/%s $fpath)\n", base.ConfigPath)
fmt.Println("autoload -U +X compinit && compinit")
complete -F $(which ucloud) ucloud`)
}

func getBashVersion() (version string, err error) {
Expand Down
Loading