Skip to content

Commit

Permalink
Merge pull request #9 from ReSTARTR/feature/args-profile
Browse files Browse the repository at this point in the history
Add `--profile` option
  • Loading branch information
ReSTARTR authored Apr 8, 2017
2 parents e6b0e54 + cce1bb2 commit 22fd2d9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Not Display field headers, if set true.
```
[options]
creds = shared
profile = default
region = ap-northeast-1
tags = Role:app,Env:production
fields = instance-id,tag:Name,public-ip,private-ip
Expand Down
3 changes: 2 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
)

type Options struct {
Profile string
Filters map[string]string
TagFilters map[string]string
Fields []string
Expand Down Expand Up @@ -63,7 +64,7 @@ func namesToUpper(strs []string) []string {
func Describe(o *Options, w *tabwriter.Writer) error {
// build queries
config := &aws.Config{Region: aws.String(o.Region)}
credentials, err := creds.SelectCredentials(o.Credentials)
credentials, err := creds.SelectCredentials(o.Credentials, o.Profile)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions creds/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const (
)

// creds "env" or "shared" or "ec2"
func SelectCredentials(creds string) (*credentials.Credentials, error) {
func SelectCredentials(creds string, profile string) (*credentials.Credentials, error) {
sharedCredsFile := os.Getenv("HOME") + SHARED_CREDS_FILENAME
switch creds {
case "env":
return credentials.NewEnvCredentials(), nil
case "shared":
return credentials.NewSharedCredentials(sharedCredsFile, SHARED_CREDS_PROFILE), nil
return credentials.NewSharedCredentials(sharedCredsFile, profile), nil
case "ec2":
return credentials.NewCredentials(&ec2rolecreds.EC2RoleProvider{
Client: ec2metadata.New(session.New()),
Expand All @@ -35,7 +35,7 @@ func SelectCredentials(creds string) (*credentials.Credentials, error) {
&credentials.EnvProvider{},
&credentials.SharedCredentialsProvider{
Filename: sharedCredsFile,
Profile: SHARED_CREDS_PROFILE,
Profile: profile,
},
&ec2rolecreds.EC2RoleProvider{
Client: ec2metadata.New(session.New()),
Expand Down
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ var (
version string
)

func loadRegionInAwsConfig() string {
func loadRegionInAwsConfig(profile string) string {
cfg, err := creds.LoadAwsConfig()
if err == nil {
return cfg.Section("default").Key("region").Value()
return cfg.Section(profile).Key("region").Value()
}
return ""
}
Expand Down Expand Up @@ -60,6 +60,7 @@ func parseFieldsString(str string) []string {
func optionsFromFile() *client.Options {
opt := client.NewOptions()
if cfg, err := loadConfig(); err == nil {
opt.Profile = cfg.Section("options").Key("profile").Value()
opt.Region = cfg.Section("options").Key("region").Value()
opt.TagFilters = parseFilterString(cfg.Section("options").Key("tags").Value())
opt.Fields = parseFieldsString(cfg.Section("options").Key("fields").Value())
Expand All @@ -77,6 +78,7 @@ func NewTableWriter() *tabwriter.Writer {

func main() {
// parse options
profile := flag.String("profile", "default", "profile name of aws credentials")
filters := flag.String("filters", "", "key1:value1,key2:value2,...")
tagFilters := flag.String("tags", "", "key1:value1,key2:value2,...")
fields := flag.String("fields", "", "column1,column2,...")
Expand All @@ -91,7 +93,8 @@ func main() {
}

opt := optionsFromFile()
awsConfigRegion := loadRegionInAwsConfig()
opt.Profile = *profile
awsConfigRegion := loadRegionInAwsConfig(opt.Profile)
if awsConfigRegion != "" {
opt.Region = awsConfigRegion
}
Expand Down

0 comments on commit 22fd2d9

Please sign in to comment.