Skip to content

Commit

Permalink
Merge pull request #130 from synfinatic/history-time
Browse files Browse the repository at this point in the history
Add timestamp to history & fix sorting
  • Loading branch information
synfinatic authored Nov 14, 2021
2 parents f2cb9c6 + dc82126 commit 7592ed6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

## [Unreleased]

## [v1.3.0] - 2021-11-14

* Add report card and make improvements to code style #124
* Add auto-complete support #12
* Add golangci-lint support & config file

* Sort History tag based on time, not alphabetical
* History entries now have how long since it was last used #123

## [v1.2.3] - 2021-11-13

* Add support for tracking recently used roles via History tag for exec & console #29
Expand Down Expand Up @@ -84,7 +88,8 @@
## [v1.0.0] - 2021-07-15

Initial release
[Unreleased]: https://github.com/synfinatic/aws-sso-cli/compare/v1.2.3...main
[Unreleased]: https://github.com/synfinatic/aws-sso-cli/compare/v1.3.0...main
[v1.3.0]: https://github.com/synfinatic/aws-sso-cli/releases/tag/v1.3.0
[v1.2.3]: https://github.com/synfinatic/aws-sso-cli/releases/tag/v1.2.3
[v1.2.2]: https://github.com/synfinatic/aws-sso-cli/releases/tag/v1.2.2
[v1.2.1]: https://github.com/synfinatic/aws-sso-cli/releases/tag/v1.2.1
Expand Down
2 changes: 1 addition & 1 deletion cmd/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func completeTags(roleTags *sso.RoleTags, allTags *sso.TagsList, accountPrimaryT
continue

case 1:
desc = checkRoles[0]
desc = checkRoles[0] // the ARN of the role we selected

default:
desc = fmt.Sprintf("%d roles", roleCnt)
Expand Down
34 changes: 19 additions & 15 deletions sso/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,25 @@ func (c *Cache) Save(updateTime bool) error {
// adds a role to the History list up to the max number of entries
// and then removes the History tag from any roles that aren't in our list
func (c *Cache) AddHistory(item string, max int) {
// first make sure it's not already in the list because we don't want duplicates
duplicate := false
for _, h := range c.History {
// If it's already in the list, remove it
for x, h := range c.History {
if h == item {
duplicate = true
// delete from history
c.History = append(c.History[:x], c.History[x+1:]...)
break
}
}

if !duplicate {
c.History = append([]string{item}, c.History...) // push on top
for len(c.History) > max {
// remove the oldest entry
c.History = c.History[:len(c.History)-1]
}
aId, roleName, _ := utils.ParseRoleARN(item)
c.History = append([]string{item}, c.History...) // push on top
for len(c.History) > max {
// remove the oldest entry
c.History = c.History[:len(c.History)-1]
}
aId, roleName, _ := utils.ParseRoleARN(item)

if a, ok := c.Roles.Accounts[aId]; ok {
if r, ok := a.Roles[roleName]; ok {
r.Tags["History"] = fmt.Sprintf("%s:%s", a.Alias, roleName)
}
if a, ok := c.Roles.Accounts[aId]; ok {
if r, ok := a.Roles[roleName]; ok {
r.Tags["History"] = fmt.Sprintf("%s:%s,%d", a.Alias, roleName, time.Now().Unix())
}
}

Expand Down Expand Up @@ -351,6 +349,9 @@ func (c *Cache) GetAllTagsSelect() *TagsList {
for k, values := range *tags {
key := strings.ReplaceAll(k, " ", "_")
for _, v := range values {
if key == "History" {
v = reformatHistory(v)
}
fixedTags.Add(key, strings.ReplaceAll(v, " ", "_"))
}
}
Expand All @@ -366,6 +367,9 @@ func (c *Cache) GetRoleTagsSelect() *RoleTags {
ret[role.Arn] = map[string]string{}
for k, v := range role.Tags {
key := strings.ReplaceAll(k, " ", "_")
if key == "History" {
v = reformatHistory(v)
}
value := strings.ReplaceAll(v, " ", "_")
ret[role.Arn][key] = value
}
Expand Down
36 changes: 36 additions & 0 deletions sso/tags_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ package sso
*/

import (
"fmt"
"sort"
"strconv"
"strings"
"time"

log "github.com/sirupsen/logrus"
)

// TagsList provides the necessary struct finding all the possible tag key/values
Expand All @@ -32,6 +38,7 @@ func NewTagsList() *TagsList {
// Inserts the tag/value if it does not already exist in the sorted order
func (t *TagsList) Add(tag, v string) {
tt := *t

if tt[tag] == nil {
tt[tag] = []string{v}
return // inserted
Expand Down Expand Up @@ -95,9 +102,38 @@ func (t *TagsList) UniqueKeys(picked []string) []string {
return keys
}

// reformatHistory modifies the History tag values to their human format for the selector
func reformatHistory(value string) string {
x := strings.Split(value, ",")

// oldformat
if len(x) == 1 {
return value
}

i, err := strconv.ParseInt(x[1], 10, 64)
if err != nil {
log.WithError(err).Fatalf("Unable to parse: %s", value)
}

d := time.Since(time.Unix(i, 0)).Truncate(time.Second)
var s string

if d.Hours() >= 1 {
s = d.String()
} else if d.Minutes() >= 1 {
s = fmt.Sprintf("0h%s", d.String())
} else {
s = fmt.Sprintf("0h0m%s", d.String())
}

return fmt.Sprintf("[%s] %s", s, x[0])
}

// Returns a sorted unique list of tag values for the given key
func (t *TagsList) UniqueValues(key string) []string {
x := *t

if values, ok := x[key]; ok {
sort.Strings(values)
return values
Expand Down

0 comments on commit 7592ed6

Please sign in to comment.