Skip to content

Commit

Permalink
Merge pull request #4 from goldeneggg/color
Browse files Browse the repository at this point in the history
support printing state with color, and some refactoring
  • Loading branch information
goldeneggg authored Jan 3, 2017
2 parents 5c48178 + 900b768 commit 74eb407
Show file tree
Hide file tree
Showing 229 changed files with 125,484 additions and 39 deletions.
18 changes: 18 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ $ lsec2 -H

# print only private IP address
$ lsec2 -p

# print state with color
# ("running" is green, "stopped" is red, and others are yellow)
$ lsec2 -c
```

## Tips
Expand Down Expand Up @@ -182,7 +186,3 @@ Bugs: [issues](https://github.com/goldeneggg/lsec2/issues)

## Special Thanks
[@sugitak](https://github.com/sugitak)

## TODO
* [ ] Add tests
* [ ] Add multi configuration type(ex: support `~/.aws/config`, `~/.aws/credentials`)
13 changes: 9 additions & 4 deletions awsec2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
)

// Client is options definition of print
// client attributes
type Client struct {
PrintHeader bool
OnlyPrivateIP bool
Region string
Tags []string
WithColor bool
}

// Print is print method for aws ec2 instances
// print information of aws ec2 instances
func (client *Client) Print() error {
infos, err := client.buildInfos()
if err != nil {
Expand Down Expand Up @@ -62,10 +67,10 @@ func (client *Client) filterParams() *ec2.DescribeInstancesInput {
// ex. "Name=Value"
// ex. "Name=Value1,Value2"
for _, tag := range client.Tags {
tagNameValue := strings.Split(tag, TagPairSeparator)
name := aws.String(TagFilterPrefix + tagNameValue[0])
tagNameValue := strings.Split(tag, tagPairSeparator)
name := aws.String(tagFilterPrefix + tagNameValue[0])
values := make([]*string, 0, 3)
for _, value := range strings.Split(tagNameValue[1], TagValueSeparator) {
for _, value := range strings.Split(tagNameValue[1], tagValueSeparator) {
values = append(values, aws.String(value))
}

Expand All @@ -92,7 +97,7 @@ func (client *Client) printInfos(infos []*InstanceInfo) {
if client.OnlyPrivateIP {
fmt.Printf("%s\n", info.PrivateIPAddress)
} else {
info.printRow()
info.printRow(client.WithColor)
}
}
}
12 changes: 6 additions & 6 deletions awsec2/const.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package awsec2

const (
TagFilterPrefix = "tag:"
TagPairSeparator = "="
TagValueSeparator = ","
tagFilterPrefix = "tag:"
tagPairSeparator = "="
tagValueSeparator = ","

ParsedTagSeparator = ","
FieldSeparater = "\t"
parsedTagSeparator = ","
fieldSeparater = "\t"

UndefinedItem = "UNDEFINED"
undefinedItem = "UNDEFINED"
)
38 changes: 30 additions & 8 deletions awsec2/instance_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/fatih/color"
)

// InstanceInfo is attributes of aws ec2 instance
type InstanceInfo struct {
InstanceID string `header:"INSTANCE_ID"`
PrivateIPAddress string `header:"PRIVATE_IP"`
Expand All @@ -19,6 +21,7 @@ type InstanceInfo struct {
Tags map[string]string `header:"TAGS"`
}

// NewInstanceInfo creates a new InstanceInfo
func NewInstanceInfo(instance *ec2.Instance) (*InstanceInfo, error) {
if instance == nil {
return nil, fmt.Errorf("ec2.Instance: %v is invalid", instance)
Expand All @@ -44,18 +47,19 @@ func NewInstanceInfo(instance *ec2.Instance) (*InstanceInfo, error) {
return i, nil
}

func (info *InstanceInfo) ParseRow() string {
// ParseRow parses from InstanceInfo to one line string
func (info *InstanceInfo) ParseRow(withColor bool) string {
var values []string

values = append(values, info.InstanceID)
values = append(values, info.PrivateIPAddress)
values = append(values, info.PublicIPAddress)
values = append(values, info.InstanceType)
values = append(values, info.StateName)
values = append(values, info.decorateStateName(withColor))

values = append(values, strings.Join(info.parseTags(), ParsedTagSeparator))
values = append(values, strings.Join(info.parseTags(), parsedTagSeparator))

return fmt.Sprintf("%s", strings.Join(values, FieldSeparater))
return fmt.Sprintf("%s", strings.Join(values, fieldSeparater))
}

func (info *InstanceInfo) printHeader() {
Expand All @@ -67,11 +71,29 @@ func (info *InstanceInfo) printHeader() {
headers = append(headers, field.Tag.Get("header"))
}

fmt.Printf("%s\n", strings.Join(headers, FieldSeparater))
fmt.Printf("%s\n", strings.Join(headers, fieldSeparater))
}

func (info *InstanceInfo) printRow() {
fmt.Printf("%s\n", info.ParseRow())
func (info *InstanceInfo) printRow(withColor bool) {
fmt.Printf("%s\n", info.ParseRow(withColor))
}

func (info *InstanceInfo) decorateStateName(withColor bool) string {
if !withColor {
return info.StateName
}

var colorAttr color.Attribute
switch info.StateName {
case "running":
colorAttr = color.FgGreen
case "stopped":
colorAttr = color.FgRed
default:
colorAttr = color.FgYellow
}

return color.New(colorAttr).SprintFunc()(info.StateName)
}

func (info *InstanceInfo) parseTags() []string {
Expand All @@ -91,7 +113,7 @@ func (info *InstanceInfo) parseTags() []string {

func fetchItem(instanceItem *string) string {
if len(aws.StringValue(instanceItem)) == 0 {
return UndefinedItem
return undefinedItem
}

return *instanceItem
Expand Down
10 changes: 5 additions & 5 deletions awsec2/instance_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var infoTests = []infoTest{
expected: &InstanceInfo{
InstanceID: dummyInstanceID,
PrivateIPAddress: dummyPrivateIPAddress,
PublicIPAddress: UndefinedItem,
PublicIPAddress: "UNDEFINED",
InstanceType: dummyInstanceType,
StateName: dummyStateName,
Tags: map[string]string{
Expand All @@ -109,7 +109,7 @@ var infoTests = []infoTest{
expectedParsed: fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%s=%s",
dummyInstanceID,
dummyPrivateIPAddress,
UndefinedItem,
"UNDEFINED",
dummyInstanceType,
dummyStateName,
dummyTagKeys[0],
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestNewInstanceInfo(t *testing.T) {
for _, it := range infoTests {
out, err := NewInstanceInfo(it.in)
if err != nil {
t.Errorf("occured error: %v, in: %#v", err, it.in)
t.Errorf("occurred error: %v, in: %#v", err, it.in)
}
if !compare(out, it.expected) {
t.Errorf("expected: %#v, but out: %#v", it.expected, out)
Expand Down Expand Up @@ -193,13 +193,13 @@ func compare(out *InstanceInfo, expected *InstanceInfo) bool {
func TestNewInstanceInfoByNil(t *testing.T) {
out, err := NewInstanceInfo(nil)
if err == nil {
t.Errorf("not occured expected error. out: %#v", out)
t.Errorf("not occurred expected error. out: %#v", out)
}
}

func TestParseRow(t *testing.T) {
for _, it := range infoTests {
out := it.expected.ParseRow()
out := it.expected.ParseRow(false)
if out != it.expectedParsed {
t.Errorf("expected: [%s], but out: [%s]", it.expectedParsed, out)
}
Expand Down
15 changes: 4 additions & 11 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ import (

import "github.com/goldeneggg/lsec2/awsec2"

/*
var commands = []cli.Command{
cli.Command{
Name: "show",
Usage: "show instance list",
Flags: showFlags,
Action: action,
},
}
*/

func run(args []string) error {
app := cli.NewApp()

Expand Down Expand Up @@ -55,6 +44,10 @@ func action(c *cli.Context) {
client.PrintHeader = false
}

if c.IsSet("c") {
client.WithColor = c.Bool("c")
}

if err := client.Print(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
sts = exitStsNg
Expand Down
4 changes: 4 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ var showFlags = []cli.Flag{
Name: "show-build",
Usage: "show build info",
},
cli.BoolFlag{
Name: "with-color, c",
Usage: "print state with color",
},
}
5 changes: 5 additions & 0 deletions vendor/github.com/fatih/color/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/fatih/color/LICENSE.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 74eb407

Please sign in to comment.