-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (120 loc) · 3.74 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
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strconv"
"text/template"
"time"
)
var tmpl *template.Template
func getWeekDates(year int, month time.Month, day int) (int, string, string, map[string]string) {
date := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
_, isoWeek := date.ISOWeek()
// Calculate the start and end dates of the week
monday := date.AddDate(0, 0, -int(date.Weekday())+1)
sunday := monday.AddDate(0, 0, 6)
// Details for each day of the week
days := map[string]string{
"Montag": monday.Format("02.01.2006"),
"Dienstag": monday.AddDate(0, 0, 1).Format("02.01.2006"),
"Mittwoch": monday.AddDate(0, 0, 2).Format("02.01.2006"),
"Donnerstag": monday.AddDate(0, 0, 3).Format("02.01.2006"),
"Freitag": monday.AddDate(0, 0, 4).Format("02.01.2006"),
"Saturday": monday.AddDate(0, 0, 5).Format("02.01.2006"),
}
return isoWeek, monday.Format("02.01"), sunday.Format("02.01.2006"), days
}
func generateAndWriteFileNames(year int, outputFolder string) error {
var err error
tmpl, err = template.ParseFiles("template.txt")
if err != nil {
return fmt.Errorf("Error parsing template: %w", err)
}
for month := time.January; month <= time.December; month++ {
// Create a file for each week in the month
for day := 1; day <= 31; day += 7 {
// Skip if the date is invalid (e.g., February 30th)
if time.Date(year, month, day, 0, 0, 0, 0, time.UTC).IsZero() {
continue
}
isoWeek, startDate, endDate, days := getWeekDates(year, month, day)
fileName := fmt.Sprintf("KW%d_%s-%s.txt", isoWeek, startDate, endDate)
// Derive the file path relative to the output folder
filePath := filepath.Join(outputFolder, fileName)
file, err := os.Create(filePath)
if err != nil {
return err
}
data := map[string]string{
"Montag": days["Montag"],
"Dienstag": days["Dienstag"],
"Mittwoch": days["Mittwoch"],
"Donnerstag": days["Donnerstag"],
"Freitag": days["Freitag"],
}
var buffer bytes.Buffer
err = tmpl.Execute(&buffer, data)
if err != nil {
return fmt.Errorf("Error executing template: %w", err)
}
_, err = file.WriteString(buffer.String())
if err != nil {
return err
}
file.Close()
}
}
return nil
}
func main() {
var year int
var createNewFolder bool
var outputFolder string
// Check if command-line arguments are provided
if len(os.Args) > 1 {
// Parse the provided year argument
argYear, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println("Error parsing the year argument:", err)
return
}
year = argYear
// Check if a second argument is provided and is "y" or "1"
if len(os.Args) > 2 && (os.Args[2] == "y" || os.Args[2] == "1") {
createNewFolder = true
}
// Check if a third argument is provided as the output directory
if len(os.Args) > 3 {
outputFolder = os.Args[3]
}
} else {
// If no argument is provided, use the current year
year = time.Now().Year()
}
// Get the current working directory if outputFolder is not specified
if outputFolder == "" {
currentDir, err := os.Getwd()
if err != nil {
fmt.Println("Error getting current working directory:", err)
return
}
outputFolder = currentDir
}
// If createNewFolder is true, create a new folder based on the provided year
if createNewFolder {
outputFolder = filepath.Join(outputFolder, fmt.Sprintf("Berichtshefte_%d", year))
err := os.Mkdir(outputFolder, os.ModePerm)
if err != nil {
fmt.Println("Error creating a new folder:", err)
return
}
}
err := generateAndWriteFileNames(year, outputFolder)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Printf("Your Berichtshefte have been generated.\nYear used for operation: %d\nThe Berichtshefte are located at: %s\n", year, outputFolder)
}