Skip to content

Commit

Permalink
Merge pull request #29 from lixiaojun629/develop
Browse files Browse the repository at this point in the history
 add flags page-off and uhost-id-only for uhost list
  • Loading branch information
lixiaojun629 authored May 10, 2019
2 parents ab51416 + 9b5b86b commit ecbfc76
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
out
.vscode/
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.16
export VERSION=0.1.17

.PHONY : build
build:
Expand Down
2 changes: 1 addition & 1 deletion base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
const DefaultProfile = "default"

//Version 版本号
const Version = "0.1.16"
const Version = "0.1.17"

//ConfigIns 配置实例, 程序加载时生成
var ConfigIns = &AggConfig{}
Expand Down
106 changes: 74 additions & 32 deletions cmd/uhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,51 +75,88 @@ type UHostRow struct {
CreationTime string
}

func listUhost(uhosts []uhost.UHostInstanceSet, out io.Writer) {
list := make([]UHostRow, 0)
for _, host := range uhosts {
row := UHostRow{}
row.UHostName = host.Name
row.ResourceID = host.UHostId
row.Group = host.Tag
for _, ip := range host.IPSet {
if row.PublicIP != "" {
row.PublicIP += " | "
}
if ip.Type == "Private" {
row.PrivateIP = ip.IP
} else {
row.PublicIP += fmt.Sprintf("%s", ip.IP)
}
}
cupCore := host.CPU
memorySize := host.Memory / 1024
diskSize := 0
for _, disk := range host.DiskSet {
if disk.Type == "Data" || disk.Type == "Udisk" {
diskSize += disk.Size
}
}
row.Config = fmt.Sprintf("cpu:%d memory:%dG disk:%dG", cupCore, memorySize, diskSize)
row.Image = fmt.Sprintf("%s|%s", host.BasicImageId, host.BasicImageName)
row.CreationTime = base.FormatDate(host.CreateTime)
row.State = host.State
row.Type = host.UHostType + "/" + host.HostType
list = append(list, row)
}
base.PrintList(list, out)
}

func listUhostID(uhosts []uhost.UHostInstanceSet, out io.Writer) {
for _, u := range uhosts {
fmt.Fprintln(out, u.UHostId)
}
}

//NewCmdUHostList [ucloud uhost list]
func NewCmdUHostList(out io.Writer) *cobra.Command {
var pageoff, idOnly bool
req := base.BizClient.NewDescribeUHostInstanceRequest()
cmd := &cobra.Command{
Use: "list",
Short: "List all UHost Instances",
Long: `List all UHost Instances`,
Run: func(cmd *cobra.Command, args []string) {
resp, err := base.BizClient.DescribeUHostInstance(req)
if err != nil {
base.HandleError(err)
return
}
list := make([]UHostRow, 0)
for _, host := range resp.UHostSet {
row := UHostRow{}
row.UHostName = host.Name
row.ResourceID = host.UHostId
row.Group = host.Tag
for _, ip := range host.IPSet {
if row.PublicIP != "" {
row.PublicIP += " | "
if pageoff {
uhostList := make([]uhost.UHostInstanceSet, 0)
for limit, offset := 50, *req.Offset; ; offset += limit {
req.Offset = sdk.Int(offset)
req.Limit = sdk.Int(limit)
resp, err := base.BizClient.DescribeUHostInstance(req)
if err != nil {
base.HandleError(err)
return
}
if ip.Type == "Private" {
row.PrivateIP = ip.IP
} else {
row.PublicIP += fmt.Sprintf("%s", ip.IP)
uhostList = append(uhostList, resp.UHostSet...)
if resp.TotalCount <= offset+limit {
break
}
}
cupCore := host.CPU
memorySize := host.Memory / 1024
diskSize := 0
for _, disk := range host.DiskSet {
if disk.Type == "Data" || disk.Type == "Udisk" {
diskSize += disk.Size
}
if idOnly {
listUhostID(uhostList, out)
} else {
listUhost(uhostList, out)
}
} else {
resp, err := base.BizClient.DescribeUHostInstance(req)
if err != nil {
base.HandleError(err)
return
}
if idOnly {
listUhostID(resp.UHostSet, out)
} else {
listUhost(resp.UHostSet, out)
}
row.Config = fmt.Sprintf("cpu:%d memory:%dG disk:%dG", cupCore, memorySize, diskSize)
row.Image = fmt.Sprintf("%s|%s", host.BasicImageId, host.BasicImageName)
row.CreationTime = base.FormatDate(host.CreateTime)
row.State = host.State
row.Type = host.UHostType + "/" + host.HostType
list = append(list, row)
}
base.PrintList(list, out)
},
}
cmd.Flags().SortFlags = false
Expand All @@ -129,8 +166,13 @@ func NewCmdUHostList(out io.Writer) *cobra.Command {
cmd.Flags().StringSliceVar(&req.UHostIds, "uhost-id", make([]string, 0), "Optional. Resource ID of uhost instances, multiple values separated by comma(without space)")
req.Offset = cmd.Flags().Int("offset", 0, "Optional. Offset default 0")
req.Limit = cmd.Flags().Int("limit", 50, "Optional. Limit default 50, max value 100")
cmd.Flags().BoolVar(&pageoff, "page-off", false, "Optional. Paging or not. If assigned, the limit flag will be disabled and list all uhost instances")
cmd.Flags().BoolVar(&idOnly, "uhost-id-only", false, "Optional. Just display resource id of uhost")
bindGroup(req, cmd.Flags())

cmd.Flags().SetFlagValues("page-off", "true", "false")
cmd.Flags().SetFlagValues("uhost-id-only", "true", "false")

return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion model/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var ctx = Context{
os.Stdout,
writer: os.Stdout,
}

func TestPrintln(t *testing.T) {
Expand Down

0 comments on commit ecbfc76

Please sign in to comment.