-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentry.go
More file actions
159 lines (140 loc) · 3.87 KB
/
entry.go
File metadata and controls
159 lines (140 loc) · 3.87 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
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
package main
import (
"flag"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"
)
func main() {
fmt.Println("Hello awesome person.")
var template string
var suffix string
const (
templateDefault = ""
templateUsage = "the notes template to create"
suffixDefault = ""
suffixUsage = "text to add to end of created file (or folder)"
)
flag.StringVar(&template, "template", templateDefault, templateUsage)
flag.StringVar(&template, "t", templateDefault, templateUsage+" (shorthand)")
flag.StringVar(&suffix, "suffix", suffixDefault, suffixUsage)
flag.StringVar(&suffix, "s", suffixDefault, suffixUsage+" (shorthand)")
flag.Parse()
if template == "" {
fmt.Println("ERR: --template is required (shorthand: -t)")
os.Exit(1)
}
createEntryParams := CreateEntryParams{template, suffix}
create_entry(createEntryParams)
}
type CreateEntryParams struct {
template, suffix string
}
func create_entry(p CreateEntryParams) {
// output dir
entriesDir := "notemplates"
if p.template != "" {
entriesDir = p.template
}
// try to load content
content := ""
var loadTemplateErr error
if p.template != "" {
content, loadTemplateErr = loadTemplate(p.template)
if loadTemplateErr != nil {
fmt.Printf("Error loading template: %v\n", loadTemplateErr)
os.Exit(1)
}
} else {
content = "# Notemplate entry\n"
}
docsRoot := "documents"
err1 := os.MkdirAll(docsRoot, 0755)
if err1 != nil {
fmt.Printf("Error creating directory: %v\n", err1)
return
}
err2 := os.MkdirAll(path.Join(docsRoot, entriesDir), 0755)
if err2 != nil {
fmt.Printf("Error creating directory: %v\n", err1)
return
}
// create file name
currentDate := time.Now().Format("2006-01-02")
n := 0
var foldername string
println(p.suffix)
if p.suffix != "" && p.suffix != "_" {
p.suffix = "-" + p.suffix
}
// increment n until a new file name is available
for {
foldername = fmt.Sprintf("%s-entry-%d%s", currentDate, n, p.suffix)
foldernameNosuffix := fmt.Sprintf("%s-entry-%d", currentDate, n)
fullPath := filepath.Join(docsRoot, entriesDir, foldername)
fullPathNosuffix := filepath.Join(docsRoot, entriesDir, foldernameNosuffix)
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
if _, err := os.Stat(fullPathNosuffix); os.IsNotExist(err) {
break // break only if neither file nor file with suffix exist
}
}
n++
}
if err := os.Mkdir(filepath.Join(docsRoot, entriesDir, foldername), 0755); err != nil {
println("%s", err.Error())
os.Exit(1)
}
if p.template == "job_applications" {
filenames := []string{
"info.toml",
"events.toml",
}
for _, filename := range filenames {
dirPath := filepath.Join(docsRoot, entriesDir, foldername)
tmplName := filename
var text string
if filename == "info.toml" {
text = content // main template for this already loaded
} else {
text, loadTemplateErr = loadTemplate(tmplName)
if loadTemplateErr != nil {
fmt.Printf("Error loading template: %v\n", loadTemplateErr)
os.Exit(1)
}
}
createFile(dirPath, filename, text)
}
} else {
dirPath := filepath.Join(docsRoot, entriesDir, foldername)
createFile(dirPath, "info.toml", content)
}
}
func loadTemplate(templateName string) (string, error) {
templatePath := filepath.Join("templates", templateName)
if !strings.HasSuffix(templatePath, ".toml") {
templatePath += ".toml"
}
content, err := os.ReadFile(templatePath)
if err != nil {
return "", fmt.Errorf("failed to read template file: %v", err)
}
return string(content), nil
}
func createFile(entriesDir string, filename string, content string) {
fullPath := filepath.Join(entriesDir, filename)
file, err := os.Create(fullPath)
if err != nil {
fmt.Printf("Error creating file: %v\n", err)
return
}
defer file.Close()
_, err = file.WriteString(content)
if err != nil {
fmt.Printf("Error writing to file: %v\n", err)
return
}
fmt.Printf("Created file: %s\n", fullPath)
}