-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
102 lines (89 loc) · 2.1 KB
/
logger.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/writeas/go-writeas/v2"
)
var responses = []response{}
var uploadErrors = []uploadError{}
func LogResponse(p *writeas.Post) {
r := response{
ID: p.ID,
Slug: p.Slug,
Font: p.Font,
Language: p.Language,
RTL: p.RTL,
Created: p.Created,
Updated: p.Updated,
Title: p.Title,
Content: p.Content,
Tags: p.Tags,
Collection: p.Collection.Alias,
}
responses = append(responses, r)
}
func LogUploadError(i string, e string) {
ue := uploadError{
ImagePath: i,
UploadError: e,
}
uploadErrors = append(uploadErrors, ue)
}
func WriteResponsesToDisk() error {
if len(responses) == 0 {
return errors.New("Could not find publishing results")
}
responsesJson, _ := json.Marshal(responses)
fmt.Println("Writing publishing log to disk...")
t := time.Now()
ts := t.Format("20060102150405")
f := "publishing-log_" + ts + ".json"
err := ioutil.WriteFile(f, responsesJson, 0644)
if err != nil {
return err
}
fmt.Println("Publishing log written.")
return nil
}
func WriteUploadErrorsTo(p string) error {
if len(uploadErrors) > 0 {
fmt.Println("Writing image-upload error log to disk...")
f := "upload-error.log"
logfilePathName := filepath.Join(p, f)
file, err := os.Create(logfilePathName)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range uploadErrors {
fmt.Fprintln(w, line)
}
fmt.Println("Error log written.")
return w.Flush()
}
return nil
}
type response struct {
ID string `json:"id"`
Slug string `json:"slug"`
Font string `json:"appearance"`
Language *string `json:"language"`
RTL *bool `json:"rtl"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Title string `json:"title"`
Content string `json:"body"`
Tags []string `json:"tags"`
Collection string `json:"collection,omitempty"`
}
type uploadError struct {
ImagePath string
UploadError string
}