-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
430 lines (349 loc) · 10.2 KB
/
main.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
const (
version = "1.0.0"
author = "Morten Johansen (mojoaar)"
)
type Template struct {
Content string `yaml:"content"`
Tags []string `yaml:"tags,omitempty"`
}
const defaultTemplateContent = `content: |
# {{TITLE}}
Date: {{DATE}}
Time: {{TIME}}
Tags: {{TAGS}}
## Introduction
## Main Content
## Conclusion
tags:
`
// Custom error types
type ValidationError struct {
Field string
Msg string
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("Validation error for %s: %s", e.Field, e.Msg)
}
type FileError struct {
Op string
Path string
Err error
}
func (e *FileError) Error() string {
return fmt.Sprintf("%s error for file %s: %v", e.Op, e.Path, e.Err)
}
var rootCmd = &cobra.Command{
Use: "md",
Short: "Markdown File Creator",
Version: version,
Long: fmt.Sprintf(`Markdown File Creator v%s
Author: %s
A tool for creating markdown files and managing templates.
Usage Examples:
Create a new template:
md template create my-template
List all templates:
md template list
Create a new note using the default template:
md note -t "My Note Title"
Create a new note with a custom name and template:
md note -t "My Note Title" -n my-custom-note -m my-template
Create a new note with tags:
md note -t "My Note Title" -g tag1,tag2,tag3
List all notes with their tags:
md list`, version, author),
}
var templateCmd = &cobra.Command{
Use: "template",
Short: "Manage templates",
Long: `Create or list template files for markdown notes.
Usage Examples:
Create a new template:
md template create my-template
List all templates:
md template list`,
}
var createTemplateCmd = &cobra.Command{
Use: "create [name]",
Short: "Create a new template",
Long: `Create a new template file for markdown notes.
Usage Example:
md template create my-template`,
Args: cobra.ExactArgs(1),
RunE: createTemplate,
}
var listTemplatesCmd = &cobra.Command{
Use: "list",
Short: "List all available templates",
Long: `List all available template files for markdown notes.
Usage Example:
md template list`,
RunE: listTemplates,
}
var noteCmd = &cobra.Command{
Use: "note",
Short: "Create a new note",
Long: `Create a new markdown note using a specified template.
Usage Examples:
Create a note with the default template:
md note -t "My Note Title"
Create a note with a custom name and template:
md note -t "My Note Title" -n my-custom-note -m my-template
Create a note with tags:
md note -t "My Note Title" -g tag1,tag2,tag3`,
RunE: createNote,
}
var listNotesCmd = &cobra.Command{
Use: "list",
Short: "List all notes with their tags",
Long: `List all markdown notes in the notes directory with their associated tags.`,
RunE: listNotes,
}
var (
noteTitle string
noteName string
templateName string
noteTags []string
)
func init() {
rootCmd.AddCommand(templateCmd, noteCmd, listNotesCmd)
templateCmd.AddCommand(createTemplateCmd, listTemplatesCmd)
noteCmd.Flags().StringVarP(¬eTitle, "title", "t", "", "Title of the markdown document (required)")
noteCmd.Flags().StringVarP(¬eName, "name", "n", "", "Name of the markdown file (optional, defaults to title)")
noteCmd.Flags().StringVarP(&templateName, "template", "m", "default", "Name of the template to use")
noteCmd.Flags().StringSliceVarP(¬eTags, "tags", "g", []string{}, "Tags for the note (comma-separated)")
noteCmd.MarkFlagRequired("title")
}
func main() {
if err := LoadConfig(); err != nil {
fmt.Printf("Error loading configuration: %v\n", err)
os.Exit(1)
}
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func ensureTemplatesDirectory() error {
if _, err := os.Stat(AppConfig.TemplatesDir); os.IsNotExist(err) {
err := os.Mkdir(AppConfig.TemplatesDir, 0755)
if err != nil {
return &FileError{Op: "create", Path: AppConfig.TemplatesDir, Err: err}
}
fmt.Printf("Created templates directory: %s\n", AppConfig.TemplatesDir)
// Create default template
defaultTemplatePath := filepath.Join(AppConfig.TemplatesDir, "default.yaml")
err = os.WriteFile(defaultTemplatePath, []byte(defaultTemplateContent), 0644)
if err != nil {
return &FileError{Op: "write", Path: defaultTemplatePath, Err: err}
}
fmt.Println("Created default template file: default.yaml")
}
return nil
}
func ensureNotesDirectory() error {
if AppConfig.NotesDir == "" {
return nil // No notes directory specified, we'll use the current directory
}
if _, err := os.Stat(AppConfig.NotesDir); os.IsNotExist(err) {
err := os.MkdirAll(AppConfig.NotesDir, 0755)
if err != nil {
return &FileError{Op: "create", Path: AppConfig.NotesDir, Err: err}
}
fmt.Printf("Created notes directory: %s\n", AppConfig.NotesDir)
}
return nil
}
func createTemplate(cmd *cobra.Command, args []string) error {
name := args[0]
if err := validateFileName(name); err != nil {
return err
}
if err := ensureTemplatesDirectory(); err != nil {
return err
}
filename := filepath.Join(AppConfig.TemplatesDir, sanitizeFileName(name)+".yaml")
err := os.WriteFile(filename, []byte(defaultTemplateContent), 0644)
if err != nil {
return &FileError{Op: "write", Path: filename, Err: err}
}
fmt.Printf("Template file '%s.yaml' created successfully.\n", name)
return nil
}
func listTemplates(cmd *cobra.Command, args []string) error {
if err := ensureTemplatesDirectory(); err != nil {
return err
}
files, err := os.ReadDir(AppConfig.TemplatesDir)
if err != nil {
return &FileError{Op: "read", Path: AppConfig.TemplatesDir, Err: err}
}
templateFiles := []string{}
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".yaml") {
templateFiles = append(templateFiles, strings.TrimSuffix(file.Name(), ".yaml"))
}
}
if len(templateFiles) == 0 {
fmt.Println("No template files found.")
} else {
fmt.Println("Available template files:")
for _, name := range templateFiles {
fmt.Println("-", name)
}
}
return nil
}
func createNote(cmd *cobra.Command, args []string) error {
if noteName == "" {
noteName = noteTitle
}
if err := validateFileName(noteName); err != nil {
return err
}
if err := validateTitle(noteTitle); err != nil {
return err
}
if err := ensureNotesDirectory(); err != nil {
return err
}
template, err := loadTemplate(templateName)
if err != nil {
return err
}
content := generateContent(template, noteTitle, noteTags)
err = saveMarkdownFile(noteName, content)
if err != nil {
return err
}
if len(noteTags) > 0 {
fmt.Printf("Markdown note '%s.md' created successfully with tags: %s\n", noteName, strings.Join(noteTags, ", "))
} else {
fmt.Printf("Markdown note '%s.md' created successfully\n", noteName)
}
return nil
}
func loadTemplate(templateName string) (Template, error) {
if err := ensureTemplatesDirectory(); err != nil {
return Template{}, err
}
filename := filepath.Join(AppConfig.TemplatesDir, sanitizeFileName(templateName)+".yaml")
data, err := os.ReadFile(filename)
if err != nil {
return Template{}, &FileError{Op: "read", Path: filename, Err: err}
}
var template Template
err = yaml.Unmarshal(data, &template)
if err != nil {
return Template{}, &FileError{Op: "parse", Path: filename, Err: err}
}
return template, nil
}
func generateContent(template Template, title string, tags []string) string {
now := time.Now()
content := strings.ReplaceAll(template.Content, "{{TITLE}}", title)
content = strings.ReplaceAll(content, "{{DATE}}", now.Format("2006-01-02"))
content = strings.ReplaceAll(content, "{{TIME}}", now.Format("15:04:05"))
tagsString := strings.Join(tags, ", ")
content = strings.ReplaceAll(content, "{{TAGS}}", tagsString)
return content
}
func saveMarkdownFile(name, content string) error {
filename := sanitizeFileName(name) + ".md"
if AppConfig.NotesDir != "" {
filename = filepath.Join(AppConfig.NotesDir, filename)
}
err := os.WriteFile(filename, []byte(content), 0644)
if err != nil {
return &FileError{Op: "write", Path: filename, Err: err}
}
return nil
}
func sanitizeFileName(name string) string {
// Replace spaces with underscores
name = strings.ReplaceAll(name, " ", "_")
// Remove any character that is not alphanumeric, underscore, or hyphen
reg := regexp.MustCompile(`[^a-zA-Z0-9_-]`)
name = reg.ReplaceAllString(name, "")
// Convert to lowercase
name = strings.ToLower(name)
// Trim to a reasonable length (e.g., 255 characters)
if len(name) > 255 {
name = name[:255]
}
return name
}
func validateFileName(name string) error {
if name == "" {
return &ValidationError{Field: "file name", Msg: "cannot be empty"}
}
if len(name) > 255 {
return &ValidationError{Field: "file name", Msg: "too long (max 255 characters)"}
}
if strings.ContainsAny(name, "/\\:*?\"<>|") {
return &ValidationError{Field: "file name", Msg: "contains invalid characters"}
}
return nil
}
func validateTitle(title string) error {
if title == "" {
return &ValidationError{Field: "title", Msg: "cannot be empty"}
}
if len(title) > 100 {
return &ValidationError{Field: "title", Msg: "too long (max 100 characters)"}
}
if strings.TrimSpace(title) == "" {
return &ValidationError{Field: "title", Msg: "cannot be only whitespace"}
}
return nil
}
func listNotes(cmd *cobra.Command, args []string) error {
if err := ensureNotesDirectory(); err != nil {
return err
}
notesDir := AppConfig.NotesDir
if notesDir == "" {
notesDir = "."
}
files, err := os.ReadDir(notesDir)
if err != nil {
return &FileError{Op: "read", Path: notesDir, Err: err}
}
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".md") {
content, err := os.ReadFile(filepath.Join(notesDir, file.Name()))
if err != nil {
fmt.Printf("Error reading %s: %v\n", file.Name(), err)
continue
}
tags := extractTags(string(content))
fmt.Printf("%s: %v\n", file.Name(), tags)
}
}
return nil
}
func extractTags(content string) []string {
lines := strings.Split(content, "\n")
for _, line := range lines {
if strings.HasPrefix(line, "Tags:") {
tagsPart := strings.TrimPrefix(line, "Tags:")
tags := strings.Split(tagsPart, ",")
for i, tag := range tags {
tags[i] = strings.TrimSpace(tag)
}
return tags
}
}
return []string{}
}