comparison 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
comparison
equal deleted inserted replaced
1626:92da44ba610c 1627:b10aa02d7819
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2018 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
13
14 package imports
15
16 import (
17 "encoding/json"
18 "fmt"
19
20 "github.com/robfig/cron"
21 )
22
23 type (
24 CronSpec string
25 ImportKind string
26
27 Config struct {
28 Kind ImportKind `json:"kind"`
29 SendEMail bool `json:"send-email"`
30 AutoAccept bool `json:"auto-accept"`
31 Cron *CronSpec `json:"cron"`
32 URL *string `json:"url"`
33 }
34
35 IDConfig struct {
36 ID int64 `json:"id"`
37 User string `json:"user"`
38 Kind ImportKind `json:"kind"`
39 SendEMail bool `json:"send-email"`
40 AutoAccept bool `json:"auto-accept"`
41 Cron *CronSpec `json:"cron,omitempty"`
42 URL *string `json:"url,omitempty"`
43 }
44 )
45
46 func (ik *ImportKind) UnmarshalJSON(data []byte) error {
47 var s string
48 if err := json.Unmarshal(data, &s); err != nil {
49 return err
50 }
51
52 if !HasImportKindName(s) {
53 return fmt.Errorf("Unknown kind '%s'", s)
54 }
55
56 *ik = ImportKind(s)
57
58 return nil
59 }
60
61 func (cs *CronSpec) UnmarshalJSON(data []byte) error {
62 var spec string
63 if err := json.Unmarshal(data, &spec); err != nil {
64 return err
65 }
66 if _, err := cron.Parse(spec); err != nil {
67 return err
68 }
69 *cs = CronSpec(spec)
70 return nil
71 }