Permalink
Browse files

cleanup, fix aws calls, add makefile

  • Loading branch information...
peter-edge committed Oct 9, 2015
1 parent 47bcaf4 commit 9962a9bc839e274c3eec5daf8ad13e29ae22232b
Showing with 126 additions and 100 deletions.
  1. +53 −0 Makefile
  2. +19 −15 common/aws/commonaws.go
  3. +0 −59 common/common.go
  4. +11 −6 ecs-list-ips/main.go
  5. +5 −3 go-benchmark-columns/main.go
  6. +13 −8 go-external-deps/main.go
  7. +8 −3 godep-update/main.go
  8. +8 −4 protoc-all/main.go
  9. +9 −2 sqs-purge/main.go
@@ -0,0 +1,53 @@
all: test

deps:
go get -d -v ./...

updatedeps:
go get -d -v -u -f ./...

testdeps:
go get -d -v -t ./...

updatetestdeps:
go get -d -v -t -u -f ./...

build: deps
go build ./...

install: deps
go install ./...

lint: testdeps
go get -v github.com/golang/lint/golint
golint ./...

vet: testdeps
go vet ./...

errcheck: testdeps
go get -v github.com/kisielk/errcheck
errcheck ./...

pretest: lint

test: testdeps pretest
go test ./...

clean:
go clean -i ./...

.PHONY: \
all \
deps \
updatedeps \
testdeps \
updatetestdeps \
build \
install \
lint \
vet \
errcheck \
pretest \
test \
clean
@@ -5,25 +5,29 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/peter-edge/go-cacerts"
"go.pedge.io/env"
"go.pedge.io/tools/common"
)

type awsEnv struct {
AwsRegion string `env:"AWS_REGION,required"`
}

func Main(do func(*aws.Config) error) {
common.Main(func() error {
var awsEnv awsEnv
if err := env.Populate(&awsEnv, env.PopulateOptions{}); err != nil {
return err
}
return do(
&aws.Config{
Credentials: credentials.NewEnvCredentials(),
HTTPClient: cacerts.NewHTTPClient(),
Region: aws.String(awsEnv.AwsRegion),
},
)
})
// Do calls the given function with an *aws.Config.
func Do(f func(*aws.Config) error) error {
awsConfig, err := getAWSConfig()
if err != nil {
return err
}
return f(awsConfig)
}

func getAWSConfig() (*aws.Config, error) {
var awsEnv awsEnv
if err := env.Populate(&awsEnv, env.PopulateOptions{}); err != nil {
return nil, err
}
return &aws.Config{
Credentials: credentials.NewEnvCredentials(),
HTTPClient: cacerts.NewHTTPClient(),
Region: aws.String(awsEnv.AwsRegion),
}, nil
}

This file was deleted.

Oops, something went wrong.
@@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"
"sort"

"go.pedge.io/env"
@@ -17,7 +18,11 @@ type appEnv struct {
}

func main() {
commonaws.Main(do)
if err := commonaws.Do(do); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
os.Exit(0)
}

func do(awsConfig *aws.Config) error {
@@ -37,20 +42,20 @@ func do(awsConfig *aws.Config) error {
describeContainerInstancesOutput, err := ecsClient.DescribeContainerInstances(
&ecs.DescribeContainerInstancesInput{
Cluster: aws.String(appEnv.EcsCluster),
ContainerInstances: listContainerInstancesOutput.ContainerInstanceARNs,
ContainerInstances: listContainerInstancesOutput.ContainerInstanceArns,
},
)
if err != nil {
return err
}
ec2InstanceIDs := make([]*string, len(describeContainerInstancesOutput.ContainerInstances))
for i, containerInstance := range describeContainerInstancesOutput.ContainerInstances {
ec2InstanceIDs[i] = containerInstance.EC2InstanceID
ec2InstanceIDs[i] = containerInstance.Ec2InstanceId
}
ec2Client := ec2.New(awsConfig)
describeInstancesOutput, err := ec2Client.DescribeInstances(
&ec2.DescribeInstancesInput{
InstanceIDs: ec2InstanceIDs,
InstanceIds: ec2InstanceIDs,
},
)
if err != nil {
@@ -59,8 +64,8 @@ func do(awsConfig *aws.Config) error {
var publicDNSNames []string
for _, reservation := range describeInstancesOutput.Reservations {
for _, instance := range reservation.Instances {
if instance.PublicDNSName != nil {
publicDNSNames = append(publicDNSNames, *instance.PublicDNSName)
if instance.PublicDnsName != nil {
publicDNSNames = append(publicDNSNames, *instance.PublicDnsName)
}
}
}
@@ -7,8 +7,6 @@ import (
"io"
"os"
"strings"

"go.pedge.io/tools/common"
)

const (
@@ -17,7 +15,11 @@ const (
)

func main() {
common.Main(do)
if err := do(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
os.Exit(0)
}

func do() error {
@@ -2,18 +2,23 @@ package main

import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strings"

"go.pedge.io/tools/common"
"go.pedge.io/pkg/exec"
)

func main() {
common.Main(do)
if err := do(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
os.Exit(0)
}

func do() error {
@@ -28,21 +33,21 @@ func do() error {
if strings.HasPrefix(pkg, ".") || strings.HasSuffix(pkg, "/") || strings.HasSuffix(pkg, ".") {
return fmt.Errorf("just use actual package name, not . at the beginning or / or /... at the end")
}
reader, err := common.RunStdout("go", "list", "-f", "{{join .Deps \"\\n\"}}", fmt.Sprintf("%s/...", pkg))
if err != nil {
buffer := bytes.NewBuffer(nil)
if err := pkgexec.RunStdout(buffer, "go", "list", "-f", "{{join .Deps \"\\n\"}}", fmt.Sprintf("%s/...", pkg)); err != nil {
return err
}
lines, err := getUniqueSortedLines(reader)
lines, err := getUniqueSortedLines(buffer)
if err != nil {
return err
}
args := []string{"go", "list", "-f", "{{if not .Standard}}{{.ImportPath}}{{end}}"}
args = append(args, lines...)
reader, err = common.RunStdout(args...)
if err != nil {
buffer = bytes.NewBuffer(nil)
if err = pkgexec.RunStdout(buffer, args...); err != nil {
return err
}
lines, err = getUniqueSortedLines(reader)
lines, err = getUniqueSortedLines(buffer)
if err != nil {
return err
}
@@ -4,14 +4,19 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"go.pedge.io/tools/common"
"go.pedge.io/pkg/exec"
)

func main() {
common.Main(do)
if err := do(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
os.Exit(0)
}

type godeps struct {
@@ -44,7 +49,7 @@ func do() error {
if _, ok := successImportPaths[importPath]; ok {
break
}
if err := common.Run("godep", "update", importPath+"/..."); err == nil {
if err := pkgexec.Run("godep", "update", importPath+"/..."); err == nil {
fmt.Println(importPath)
successImportPaths[importPath] = true
break
@@ -14,7 +14,7 @@ import (
"strings"

"go.pedge.io/env"
"go.pedge.io/tools/common"
"go.pedge.io/pkg/exec"

"github.com/fsouza/go-dockerclient"
)
@@ -47,7 +47,11 @@ type appEnv struct {
}

func main() {
common.Main(do)
if err := do(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
os.Exit(0)
}

func do() error {
@@ -209,10 +213,10 @@ func chownAll(protologFileName string, protocFilePaths []string) error {
func chown(filePath string) error {
uid := fmt.Sprintf("%d", os.Getuid())
gid := fmt.Sprintf("%d", os.Getgid())
if err := common.RunWithOptions(common.RunOptions{Stdout: os.Stderr, Stderr: os.Stderr}, "sudo", "chown", uid, filePath); err != nil {
if err := pkgexec.RunIO(pkgexec.IO{Stdout: os.Stderr, Stderr: os.Stderr}, "sudo", "chown", uid, filePath); err != nil {
return err
}
if err := common.RunWithOptions(common.RunOptions{Stdout: os.Stderr, Stderr: os.Stderr}, "sudo", "chgrp", gid, filePath); err != nil {
if err := pkgexec.RunIO(pkgexec.IO{Stdout: os.Stderr, Stderr: os.Stderr}, "sudo", "chgrp", gid, filePath); err != nil {
return err
}
return nil
@@ -1,6 +1,9 @@
package main

import (
"fmt"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"go.pedge.io/env"
@@ -12,7 +15,11 @@ type appEnv struct {
}

func main() {
commonaws.Main(do)
if err := commonaws.Do(do); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
os.Exit(0)
}

func do(awsConfig *aws.Config) error {
@@ -31,7 +38,7 @@ func do(awsConfig *aws.Config) error {
}
_, err = sqsClient.PurgeQueue(
&sqs.PurgeQueueInput{
QueueURL: createQueueOutput.QueueURL,
QueueUrl: createQueueOutput.QueueUrl,
},
)
return err

0 comments on commit 9962a9b

Please sign in to comment.