-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile.go
More file actions
87 lines (70 loc) · 2.78 KB
/
file.go
File metadata and controls
87 lines (70 loc) · 2.78 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
"strings"
"time"
ptime "github.com/yaa110/go-persian-calendar"
)
var ErrContentLengthMismatch = errors.New("content length mismatch")
type FileContent struct {
UID string `json:"uid" toml:"uid"`
BillID string `json:"bill_id" toml:"bill_id"`
Sequence uint `json:"sequence" toml:"sequence"`
OutageNumber int `json:"outage_number" toml:"outage_number"`
FarsiOutageDate string `json:"farsi_outage_date" toml:"farsi_outage_date"`
StartOutageDateTime time.Time `json:"start_outage_datetime" toml:"outage_datetime"`
EndOutageDateTime time.Time `json:"end_outage_datetime" toml:"end_outage_datetime"`
Recipients []string `json:"recipients" toml:"recipients"`
Address string `json:"address" toml:"address"`
ReasonOutage string `json:"reason_outage" toml:"reason_outage"`
}
// Pattern: "{bill_id}_{outage-number}_{outage-date}.json"
func (f *FileContent) FileName() string {
return FileName(f.BillID, f.OutageNumber, f.StartOutageDateTime)
}
func FileName(billID string, outageNumber int, date time.Time) string {
return fmt.Sprintf("%s_%d_%s.json", billID, outageNumber, date.Format(time.DateOnly))
}
func (f *FileContent) Write(file *os.File) error {
content, err := json.Marshal(f)
if err != nil {
slog.Error("Encode data failed", "error", err)
return err
}
if _, err := file.WriteAt(content, 0); err != nil {
slog.Error("Failed to write content into file", "error", err)
return err
}
return nil
}
func LoadOrCreateFile(cachePathDir, billID string, outageNumber int, date time.Time) (*os.File, error) {
filePath := cachePathDir + FileName(billID, outageNumber, date)
slog.Debug("file path to open or create", "file path", filePath)
return os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0o644)
}
func (f *FileContent) Summary() string {
return fmt.Sprintf("Power Outage on %s", f.Address)
}
func (f *FileContent) Description() string {
return strings.ReplaceAll(fmt.Sprintf("Blackout!\nAddress: %s\nDate: %s\nFrom %s until %s\nReason: %s",
f.Address, ptime.New(f.StartOutageDateTime).Format("yyyy/MM/dd"), f.StartOutageDateTime.Format(time.TimeOnly), f.EndOutageDateTime.Format(time.TimeOnly), f.ReasonOutage), "\n", "\\n")
}
func CreateCachePath() (string, error) {
cachePath, err := os.UserCacheDir()
if err != nil {
slog.Error("unable to get user cache path directory", "error", err)
return "", err
}
cachePathDir := cachePath + "/" + appName + "/"
if err := os.MkdirAll(cachePathDir, 0o755); err != nil {
if !errors.Is(err, os.ErrExist) {
slog.Error("cannot create directory under the cache path", "error", err, "cache path directory", cachePathDir)
return "", err
}
}
return cachePathDir, nil
}