Commit 685a0c83 authored by Joan Piles's avatar Joan Piles

Added support for almost human-readable JSON format for external schedules

parent ffa8e204
......@@ -18,6 +18,7 @@ import (
const (
myName = "snaprd"
defaultSchedFileName = "/etc/"+myName+".schedules"
DATA_SUBDIR = ".data"
)
......@@ -149,8 +150,8 @@ func LoadConfig() *Config {
"noLogDate", false,
"if set, does not print date and time in the log output. Useful if output is redirected to syslog")
flags.StringVar(&(config.SchedFile),
"schedFile", "",
"path to rsync binary")
"schedFile", defaultSchedFileName,
"path to external schedules")
flags.Parse(os.Args[2:])
if config.SchedFile != "" {
schedules.AddFromFile(config.SchedFile)
......@@ -186,8 +187,8 @@ func LoadConfig() *Config {
"schedule", "longterm",
"one of "+schedules.String())
flags.StringVar(&(config.SchedFile),
"schedFile", "",
"path to rsync binary")
"schedFile", defaultSchedFileName,
"path to external schedules")
flags.Parse(os.Args[2:])
if config.SchedFile != "" {
schedules.AddFromFile(config.SchedFile)
......@@ -208,8 +209,8 @@ func LoadConfig() *Config {
{
flags := flag.NewFlagSet(subcmd, flag.ExitOnError)
flags.StringVar(&(config.SchedFile),
"schedFile", "",
"path to rsync binary")
"schedFile", defaultSchedFileName,
"path to external schedules")
flags.Parse(os.Args[2:])
if config.SchedFile != "" {
schedules.AddFromFile(config.SchedFile)
......
......@@ -10,6 +10,7 @@ import (
"encoding/json"
"io/ioutil"
"fmt"
"os"
)
const (
......@@ -44,6 +45,8 @@ func (il intervalList) goal(i int) int {
type scheduleList map[string]intervalList
type jsonInterval []map[string]time.Duration
func (schl *scheduleList) String() string {
a := []string{}
for sch := range *schl {
......@@ -62,26 +65,77 @@ var schedules = scheduleList{
"testing2": {second * 5, second * 20, second * 40, second * 80, long},
}
// Adds an external JSON file to the list of available scheds
func (schl scheduleList) AddFromFile(file string) {
// If we are using the default file name, and it doesn't exist, no problem, just return
if _, err := os.Stat(file); os.IsNotExist(err) && file == defaultSchedFileName {
return
}
schedFile, err := ioutil.ReadFile(file)
if err != nil {
fmt.Printf("Error opening schedule file: %v\n", err)
return
}
var readData scheduleList
var readData map[string]jsonInterval
err = json.Unmarshal(schedFile,&readData)
err = json.Unmarshal(schedFile,&readData)
if err != nil {
fmt.Printf("Error parsing data: %v\n", err)
return
}
for k,v := range readData {
schl[k] = v
schl[k] = v.IntervalList()
}
}
// Prints the stored schedules in the list
func (schl scheduleList) List() {
for name,sched := range schl {
fmt.Println(name,": ", sched);
}
}
// Transform a JSON formatted intervalList like this:
// [
// { "day" : 1, "hour" : 12 },
// { "week" : 2 },
// { "month" : 1, "week" : 2}
// { "long" : 1}
// ]
// and it makes it equivalent to
// { 1*day + 12*hour, 2*week, 1*month + 2*week, long }
func (json jsonInterval) IntervalList() intervalList {
il := make(intervalList,len(json))
for i,interval := range json {
var duration time.Duration = 0
Loop:
for k,v := range interval {
switch k {
case "s","second":
duration += v * second
case "m","minute":
duration += v * minute
case "h","hour":
duration += v * hour
case "d","day":
duration += v * day
case "w","week":
duration += v * week
case "M","month":
duration += v * month
case "y","year":
duration += v * year
case "l","long":
duration = long
break Loop
}
}
il[i] = duration
}
return il
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment