Skip to content

Commit

Permalink
Merge pull request #1 from letscloud-community/develop
Browse files Browse the repository at this point in the history
adding location in the instance data
  • Loading branch information
letscloud-inc committed Mar 7, 2021
2 parents 8014905 + 1eb2aa0 commit 0df62e3
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 59 deletions.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

```
$ letscloud instance list
IDENTIFIER LABEL IPv4 OS STATUS
rstpgafocoznzx my-new-label-1 38.143.68.93 CentOS 6.8 x86 running
xzosdqyydeyutr my-new-label-2 38.143.68.43 Ubuntu 20.04 x64 running
cqwdciprplqmkk my-new-label-3 38.143.68.53 CentOS 6.8 x86 running
xiflugipmecvfo my-new-label-4 38.143.68.29 Ubuntu 20.04 x64 running
IDENTIFIER LABEL IPv4 DC OS STATUS
rstpgafocoznzx my-new-label-1 38.143.68.93 MIA1 CentOS 6.8 x86 running
xzosdqyydeyutr my-new-label-2 38.143.68.43 LON1 Ubuntu 20.04 x64 running
cqwdciprplqmkk my-new-label-3 38.143.68.53 MIA2 CentOS 6.8 x86 running
xiflugipmecvfo my-new-label-4 38.143.68.29 SFO1 Ubuntu 20.04 x64 running
```

## Installation
Expand Down Expand Up @@ -45,15 +45,21 @@ If you have **Go and Makefile** installed, you can build and install the `letscl

## Examples

### Create new instance

```
$ letscloud instance create --location MIA2 --plan 1vcpu-1gb-10ssd --image centos-69-x64 --hostname test-api.com --label test-api
```

### List all instances

```
$ letscloud instance list
IDENTIFIER LABEL IPv4 OS STATUS
rstpgafocoznzx my-new-label-1 38.143.68.93 CentOS 6.8 x86 running
xzosdqyydeyutr my-new-label-2 38.143.68.43 Ubuntu 20.04 x64 running
cqwdciprplqmkk my-new-label-3 38.143.68.53 CentOS 6.8 x86 running
xiflugipmecvfo my-new-label-4 38.143.68.29 Ubuntu 20.04 x64 running
IDENTIFIER LABEL IPv4 DC OS STATUS
rstpgafocoznzx my-new-label-1 38.143.68.93 MIA1 CentOS 6.8 x86 running
xzosdqyydeyutr my-new-label-2 38.143.68.43 LON1 Ubuntu 20.04 x64 running
cqwdciprplqmkk my-new-label-3 38.143.68.53 MIA2 CentOS 6.8 x86 running
xiflugipmecvfo my-new-label-4 38.143.68.29 SFO1 Ubuntu 20.04 x64 running
```

### Create a New SSH Key
Expand Down
102 changes: 53 additions & 49 deletions internal/commands/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/jedib0t/go-pretty/v6/list"
"github.com/letscloud-community/letscloud-cli/internal/helpers"
"github.com/letscloud-community/letscloud-cli/internal/pkg/writer"
"github.com/letscloud-community/letscloud-go/domains"
"github.com/urfave/cli/v2"
Expand All @@ -30,22 +32,9 @@ func (c Commands) instanceCmd() *cli.Command {
}

wr := writer.New(os.Stdout)
wr.WriteHeader("IDENTIFIER", "LABEL", "IPv4", "OS", "STATUS")
wr.WriteHeader("IDENTIFIER", "LABEL", "IPv4", "DC", "OS", "STATUS")
for _, v := range srvs {
status := "off"
if v.Booted {
status = "running"
}
if v.Locked {
status = "action in progess"
}
if !v.Built {
status = "building"
}
if v.Suspended {
status = "suspended"
}
wr.WriteData(v.Identifier, v.Label, v.IPAddresses[0].Address, v.TemplateLabel, status)
wr.WriteData(v.Identifier, v.Label, v.IPAddresses[0].Address, v.Location.Slug, v.TemplateLabel, helpers.GetInstanceStatus(v.Booted, v.Locked, v.Built, v.Suspended))
}

return wr.Flush()
Expand All @@ -57,67 +46,82 @@ func (c Commands) instanceCmd() *cli.Command {
// Data Validation
identifier := ctx.Args().First()
if identifier == "" {
return errors.New("Identifier can not be empty. Please use `instance destroy <identifier>`")
return errors.New("Identifier can not be empty. Please use `instance details <identifier>`")
}
srv, err := c.sdk.Instance(identifier)
if err != nil {
return err
}

//fmt.Println(srv.location)

l := list.NewWriter()
lTemp := list.List{}
lTemp.Render()

l.AppendItem("OS")
l.AppendItem(srv.Label)
l.Indent()
l.AppendItems([]interface{}{"Cent OS 6.4"})
l.AppendItem("CPU " + strconv.Itoa(srv.CPUS))
l.AppendItem("RAM " + strconv.Itoa(srv.Memory) + " MB")
l.AppendItem("SSD " + strconv.Itoa(srv.TotalDiskSize) + " GB")
l.UnIndent()
l.AppendItems([]interface{}{"Status"})

l.AppendItem("Location")
l.Indent()
l.AppendItems([]interface{}{"running"})
l.AppendItem(srv.Location.Slug)
l.AppendItem(srv.Location.Country)
l.AppendItem(srv.Location.City)
l.UnIndent()

l.AppendItem("Hostname")
l.Indent()
l.AppendItem(srv.Hostname)
l.UnIndent()

l.AppendItem("IPv4")
l.Indent()
l.AppendItem("192.168.121.121")
l.AppendItem("192.168.121.121")
l.AppendItem("192.168.121.121")
l.AppendItem("192.168.121.121")
for _, IPAddress := range srv.IPAddresses {
if helpers.IsIpv4Regex(IPAddress.Address) {
l.AppendItem(IPAddress.Address)
}
}
l.UnIndent()

l.AppendItem("IPv6")
l.Indent()
l.AppendItem("sdfsdfsdfsdfsdccds")
l.AppendItem("sdfsdfsdfsdfsdccds")
l.AppendItem("sdfsdfsdfsdfsdccds")
l.AppendItem("sdfsdfsdfsdfsdccds")
for _, IPAddress := range srv.IPAddresses {
if !helpers.IsIpv4Regex(IPAddress.Address) {
l.AppendItem(IPAddress.Address)
}
}
l.UnIndent()

l.AppendItem("Distro")
l.Indent()
l.AppendItem(srv.TemplateLabel)
l.UnIndent()

l.AppendItem("Root Password")
l.Indent()
l.AppendItem(srv.RootPassword)
l.UnIndent()

l.AppendItem("Status")
l.Indent()
l.AppendItem(helpers.GetInstanceStatus(srv.Booted, srv.Locked, srv.Built, srv.Suspended))
l.UnIndent()

l.SetStyle(list.StyleConnectedRounded)

prefix := ""
title := "Instance " + identifier

fmt.Printf("%s\n", title)
fmt.Println(strings.Repeat("-", len(title)+1))
for _, line := range strings.Split(l.Render(), "\n") {
fmt.Printf("%s%s\n", prefix, line)
}
fmt.Println()

// // init new tab writer
// wr := writer.New(os.Stdout)
// // write headers
// wr.WriteHeader("IDENTIFIER", "LABEL", "IPv4", "OS", "STATUS")
// // write data value
// for _, v := range srvs {
// status := "off"
// if v.Booted {
// status = "running"
// }
// if v.Locked {
// status = "action in progess"
// }
// if !v.Built {
// status = "building"
// }
// wr.WriteData(v.Identifier, v.Label, v.IPAddresses[0].Address, v.TemplateLabel, status)
// }

// return wr.Flush()
return nil
},
},
Expand Down
33 changes: 33 additions & 0 deletions internal/helpers/instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package helpers

import (
"regexp"
"strings"
)

var (
ipRegex, _ = regexp.Compile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`)
)

func IsIpv4Regex(ipAddress string) bool {
ipAddress = strings.Trim(ipAddress, " ")
return ipRegex.MatchString(ipAddress)
}

func GetInstanceStatus(Booted bool, Locked bool, Built bool, Suspended bool) string {
status := "off"
if Booted {
status = "running"
}
if Locked {
status = "action in progess"
}
if !Built {
status = "building"
}
if Suspended {
status = "suspended"
}

return status
}

0 comments on commit 0df62e3

Please sign in to comment.