Add support for "Expect" check, add Duration support

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-07-25 13:42:56 +02:00
parent 00e4b2f859
commit f59d4dc5b8
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

View file

@ -1,8 +1,11 @@
package main
import (
"errors"
"fmt"
"strings"
"time"
"github.com/pkg/errors"
)
var (
@ -12,6 +15,33 @@ var (
type moduleAttributeStore map[string]interface{}
func (m moduleAttributeStore) Expect(keys ...string) error {
var missing []string
for _, k := range keys {
if _, ok := m[k]; !ok {
missing = append(missing, k)
}
}
if len(missing) > 0 {
return errors.Errorf("missing key(s) %s", strings.Join(missing, ", "))
}
return nil
}
func (m moduleAttributeStore) MustDuration(name string, defVal *time.Duration) time.Duration {
v, err := m.Duration(name)
if err != nil {
if defVal != nil {
return *defVal
}
panic(err)
}
return v
}
func (m moduleAttributeStore) MustInt64(name string, defVal *int64) int64 {
v, err := m.Int64(name)
if err != nil {
@ -34,6 +64,16 @@ func (m moduleAttributeStore) MustString(name string, defVal *string) string {
return v
}
func (m moduleAttributeStore) Duration(name string) (time.Duration, error) {
v, err := m.String(name)
if err != nil {
return 0, errors.Wrap(err, "getting string value")
}
d, err := time.ParseDuration(v)
return d, errors.Wrap(err, "parsing value")
}
func (m moduleAttributeStore) Int64(name string) (int64, error) {
v, ok := m[name]
if !ok {