diff --git a/Makefile b/Makefile index 1f05778..c34f48d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # Change this and commit to create new release -override VERSION ?= 0.4.0 +override VERSION ?= 0.4.1 NAME = sigil REPO = danmx/$(NAME) MODULE = github.com/$(REPO) diff --git a/cmd/list.go b/cmd/list.go index f3851af..2fec445 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -82,7 +82,7 @@ Filter format examples: var filters aws.Filters if err := cfg.UnmarshalKey("filters", &filters); err != nil { log.Error("failed unmarshaling filters") - return err + return fmt.Errorf("failed unmarshaling filters: %s", err) } outputFormat := cfg.GetString("output-format") profile := cfg.GetString("profile") @@ -92,7 +92,7 @@ Filter format examples: instanceIDs := cfg.GetStringSlice("filters.instance.ids") // hack to get map[string]string from args // https://github.com/spf13/viper/issues/608 - if cfg.IsSet("filters.session") { + if cmd.Flags().Changed("session-filters") { filters.Session = aws.SessionFilters{ After: sessionFilters["after"], Before: sessionFilters["before"], @@ -100,15 +100,14 @@ Filter format examples: Owner: sessionFilters["owner"], } } - if cfg.IsSet("filters.instance.ids") { + if cmd.Flags().Changed("instance-ids") { filters.Instance.IDs = instanceIDs } var tags []aws.TagValues - if cfg.IsSet("filters.instance.tags") { - + if cmd.Flags().Changed("instance-tags") { if err := json.Unmarshal([]byte(cfg.GetString("filters.instance.tags")), &tags); err != nil { log.WithField("tags", cfg.GetString("filters.instance.tags")).Error("failed unmarshaling tags") - return err + return fmt.Errorf("failed unmarshaling tags: %s", err) } filters.Instance.Tags = tags } diff --git a/pkg/aws/list.go b/pkg/aws/list.go index e225a89..1724482 100644 --- a/pkg/aws/list.go +++ b/pkg/aws/list.go @@ -23,16 +23,6 @@ func (p *Provider) ListInstances() ([]*Instance, error) { }, }, } - for _, tag := range p.filters.Instance.Tags { - log.WithFields(log.Fields{ - "key": tag.Key, - "values": tag.Values, - }).Debug("Tags Filter") - ssmDescribeInstancesInput.Filters = append(ssmDescribeInstancesInput.Filters, &ssm.InstanceInformationStringFilter{ - Key: aws.String("tag:" + tag.Key), - Values: aws.StringSlice(tag.Values), - }) - } if len(p.filters.Instance.IDs) > 0 { log.WithFields(log.Fields{ "IDs": p.filters.Instance.IDs, @@ -86,11 +76,22 @@ func (p *Provider) ListInstances() ([]*Instance, error) { }, }, } + + for _, tag := range p.filters.Instance.Tags { + log.WithFields(log.Fields{ + "key": tag.Key, + "values": tag.Values, + }).Debug("Tags Filter") + describeInstancesInput.Filters = append(describeInstancesInput.Filters, &ec2.Filter{ + Name: aws.String("tag:" + tag.Key), + Values: aws.StringSlice(tag.Values), + }) + } + outputInstances := make([]*Instance, 0, len(instances)) // Discovering instances private DNS names for _, instance := range instances { describeInstancesInput.InstanceIds = append(describeInstancesInput.InstanceIds, &instance.ID) - outputInstances = append(outputInstances, instance) } ec2Client := ec2.New(p.session) err = ec2Client.DescribeInstancesPages(describeInstancesInput, @@ -106,6 +107,7 @@ func (p *Provider) ListInstances() ([]*Instance, error) { } instances[*instance.InstanceId].PrivateDNSName = *instance.PrivateDnsName instances[*instance.InstanceId].Name = nameTag + outputInstances = append(outputInstances, instances[*instance.InstanceId]) } } return !lastPage @@ -152,7 +154,9 @@ func (p *Provider) ListSessions() ([]*Session, error) { Value: &p.filters.Session.Owner, }) } - ssmDescribeSessionsInput.Filters = filters + if len(filters) > 0 { + ssmDescribeSessionsInput.Filters = filters + } ssmClient := ssm.New(p.session) sessions := []*Session{} for out, err := ssmClient.DescribeSessions(ssmDescribeSessionsInput); ; {