Browse Source

import

tags/version-1.0
Vladimir Smagin 1 year 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 @@
---
name: "Print base64 of 20 random symbols"
runSecond: "*/5"
commands:
- "head -c 20 /dev/urandom |base64"
- ls

+ 39
- 0
file_utils.go View File

@@ -0,0 +1,39 @@
package main

import (
"io/ioutil"
"log"

"gopkg.in/yaml.v2"
)

func readConfigDirectory(path string) []string {
var res []string
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}

for _, file := range files {
log.Println(file.Name())
res = append(res, path+"/"+file.Name())
}

return res
}

// readConfigFile reads config file and returns cronTask struct back to loadConfig()
func readConfigFile(path string) cronTask {
var config cronTask
yamldata, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Error opening file %v", err)
}

marshErr := yaml.Unmarshal(yamldata, &config)
if marshErr != nil {
log.Fatalf("Error parsing file %v", marshErr)
}

return config
}

BIN
gogocron View File


+ 27
- 0
gogocron.go View File

@@ -0,0 +1,27 @@
package main

import (
"log"
)

type cronTask struct {
name string `yaml:"name"`
runSecond string `yaml:"runSecond,omitempty"`
runMinute string `yaml:"runMinute,omitempty"`
runHour string `yaml:"runHour,omitempty"`
runDom string `yaml:"runDom,omitempty"`
runMonth string `yaml:"runMonth,omitempty"`
runDow string `yaml:"runDow,omitempty"`
command []string `yaml:"command"`
}

type cronTaskYaml struct {
Yaml cronTask
}

func main() {
configs := readConfigDirectory("configs")
log.Printf("Files found: %v", configs)
config := readConfigFile(configs[0])
log.Printf("Parsed %#v", config)
}

Loading…
Cancel
Save