diff pkg/imports/config.go @ 1627:b10aa02d7819

Refactored: Moved REST /api/imports/scheduler to /api/imports/config
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 19 Dec 2018 15:58:44 +0100
parents
children 06f08458d666
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/imports/config.go	Wed Dec 19 15:58:44 2018 +0100
@@ -0,0 +1,71 @@
+// This is Free Software under GNU Affero General Public License v >= 3.0
+// without warranty, see README.md and license for details.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// License-Filename: LICENSES/AGPL-3.0.txt
+//
+// Copyright (C) 2018 by via donau
+//   – Österreichische Wasserstraßen-Gesellschaft mbH
+// Software engineering by Intevation GmbH
+//
+// Author(s):
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+package imports
+
+import (
+	"encoding/json"
+	"fmt"
+
+	"github.com/robfig/cron"
+)
+
+type (
+	CronSpec   string
+	ImportKind string
+
+	Config struct {
+		Kind       ImportKind `json:"kind"`
+		SendEMail  bool       `json:"send-email"`
+		AutoAccept bool       `json:"auto-accept"`
+		Cron       *CronSpec  `json:"cron"`
+		URL        *string    `json:"url"`
+	}
+
+	IDConfig struct {
+		ID         int64      `json:"id"`
+		User       string     `json:"user"`
+		Kind       ImportKind `json:"kind"`
+		SendEMail  bool       `json:"send-email"`
+		AutoAccept bool       `json:"auto-accept"`
+		Cron       *CronSpec  `json:"cron,omitempty"`
+		URL        *string    `json:"url,omitempty"`
+	}
+)
+
+func (ik *ImportKind) UnmarshalJSON(data []byte) error {
+	var s string
+	if err := json.Unmarshal(data, &s); err != nil {
+		return err
+	}
+
+	if !HasImportKindName(s) {
+		return fmt.Errorf("Unknown kind '%s'", s)
+	}
+
+	*ik = ImportKind(s)
+
+	return nil
+}
+
+func (cs *CronSpec) UnmarshalJSON(data []byte) error {
+	var spec string
+	if err := json.Unmarshal(data, &spec); err != nil {
+		return err
+	}
+	if _, err := cron.Parse(spec); err != nil {
+		return err
+	}
+	*cs = CronSpec(spec)
+	return nil
+}