-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathoption.go
69 lines (59 loc) · 2.11 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cronv
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
)
const (
optDateFormat = "2006/01/02"
optTimeFormat = "15:04"
optDefaultDuration = "6h"
optDefaultOutputPath = "./crontab.html"
optDefaultTitle = "Cron Tasks"
optDefaultWidth = 100
)
type Option struct {
OutputFilePath string `short:"o" long:"output" description:"path to .html file to output"`
Duration string `short:"d" long:"duration" description:"duration to visualize in N{suffix} style. e.g.) 1d(day)/1h(hour)/1m(minute)"`
FromDate string `long:"from-date" description:"start date in the format '2006/01/02' to visualize"`
FromTime string `long:"from-time" description:"start time in the format '15:04' to visualize"`
Title string `short:"t" long:"title" description:"title/label of output"`
Width int `short:"w" long:"width" description:"Table width of output"`
}
func (o *Option) toFromTime() (time.Time, error) {
return time.Parse(fmt.Sprintf("%s %s", optDateFormat, optTimeFormat),
fmt.Sprintf("%s %s", o.FromDate, o.FromTime))
}
func (o *Option) toDurationMinutes() (float64, error) {
length := len(o.Duration)
if length < 2 {
return 0, errors.Errorf("invalid duration format: '%s'", o.Duration)
}
duration, err := strconv.Atoi(string(o.Duration[:length-1]))
if err != nil {
return 0, errors.Errorf("invalid duration format: '%s', %s", o.Duration, err)
}
unit := string(o.Duration[length-1])
switch strings.ToLower(unit) {
case "d":
return float64(duration * 24 * 60), nil
case "h":
return float64(duration * 60), nil
case "m":
return float64(duration), nil
}
return 0, errors.Errorf("invalid duration format: '%s', '%s' is not in d/h/m", o.Duration, unit)
}
func NewCronvOption(t time.Time) *Option {
now := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, time.UTC)
return &Option{
OutputFilePath: optDefaultOutputPath,
Duration: optDefaultDuration,
FromDate: now.Format(optDateFormat),
FromTime: now.Format(optTimeFormat),
Title: optDefaultTitle,
Width: optDefaultWidth,
}
}