1package model 2 3import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "hash/adler32" 8 "time" 9) 10 11const ( 12 AwlessFileExt = "aws" 13 StampLayout = "2006-01-02-15h04m05s" 14) 15 16type ServiceInfo struct { 17 Uptime string 18 ServiceAddr string 19 TickerFrequency string 20 UnixSockMode bool 21} 22 23type Task struct { 24 Content string 25 RunAt time.Time 26 RevertAt time.Time 27 Region string 28} 29 30func (tk *Task) AsFilename() string { 31 checksum := adler32.Checksum([]byte(tk.Content)) 32 return fmt.Sprintf("%d_%s_%s_%s.%s", checksum, tk.RunAt.UTC().Format(StampLayout), tk.RevertAt.UTC().Format(StampLayout), tk.Region, AwlessFileExt) 33} 34 35func (tk *Task) MarshalJSON() ([]byte, error) { 36 buffer := bytes.NewBufferString("{") 37 jsonValue, err := json.Marshal(tk.Content) 38 if err != nil { 39 return nil, err 40 } 41 buffer.WriteString(fmt.Sprintf("\"Content\":%s,", jsonValue)) 42 if !tk.RunAt.IsZero() { 43 jsonValue, err = json.Marshal(tk.RunAt) 44 if err != nil { 45 return nil, err 46 } 47 buffer.WriteString(fmt.Sprintf("\"RunAt\":%s,", jsonValue)) 48 buffer.WriteString(fmt.Sprintf("\"RunIn\":\"%s\",", time.Until(tk.RunAt))) 49 } 50 if !tk.RevertAt.IsZero() { 51 jsonValue, err = json.Marshal(tk.RevertAt) 52 if err != nil { 53 return nil, err 54 } 55 buffer.WriteString(fmt.Sprintf("\"RevertAt\":%s,", jsonValue)) 56 buffer.WriteString(fmt.Sprintf("\"RevertIn\":\"%s\",", time.Until(tk.RevertAt))) 57 } 58 buffer.WriteString(fmt.Sprintf("\"Region\":\"%s\"", tk.Region)) 59 60 buffer.WriteString("}") 61 return buffer.Bytes(), nil 62} 63