Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions command/cmdHelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (cmd *HelpCommand) Apply(m *manager.Manager) error {
update
-t|-timeout (duration) [optional = 5*time.Second]
-r|-retry (duration) [optional = 0*time.Second]
-k|-keys (string) [optional = ""]

ls
-r|-regex (bool) [optional = false]
Expand Down
11 changes: 10 additions & 1 deletion command/cmdUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"flag"
"strings"
"time"

"github.com/StackAdapt/systags/manager"
Expand All @@ -11,6 +12,7 @@ type UpdateCommand struct {
baseCommand
timeout time.Duration
retry time.Duration
keys string
}

func NewUpdateCommand() *UpdateCommand {
Expand All @@ -25,6 +27,8 @@ func NewUpdateCommand() *UpdateCommand {
cmd.flagSet.DurationVar(&cmd.timeout, "timeout", 5*time.Second, "")
cmd.flagSet.DurationVar(&cmd.retry, "r", 0*time.Second, "") // TODO: Does this get overwritten by `--retry` default?
cmd.flagSet.DurationVar(&cmd.retry, "retry", 0*time.Second, "")
cmd.flagSet.StringVar(&cmd.keys, "k", "", "")
cmd.flagSet.StringVar(&cmd.keys, "keys", "", "")

// Don't print unneeded usage
cmd.flagSet.Usage = func() {}
Expand All @@ -39,7 +43,12 @@ func (cmd *UpdateCommand) Apply(m *manager.Manager) error {
return err
}

err = m.UpdateRemote(cmd.timeout, cmd.retry)
var keys []string
if cmd.keys != "" {
keys = strings.Split(cmd.keys, ",")
}

err = m.UpdateRemote(cmd.timeout, cmd.retry, keys)
if err != nil {
return err
}
Expand Down
24 changes: 23 additions & 1 deletion manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ func (m *Manager) SaveFiles() error {
// retry duration is provided, this function will auto
// retry for the duration if empty tags are returned.
// A bounded exponential backoff strategy is employed.
func (m *Manager) UpdateRemote(timeout time.Duration, retry time.Duration) error {
// If the required keys slice is provided, the function
// will keep retrying until all the required keys are
// present in the fetched tags or until the retry
// duration is reached.
func (m *Manager) UpdateRemote(timeout time.Duration, retry time.Duration, requiredKeys []string) error {

// TODO:
// At the moment, only AWS is supported, but if you want
Expand All @@ -292,12 +296,28 @@ func (m *Manager) UpdateRemote(timeout time.Duration, retry time.Duration) error
startTime := time.Now()
untilTime := startTime.Add(retry)

hasRequiredKeys := func(tags Tags) bool {

for _, k := range requiredKeys {
if _, ok := tags[k]; !ok {
return false
}
}

return true
}

for {
res, err = getAwsTags(m.GetLogger(), timeout)
if err != nil {
return err
}

// Check whether all the required keys have values
if len(requiredKeys) > 0 && hasRequiredKeys(res) {
break
}

// Tags are not empty or we have reached time limit
if len(res) > 0 || time.Since(startTime) > retry {
break
Expand All @@ -318,6 +338,8 @@ func (m *Manager) UpdateRemote(timeout time.Duration, retry time.Duration) error
}
}

// TODO: Should we return an error if required keys and len(res) condition not met?

m.remote = res
return nil
}
Expand Down