Browse Source

import

tags/version-1.0
Vladimir Smagin 2 months ago
commit
790ce8c69d
4 changed files with 72 additions and 0 deletions
  1. 6
    0
      configs/test1.yml
  2. 39
    0
      file_utils.go
  3. BIN
      gogocron
  4. 27
    0
      gogocron.go

+ 6
- 0
configs/test1.yml View File

@@ -0,0 +1,6 @@
1
+---
2
+name: "Print base64 of 20 random symbols"
3
+runSecond: "*/5"
4
+commands: 
5
+  - "head -c 20 /dev/urandom |base64"
6
+  - ls

+ 39
- 0
file_utils.go View File

@@ -0,0 +1,39 @@
1
+package main
2
+
3
+import (
4
+	"io/ioutil"
5
+	"log"
6
+
7
+	"gopkg.in/yaml.v2"
8
+)
9
+
10
+func readConfigDirectory(path string) []string {
11
+	var res []string
12
+	files, err := ioutil.ReadDir(path)
13
+	if err != nil {
14
+		log.Fatal(err)
15
+	}
16
+
17
+	for _, file := range files {
18
+		log.Println(file.Name())
19
+		res = append(res, path+"/"+file.Name())
20
+	}
21
+
22
+	return res
23
+}
24
+
25
+// readConfigFile reads config file and returns cronTask struct back to loadConfig()
26
+func readConfigFile(path string) cronTask {
27
+	var config cronTask
28
+	yamldata, err := ioutil.ReadFile(path)
29
+	if err != nil {
30
+		log.Fatalf("Error opening file %v", err)
31
+	}
32
+
33
+	marshErr := yaml.Unmarshal(yamldata, &config)
34
+	if marshErr != nil {
35
+		log.Fatalf("Error parsing file %v", marshErr)
36
+	}
37
+
38
+	return config
39
+}

BIN
gogocron View File


+ 27
- 0
gogocron.go View File

@@ -0,0 +1,27 @@
1
+package main
2
+
3
+import (
4
+	"log"
5
+)
6
+
7
+type cronTask struct {
8
+	name      string   `yaml:"name"`
9
+	runSecond string   `yaml:"runSecond,omitempty"`
10
+	runMinute string   `yaml:"runMinute,omitempty"`
11
+	runHour   string   `yaml:"runHour,omitempty"`
12
+	runDom    string   `yaml:"runDom,omitempty"`
13
+	runMonth  string   `yaml:"runMonth,omitempty"`
14
+	runDow    string   `yaml:"runDow,omitempty"`
15
+	command   []string `yaml:"command"`
16
+}
17
+
18
+type cronTaskYaml struct {
19
+	Yaml cronTask
20
+}
21
+
22
+func main() {
23
+	configs := readConfigDirectory("configs")
24
+	log.Printf("Files found: %v", configs)
25
+	config := readConfigFile(configs[0])
26
+	log.Printf("Parsed %#v", config)
27
+}

Loading…
Cancel
Save