Skip to content

Instantly share code, notes, and snippets.

@byronmansfield
Created January 17, 2016 19:40
Show Gist options
  • Save byronmansfield/f1fd5c99e23b2af1371d to your computer and use it in GitHub Desktop.
Save byronmansfield/f1fd5c99e23b2af1371d to your computer and use it in GitHub Desktop.
refactor aws-ec2 main
package main
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
type Params struct {
region, name, env string
}
type instanceOutput struct {
name, env, id, private_ip, state, launched, instance_type string
}
var regionPtr string
var envPtr string
func init() {
flag.StringVar(&regionPtr, "region", "us-west-1", "which region")
flag.StringVar(&envPtr, "environment", "", "which environment")
flag.StringVar(&namePtr, "name", "", "which instance name")
flag.Parse()
}
func makeParams(r, n, e string) *Params {
if envPtr == "" && namePtr == "" {
return nil
} else {
params := &Params{
region: r,
name: n,
env: e,
}
return params
}
}
func getInstances(p, *Params) instanceOutput {
svc := ec2.New(session.New(), &aws.Config{
Region: &regionPtr,
})
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Name: aws.String("tag-value"),
Values: []*string{
aws.String(p.region),
aws.String(p.name),
aws.String(p.env),
},
},
},
}
resp, err := svc.DescribeInstances(&p)
CheckErr(err)
}
func main() {
params := makeParams(regionPtr, namePtr, envPtr)
getInstances(params)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment