Skip to content

Commit

Permalink
Added a new function timestamp to get the current time back. Fixes ha…
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Costanzo committed Nov 18, 2014
1 parent c1a6a48 commit e6e6e46
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
28 changes: 24 additions & 4 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import (
"fmt"
"io/ioutil"
"strings"
"time"
)

// Funcs is the mapping of built-in functions for configuration.
var Funcs map[string]InterpolationFunc

func init() {
Funcs = map[string]InterpolationFunc{
"concat": interpolationFuncConcat,
"file": interpolationFuncFile,
"join": interpolationFuncJoin,
"lookup": interpolationFuncLookup,
"concat": interpolationFuncConcat,
"file": interpolationFuncFile,
"join": interpolationFuncJoin,
"lookup": interpolationFuncLookup,
"timestamp": interpolationFuncTimestamp,
}
}

Expand Down Expand Up @@ -87,3 +89,21 @@ func interpolationFuncLookup(

return v, nil
}

// interpolationFuncTimestamp impelements print out the current date/time
// using either the given layout or a default of 20060102150405
func interpolationFuncTimestamp(
vs map[string]string, args ...string) (string, error) {
if len(args) > 1 {
return "", fmt.Errorf("timestamp expects either 0 or 1 argument")
}

v := ""
if len(args) == 1 {
v = time.Now().Format(args[0])
} else {
v = time.Now().Format("20060102150405")
}

return v, nil
}
43 changes: 43 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,46 @@ func TestInterpolateFuncLookup(t *testing.T) {
}
}
}

func TestInterpolateFuncTimestamp(t *testing.T) {
cases := []struct {
Args []string
Result string
Error bool
}{
{
[]string{},
"20060102150405",
false,
},

{
[]string{"2006-01-02.15_04_05"},
"2006-01-02.15_04_05",
false,
},

{
[]string{"blah"},
"blah",
false,
},

{
[]string{"20060102150405", "20060102150405"},
"",
true,
},
}

for i, tc := range cases {
actual, err := interpolationFuncTimestamp(nil, tc.Args...)
if (err != nil) != tc.Error {
t.Fatalf("%d: err: %s", i, err)
}

if len(actual) != len(tc.Result) {
t.Fatalf("%d: bad: %#v", i, actual)
}
}
}
6 changes: 6 additions & 0 deletions website/source/docs/configuration/interpolation.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,9 @@ The supported built-in functions are:

* `lookup(map, key)` - Performs a dynamic lookup into a mapping
variable.

* `timestamp(layout)` - Returns the current time formatted with the given layout.
Layout is an optional arguent. The layout is expected to be
based on the Go reference time of Mon Jan 2 15:04:05 MST 2006.
If no layout is passed, the default layout 20060102150405 is used.
Example: `timestamp("2006_01_02-15:04:05")` returns 2014_11_18-12:32:41

0 comments on commit e6e6e46

Please sign in to comment.