Skip to content

Commit

Permalink
All tools use common.HandleFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
hamstah committed Jul 18, 2019
1 parent a834741 commit 3a07287
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 81 deletions.
6 changes: 1 addition & 5 deletions aws/dump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()

accountsConfig = kingpin.Flag("accounts-config", "Configuration file with the accounts to list resources for.").Short('c').Required().String()
terraformBackendConfig = kingpin.Flag("terraform-backends-config", "Configuration file with the terraform backends to compare with.").Short('t').String()
output = kingpin.Flag("output", "Filename to store the results in.").Short('o').Required().String()
Expand All @@ -25,8 +22,7 @@ var (
func main() {
kingpin.CommandLine.Name = "aws-dump"
kingpin.CommandLine.Help = "Dump AWS resources"
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
common.HandleFlags()

accounts, err := NewAccounts(*accountsConfig)
common.FatalOnError(err)
Expand Down
5 changes: 1 addition & 4 deletions cloudwatch/put-metric-data/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
metricName = kingpin.Flag("metric-name", "Name of the Cloudwatch metric").Required().String()
namespace = kingpin.Flag("namespace", "Name of the Cloudwatch namespace").Required().String()
dimensions = kingpin.Flag("dimension", "Dimensions name=value").StringMap()
Expand All @@ -19,8 +17,7 @@ var (
func main() {
kingpin.CommandLine.Name = "cloudwatch-put-metric-data"
kingpin.CommandLine.Help = "Put a cloudwatch metric value."
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

session, conf := common.OpenSession(flags)

Expand Down
5 changes: 1 addition & 4 deletions ec2/describe-instances/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
filter = kingpin.Flag("filter", "The filter to use for the identifiers. eg tag:Name").String()
identifiers = kingpin.Arg("identifiers", "If omitted the instance is fetched from the EC2 metadata.").Strings()
)
Expand Down Expand Up @@ -47,8 +45,7 @@ type Result struct {
func main() {
kingpin.CommandLine.Name = "ec2-describe-instances"
kingpin.CommandLine.Help = "Returns metadata of one or more EC2 instances"
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

session, conf := common.OpenSession(flags)

Expand Down
5 changes: 1 addition & 4 deletions ec2/ip-from-name/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
name = kingpin.Flag("name", "Name of the EC2 instance").Required().String()
maxResults = kingpin.Flag("max-results", "Max number of IPs to return").Default("9").Int()
)

func main() {
kingpin.CommandLine.Name = "ec2-ip-from-name"
kingpin.CommandLine.Help = "Returns a list of instances IP with a given name."
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

session, conf := common.OpenSession(flags)

Expand Down
7 changes: 2 additions & 5 deletions ecr/get-login/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
output = kingpin.Flag("output", "Return the credentials instead of docker command").Default("shell").Enum("raw", "shell")
output = kingpin.Flag("output", "Return the credentials instead of docker command").Default("shell").Enum("raw", "shell")
)

func main() {
kingpin.CommandLine.Name = "ecr-get-login"
kingpin.CommandLine.Help = "Returns an authorization token from ECR."
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

session, conf := common.OpenSession(flags)

Expand Down
17 changes: 7 additions & 10 deletions ecs/deploy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
taskName = kingpin.Flag("task-name", "ECS task name").String()
cluster = kingpin.Flag("cluster", "ECS cluster").Required().String()
services = kingpin.Flag("service", "ECS services").Required().Strings()
images = kingpin.Flag("image", "Change the images to the new ones. Container name=image").StringMap()
timeout = kingpin.Flag("timeout", "Timeout when waiting for services to update").Default("300s").Duration()
taskJSON = kingpin.Flag("task-json", "Path to a JSON file with the task definition to use").String()
taskName = kingpin.Flag("task-name", "ECS task name").String()
cluster = kingpin.Flag("cluster", "ECS cluster").Required().String()
services = kingpin.Flag("service", "ECS services").Required().Strings()
images = kingpin.Flag("image", "Change the images to the new ones. Container name=image").StringMap()
timeout = kingpin.Flag("timeout", "Timeout when waiting for services to update").Default("300s").Duration()
taskJSON = kingpin.Flag("task-json", "Path to a JSON file with the task definition to use").String()
)

func main() {
kingpin.CommandLine.Name = "ecs-deploy"
kingpin.CommandLine.Help = "Update a task definition on ECS."
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

session, conf := common.OpenSession(flags)

Expand Down
5 changes: 1 addition & 4 deletions ecs/locate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
containerName = kingpin.Flag("container-name", "ECS container name").Required().String()
containerPort = kingpin.Flag("container-port", "ECS container port").Required().Int64()
cluster = kingpin.Flag("cluster", "ECS cluster").Required().String()
Expand All @@ -23,8 +21,7 @@ var (
func main() {
kingpin.CommandLine.Name = "ecs-locate"
kingpin.CommandLine.Help = "Find an instance/port for a service"
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

session, conf := common.OpenSession(flags)

Expand Down
5 changes: 1 addition & 4 deletions ecs/run-task/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
taskDefinition = kingpin.Flag("task-definition", "ECS task definition").Required().String()
cluster = kingpin.Flag("cluster", "ECS cluster").Required().String()
)

func main() {
kingpin.CommandLine.Name = "ecs-run-task"
kingpin.CommandLine.Help = "Run a task on ECS."
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

session, conf := common.OpenSession(flags)

Expand Down
5 changes: 1 addition & 4 deletions elb/resolve-alb-external-url/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
loadBalancerName = kingpin.Flag("name", "Name of the load balancer").Required().String()
dnsPrefix = kingpin.Flag("dns-prefix", "Prefix to match on the DNS").String()
)

func main() {
kingpin.CommandLine.Name = "elb-resolve-alb-external-url"
kingpin.CommandLine.Help = "Resolve the public URL of an ALB."
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

session, conf := common.OpenSession(flags)

Expand Down
5 changes: 1 addition & 4 deletions elb/resolve-elb-external-url/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
loadBalancerName = kingpin.Flag("name", "Name of the load balancer").Required().String()
)

func main() {

kingpin.CommandLine.Name = "elb-resolve-elb-external-url"
kingpin.CommandLine.Help = "Resolve the public URL of an ELB."
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

session, conf := common.OpenSession(flags)

Expand Down
5 changes: 1 addition & 4 deletions iam/auth-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
socksProxy = kingpin.Flag("socks-proxy", "Socks proxy host:port to use.").String()
bind = kingpin.Flag("bind", "Address to bind to").Default(":8080").String()
)
Expand Down Expand Up @@ -67,8 +65,7 @@ func setupSocks(p *goproxy.ProxyHttpServer, socksProxy string) error {
func main() {
kingpin.CommandLine.Name = "iam-auth-proxy"
kingpin.CommandLine.Help = "Proxy to generate IAM auth token"
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = false
Expand Down
11 changes: 4 additions & 7 deletions iam/public-ssh-keys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
username = kingpin.Flag("username", "Username to fetch the keys for, otherwise default to the logged in user.").Short('u').String()
encoding = kingpin.Flag("key-encoding", "Encoding of the key to return (SSH or PEM)").Default("SSH").Enum("PEM", "SSH")
groups = kingpin.Flag("allowed-group", "Fetch the keys only if the user is in this group. You can use --allowed-group multiple times.").Strings()
username = kingpin.Flag("username", "Username to fetch the keys for, otherwise default to the logged in user.").Short('u').String()
encoding = kingpin.Flag("key-encoding", "Encoding of the key to return (SSH or PEM)").Default("SSH").Enum("PEM", "SSH")
groups = kingpin.Flag("allowed-group", "Fetch the keys only if the user is in this group. You can use --allowed-group multiple times.").Strings()
)

func main() {
kingpin.CommandLine.Name = "iam-public-ssh-keys"
kingpin.CommandLine.Help = "Return public SSH keys for an IAM user."
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

session := session.Must(session.NewSession())
conf := common.AssumeRoleConfig(flags, session)
Expand Down
5 changes: 1 addition & 4 deletions iam/session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
quiet = kingpin.Flag("quiet", "Do not output anything").Short('q').Default("false").Bool()
saveProfileName = kingpin.Flag("save-profile", "Save the profile in the AWS credentials storage").Short('s').String()
overwriteProfile = kingpin.Flag("overwrite-profile", "Overwrite the profile if it already exists").Default("false").Bool()
Expand All @@ -26,8 +24,7 @@ var (
func main() {
kingpin.CommandLine.Name = "iam-session"
kingpin.CommandLine.Help = "Start a new session under a different role."
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

if len(*flags.RoleArn) == 0 && len(*saveProfileName) != 0 && len(*flags.MFASerialNumber) == 0 {
common.Fatalln("--save-profile can only be used with --assume-role-arn or --mfa-serial-number")
Expand Down
19 changes: 8 additions & 11 deletions kms/env/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
command = kingpin.Arg("command", "Command to run, prefix with -- to pass args").Required().Strings()
kmsPrefix = kingpin.Flag("kms-prefix", "Prefix for the KMS environment variables").Default("KMS_").String()
ssmPrefix = kingpin.Flag("ssm-prefix", "Prefix for the SSM environment variables").Default("SSM_").String()
Expand All @@ -50,12 +48,12 @@ func (c *Config) IsRefreshable() bool {
return len(c.Sources) > 0
}

func (c *Config) RefreshWithRetries() (map[string]string, error) {
func (c *Config) RefreshWithRetries(flags *common.SessionFlags) (map[string]string, error) {

wait := 2

for i := 0; i < *refreshMaxRetries; i++ {
result, err := c.Refresh()
result, err := c.Refresh(flags)
if err == nil {
return result, nil
}
Expand All @@ -66,7 +64,7 @@ func (c *Config) RefreshWithRetries() (map[string]string, error) {
return nil, errors.New("Failed to refresh config")
}

func (c *Config) Refresh() (map[string]string, error) {
func (c *Config) Refresh(flags *common.SessionFlags) (map[string]string, error) {
env := map[string]string{}

session, conf := common.OpenSession(flags)
Expand Down Expand Up @@ -122,8 +120,8 @@ func (c *Config) Refresh() (map[string]string, error) {
return env, nil
}

func Monitor(config *Config, comm chan<- map[string]string) {
previous, err := config.RefreshWithRetries()
func Monitor(flags *common.SessionFlags, config *Config, comm chan<- map[string]string) {
previous, err := config.RefreshWithRetries(flags)
if err != nil {
comm <- nil
return
Expand All @@ -135,7 +133,7 @@ func Monitor(config *Config, comm chan<- map[string]string) {
}

for _ = range time.Tick(*refreshInterval) {
new, err := config.RefreshWithRetries()
new, err := config.RefreshWithRetries(flags)
if err != nil {
comm <- nil
return
Expand Down Expand Up @@ -199,16 +197,15 @@ func ParseConfig(env []string) (*Config, error) {
func main() {
kingpin.CommandLine.Name = "kms-env"
kingpin.CommandLine.Help = "Decrypt environment variables encrypted with KMS, SSM or Secret Manager."
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

env := os.Environ()

config, err := ParseConfig(env)
common.FatalOnError(err)

comm := make(chan map[string]string, 1)
go Monitor(config, comm)
go Monitor(flags, config, comm)

var p *exec.Cmd

Expand Down
11 changes: 4 additions & 7 deletions s3/download/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ import (
)

var (
flags = common.KingpinSessionFlags()
infoFlags = common.KingpinInfoFlags()
bucket = kingpin.Flag("bucket", "Name of the bucket").Required().String()
key = kingpin.Flag("key", "Key to download").Required().String()
filename = kingpin.Flag("filename", "Output filename").Required().String()
bucket = kingpin.Flag("bucket", "Name of the bucket").Required().String()
key = kingpin.Flag("key", "Key to download").Required().String()
filename = kingpin.Flag("filename", "Output filename").Required().String()
)

func main() {
kingpin.CommandLine.Name = "s3-download"
kingpin.CommandLine.Help = "Download a file from S3."
kingpin.Parse()
common.HandleInfoFlags(infoFlags)
flags := common.HandleFlags()

session, conf := common.OpenSession(flags)

Expand Down

0 comments on commit 3a07287

Please sign in to comment.