This repository has been archived by the owner on Jul 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow 'd' as day unit for -range flag. Closes #7.
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package flags | ||
|
||
import ( | ||
"flag" | ||
"regexp" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type durationValue time.Duration | ||
|
||
var matchDay = regexp.MustCompile("[-+]?[0-9]*(\\.[0-9]*)?d") | ||
|
||
func (d *durationValue) Set(s string) error { | ||
if is := matchDay.FindStringIndex(s); is != nil { | ||
days, err := strconv.ParseFloat(s[is[0]:is[1]-1], 64) | ||
if err != nil { | ||
return err | ||
} | ||
s = s[:is[0]] + s[is[1]:] + strconv.FormatFloat(days*24, 'f', 6, 64) + "h" | ||
} | ||
v, err := time.ParseDuration(s) | ||
*d = durationValue(v) | ||
return err | ||
} | ||
|
||
func (d *durationValue) String() string { | ||
return (*time.Duration)(d).String() | ||
} | ||
|
||
// Duration defines a flag for time.Duration values. | ||
// It works the same way as flag.Duration except that it als parses "XXd" values as days. | ||
func Duration(name string, value time.Duration, usage string) *time.Duration { | ||
t := &value | ||
flag.Var((*durationValue)(t), name, usage) | ||
return t | ||
} |
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,49 @@ | ||
package flags | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDuration(t *testing.T) { | ||
tests := []struct { | ||
text string | ||
parsed time.Duration | ||
invalid bool | ||
}{ | ||
{text: "1h30.5m3s", parsed: time.Hour + 30*time.Minute + 33*time.Second}, | ||
{text: "1h", parsed: time.Hour}, | ||
{text: "1h2h", parsed: 3 * time.Hour}, | ||
{text: "30m1h", parsed: time.Hour + 30*time.Minute}, | ||
{text: "1d", parsed: 24 * time.Hour}, | ||
{text: "0.50d", parsed: 12 * time.Hour}, | ||
{text: ".5d", parsed: 12 * time.Hour}, | ||
{text: "1d1h", parsed: 25 * time.Hour}, | ||
{text: "1h1d", parsed: 25 * time.Hour}, | ||
{text: "1h1d60m", parsed: 26 * time.Hour}, | ||
{text: "", invalid: true}, | ||
{text: "bla", invalid: true}, | ||
} | ||
|
||
for i, tt := range tests { | ||
var u durationValue | ||
if err := u.Set(tt.text); err != nil { | ||
if !tt.invalid { | ||
t.Errorf("parsing '%s' failed unexpectedly: %v", tt.text, err) | ||
} | ||
continue | ||
} | ||
if tt.invalid { | ||
t.Errorf("parsing '%s' should have failed", tt.text) | ||
continue | ||
} | ||
if time.Duration(u) != tt.parsed { | ||
t.Errorf(` | ||
%d. | ||
Input: %s | ||
Expected: %v | ||
Got %v`, i, tt.text, tt.parsed, u) | ||
} | ||
} | ||
|
||
} |
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