-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from systemli/configurable-expiration-duration
make expiration duration for pads configurable
- Loading branch information
Showing
10 changed files
with
408 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package helper | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
const DefaultSuffix = "default" | ||
|
||
type PadExpiration map[string]time.Duration | ||
|
||
// ParsePadExpiration splits a string with format "default:30d,temp:24h,keep:365d" and returns a PadExpiration type. | ||
// The key "default:<duration>" is mandatory in the input string. | ||
func ParsePadExpiration(s string) (PadExpiration, error) { | ||
exp := make(PadExpiration) | ||
|
||
if s == "" { | ||
return exp, errors.New("input string is empty") | ||
} | ||
|
||
for _, str := range strings.Split(s, ",") { | ||
split := strings.Split(str, ":") | ||
if len(split) != 2 { | ||
log.WithField("string", str).Error("string is not valid") | ||
continue | ||
} | ||
duration, err := time.ParseDuration(split[1]) | ||
if err != nil { | ||
log.WithError(err).WithField("duration", split[1]).Error("unable to parse the duration") | ||
continue | ||
} | ||
|
||
exp[split[0]] = duration | ||
} | ||
|
||
if _, ok := exp[DefaultSuffix]; !ok { | ||
return exp, errors.New("missing default expiration duration") | ||
} | ||
|
||
return exp, nil | ||
} | ||
|
||
// GetDuration tries to get the Duration by pad name, returns the default duration if no suffix matches. | ||
func (pe *PadExpiration) GetDuration(pad string) time.Duration { | ||
for suffix, duration := range *pe { | ||
if strings.HasSuffix(pad, fmt.Sprintf("-%s", suffix)) { | ||
return -duration | ||
} | ||
} | ||
|
||
return -(*pe)[DefaultSuffix] | ||
} | ||
|
||
// GroupPadsByExpiration sorts pads for the given expiration and returns a map with string keys and string slices. | ||
func GroupPadsByExpiration(pads []string, expiration PadExpiration) map[string][]string { | ||
var suffixes []string | ||
for suffix := range expiration { | ||
if suffix == DefaultSuffix { | ||
continue | ||
} | ||
suffixes = append(suffixes, suffix) | ||
} | ||
|
||
return GroupPadsBySuffixes(pads, suffixes) | ||
} | ||
|
||
// GroupPadsBySuffixes sorts pads for the given suffixes and returns a map with string keys and string slices. | ||
func GroupPadsBySuffixes(pads, suffixes []string) map[string][]string { | ||
sorted := make(map[string][]string) | ||
|
||
for _, pad := range pads { | ||
found := false | ||
for _, suffix := range suffixes { | ||
if strings.HasSuffix(pad, fmt.Sprintf("-%s", suffix)) { | ||
sorted[suffix] = append(sorted[suffix], pad) | ||
found = true | ||
} | ||
} | ||
if !found { | ||
sorted[DefaultSuffix] = append(sorted[DefaultSuffix], pad) | ||
} | ||
} | ||
|
||
return sorted | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package helper | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParsePadExpiration(t *testing.T) { | ||
s := "default:720h,temp:24h,keep:262800h" | ||
exp, err := ParsePadExpiration(s) | ||
assert.Nil(t, err) | ||
assert.Equal(t, time.Duration(2592000000000000), exp[DefaultSuffix]) | ||
assert.Equal(t, time.Duration(946080000000000000), exp["keep"]) | ||
assert.Equal(t, time.Duration(86400000000000), exp["temp"]) | ||
|
||
s = "wrong:1d" | ||
|
||
_, err = ParsePadExpiration(s) | ||
assert.Error(t, err) | ||
assert.Equal(t, "missing default expiration duration", err.Error()) | ||
|
||
s = "wrong:1h:2h" | ||
_, err = ParsePadExpiration(s) | ||
assert.Error(t, err) | ||
assert.Equal(t, "missing default expiration duration", err.Error()) | ||
|
||
s = "" | ||
_, err = ParsePadExpiration(s) | ||
assert.Error(t, err) | ||
assert.Equal(t, "input string is empty", err.Error()) | ||
} | ||
|
||
func TestPadExpiration_GetDuration(t *testing.T) { | ||
s := "default:24h" | ||
exp, err := ParsePadExpiration(s) | ||
assert.Nil(t, err) | ||
|
||
dur := exp.GetDuration("pad") | ||
assert.Equal(t, "-24h0m0s", dur.String()) | ||
|
||
s = "default:24h,temp:10m" | ||
exp, err = ParsePadExpiration(s) | ||
assert.Nil(t, err) | ||
|
||
dur = exp.GetDuration("pad") | ||
assert.Equal(t, "-24h0m0s", dur.String()) | ||
|
||
dur = exp.GetDuration("pad-temp") | ||
assert.Equal(t, "-10m0s", dur.String()) | ||
} | ||
|
||
func TestGroupPadsByExpiration(t *testing.T) { | ||
s := "default:720h,temp:24h,keep:262800h" | ||
pads := []string{"pad", "pad2", "pad-keep", "pad-temp"} | ||
exp, err := ParsePadExpiration(s) | ||
assert.Nil(t, err) | ||
|
||
sorted := GroupPadsByExpiration(pads, exp) | ||
|
||
if _, ok := sorted[DefaultSuffix]; !ok { | ||
t.Fail() | ||
} | ||
|
||
if _, ok := sorted["keep"]; !ok { | ||
t.Fail() | ||
} | ||
|
||
if _, ok := sorted["temp"]; !ok { | ||
t.Fail() | ||
} | ||
|
||
assert.Equal(t, []string{"pad", "pad2"}, sorted[DefaultSuffix]) | ||
assert.Equal(t, []string{"pad-keep"}, sorted["keep"]) | ||
assert.Equal(t, []string{"pad-temp"}, sorted["temp"]) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.