-
Notifications
You must be signed in to change notification settings - Fork 2
/
main-json-builder-test.go_
253 lines (224 loc) 路 7.5 KB
/
main-json-builder-test.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
// https://www.algolia.com/doc/api-client/methods/indexing/
import (
// "bytes"
"encoding/json"
"flag"
"fmt"
"github.com/gernest/front"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"io/ioutil"
"os"
"path"
"time"
// "stdlog"
"strings"
// "github.com/algolia/algoliasearch-client-go/algolia/search"
// . "github.com/algolia/algoliasearch-client-go/v3@v3.Y.Z"
)
//var zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
const contentDir string = "content/blog"
const jsonOut string = "algolia.json"
// type AlgoliaIndex []struct {
// Authors interface{} `json:"authors"`
// Categories interface{} `json:"categories"`
// Date int64 `json:"date"`
// Description string `json:"description"`
// Dir string `json:"dir"`
// Expirydate int64 `json:"expirydate"`
// Fuzzywordcount int64 `json:"fuzzywordcount"`
// Keywords interface{} `json:"keywords"`
// Kind string `json:"kind"`
// Lang string `json:"lang"`
// Lastmod int64 `json:"lastmod"`
// ObjectID string `json:"objectID"`
// Permalink string `json:"permalink"`
// Publishdate string `json:"publishdate"`
// Readingtime int64 `json:"readingtime"`
// Relpermalink string `json:"relpermalink"`
// Section string `json:"section"`
// Summary string `json:"summary"`
// Tags []string `json:"tags"`
// Title string `json:"title"`
// Type string `json:"type"`
// URL string `json:"url"`
// Weight int64 `json:"weight"`
// Wordcount int64 `json:"wordcount"`
// }
type AlgoliaIndex struct {
Authors []string `json:"authors"`
Categories []string `json:"categories"`
Date string `json:"date"`
Description string `json:"description"`
Dir string `json:"dir"`
Expirydate string `json:"expirydate"`
Fuzzywordcount int64 `json:"fuzzywordcount"`
Keywords []string `json:"keywords"`
Kind string `json:"kind"`
Lang string `json:"lang"`
Lastmod string `json:"lastmod"`
ObjectID string `json:"objectID"`
Permalink string `json:"permalink"`
Publishdate string `json:"publishdate"`
Readingtime int64 `json:"readingtime"`
Relpermalink string `json:"relpermalink"`
Section string `json:"section"`
Summary string `json:"summary"`
Tags []string `json:"tags"`
Title string `json:"title"`
Type string `json:"type"`
URL string `json:"url"`
Weight int64 `json:"weight"`
Wordcount int64 `json:"wordcount"`
}
func GetFrontMatter(txt string) (f map[string]interface{}) {
m := front.NewMatter()
m.Handle("---", front.YAMLHandler)
f, body, err := m.Parse(strings.NewReader(txt))
if err != nil {
panic(err)
}
log.Debug().Msgf("The front matter is:\n%#v\n", f)
for i, fm := range f {
log.Debug().Msgf("i: %v v: %v", i, fm)
}
log.Debug().Msgf("The body is:\n%q\n", body)
return f
}
func InitLogger() {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
output.FormatLevel = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
}
output.FormatMessage = func(i interface{}) string {
return fmt.Sprintf("***%s****", i)
}
output.FormatFieldName = func(i interface{}) string {
return fmt.Sprintf("%s:", i)
}
output.FormatFieldValue = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("%s", i))
}
}
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
debug := flag.Bool("debug", false, "sets log level to debug")
flag.Parse()
// Default level for this example is info, unless debug flag is present
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
// log := zerolog.New(os.Stdout).With().
// Str("foo", "bar").
// Logger()
// stdlog.SetFlags(0)
// stdlog.SetOutput(log)
// stdlog.Print("hello world")
// InitLogger()
//log := zerolog.New(output).With().Timestamp().Logger()
files, err := ioutil.ReadDir(contentDir)
if err != nil {
log.Error().Msgf("Failed to read files: %v", err)
}
log.Info().Msgf("Total Files Returned: %v", len(files))
///var AlgItems = new([]AlgoliaIndex)
var AlgItems = []AlgoliaIndex{}
// Create sized to file count
//AlgItems := make([]AlgoliaIndex, len(files))
for i, f := range files {
//basename := f.Name()
log.Info().Msgf("i: %v f: %v", i, f.Name())
filename := path.Join(contentDir, f.Name())
b, err := ioutil.ReadFile(filename)
if err != nil {
log.Error().Msgf("Error reading file: %v", err)
}
fm := GetFrontMatter(string(b))
item := AlgoliaIndex{}
if v, ok := fm["title"].(string); ok {
item.Title = v
}
if v, ok := fm["excerpt"].(string); ok {
item.Summary = v
}
if v, ok := fm["date"].(string); ok {
item.Date = v
}
if v, ok := fm["tags"].([]string); ok {
item.Tags = v
}
log.Debug().Msgf("Front Matter: %v", fm)
log.Debug().Msg("Returned front matter for: " + filename)
AlgItems = append(AlgItems, item)
log.Debug().Msgf("AltItems Count: %v", len(AlgItems))
}
log.Info().Msgf("Total Items in Struct AlgItems: %v", len(AlgItems))
//var prettyJSON bytes.Buffer
b, err := json.MarshalIndent(AlgItems, "", "\t")
if err != nil {
log.Error().Msgf("JSON parse error: %v", err)
return
}
err = ioutil.WriteFile(jsonOut, b, 0644)
if err != nil {
log.Err(err)
}
// file, err := ioutil.ReadFile("public/algolia.json")
// if err != nil {
// fmt.Print(err)
// }
// data := AlgoliaIndex{}
// err = json.Unmarshal([]byte(file), &data)
// if err != nil {
// fmt.Print(err)
// }
// fmt.Print(data)
}
// func UpdateAlgolia() {
// type AlgoliaIndex []struct {
// Authors interface{} `json:"authors"`
// Categories interface{} `json:"categories"`
// Date int64 `json:"date"`
// Description string `json:"description"`
// Dir string `json:"dir"`
// Expirydate int64 `json:"expirydate"`
// Fuzzywordcount int64 `json:"fuzzywordcount"`
// Keywords interface{} `json:"keywords"`
// Kind string `json:"kind"`
// Lang string `json:"lang"`
// Lastmod int64 `json:"lastmod"`
// ObjectID string `json:"objectID"`
// Permalink string `json:"permalink"`
// Publishdate string `json:"publishdate"`
// Readingtime int64 `json:"readingtime"`
// Relpermalink string `json:"relpermalink"`
// Section string `json:"section"`
// Summary string `json:"summary"`
// Tags []string `json:"tags"`
// Title string `json:"title"`
// Type string `json:"type"`
// URL string `json:"url"`
// Weight int64 `json:"weight"`
// Wordcount int64 `json:"wordcount"`
// }
// fmt.Print("Doing this now")
// file, err := ioutil.ReadFile("public/algolia.json")
// if err != nil {
// fmt.Print(err)
// }
// data := AlgoliaIndex{}
// err = json.Unmarshal([]byte(file), &data)
// if err != nil {
// fmt.Print(err)
// }
// fmt.Print(data)
// client := search.NewClient(os.GetEnv("ALGOLIA_APP_ID"), os.GetEnv("ALGOLIA_ADMIN_KEY"))
// index := client.InitIndex(os.GetEnv("ALGOLIA_INDEX_NAME"))
// fmt.Print(index)
// res, err := index.SaveObjects(contacts)
// if err != nil {
// fmt.Print(err)
// }
// }