From fa97d3d98a30443c9261ff95bf2e33fcde9cf919 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Sun, 7 May 2017 12:00:34 +0200 Subject: [PATCH] config: parse InitialReplPolicy with default to most_recent --- cmd/config.go | 61 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 05e92d8..e1563b1 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -16,6 +16,8 @@ import ( const LOCAL_TRANSPORT_IDENTITY string = "local" +const DEFAULT_INITIAL_REPL_POLICY = InitialReplPolicyMostRecent + type Pool struct { Name string Transport Transport @@ -37,13 +39,22 @@ type SSHTransport struct { Options []string } +type InitialReplPolicy string + +const ( + InitialReplPolicyMostRecent InitialReplPolicy = "most_recent" + InitialReplPolicyAll InitialReplPolicy = "all" +) + type Push struct { - To *Pool - Datasets []zfs.DatasetPath + To *Pool + Datasets []zfs.DatasetPath + InitialReplPolicy InitialReplPolicy } type Pull struct { - From *Pool - Mapping zfs.DatasetMapping + From *Pool + Mapping zfs.DatasetMapping + InitialReplPolicy InitialReplPolicy } type ClientMapping struct { From string @@ -165,8 +176,9 @@ type poolLookup func(name string) (*Pool, error) func parsePushs(v interface{}, pl poolLookup) (p []Push, err error) { asList := make([]struct { - To string - Datasets []string + To string + Datasets []string + InitialReplPolicy string }, 0) if err = mapstructure.Decode(v, &asList); err != nil { @@ -192,6 +204,10 @@ func parsePushs(v interface{}, pl poolLookup) (p []Push, err error) { } } + if push.InitialReplPolicy, err = parseInitialReplPolicy(e.InitialReplPolicy, DEFAULT_INITIAL_REPL_POLICY); err != nil { + return + } + p[i] = push } @@ -201,8 +217,9 @@ func parsePushs(v interface{}, pl poolLookup) (p []Push, err error) { func parsePulls(v interface{}, pl poolLookup) (p []Pull, err error) { asList := make([]struct { - From string - Mapping map[string]string + From string + Mapping map[string]string + InitialReplPolicy string }, 0) if err = mapstructure.Decode(v, &asList); err != nil { @@ -232,12 +249,40 @@ func parsePulls(v interface{}, pl poolLookup) (p []Pull, err error) { if pull.Mapping, err = parseComboMapping(e.Mapping); err != nil { return } + if pull.InitialReplPolicy, err = parseInitialReplPolicy(e.InitialReplPolicy, DEFAULT_INITIAL_REPL_POLICY); err != nil { + return + } + p[i] = pull } return } +func parseInitialReplPolicy(v interface{}, defaultPolicy InitialReplPolicy) (p InitialReplPolicy, err error) { + s, ok := v.(string) + if !ok { + goto err + } + + switch { + case s == "": + p = defaultPolicy + case s == "most_recent": + p = InitialReplPolicyMostRecent + case s == "all": + p = InitialReplPolicyAll + default: + goto err + } + + return + +err: + err = errors.New(fmt.Sprintf("expected InitialReplPolicy, got %#v", v)) + return +} + func expectList(v interface{}) (asList []interface{}, err error) { var ok bool if asList, ok = v.([]interface{}); !ok {