-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
168 lines (134 loc) · 3.31 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
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/reiver/go-path"
)
var location string
var verbose bool
var when string
var trial bool
func init() {
flag.StringVar(&location, "location", "", "location; the year, month, and time can be different depending on the location. ex: --location=America/Vancouver or --location=Asia/Seoul or --location=Asia/Tehran")
flag.BoolVar(&verbose, "v", false, "verbose")
flag.StringVar(&when, "when", "", "when. ex: --when=2022-11-04T18:03:45Z or --when=2022-11-05T01:03:45-07:00 or --when=now")
flag.BoolVar(&trial, "t", false, "trial")
flag.Parse()
}
// outputdatetimelayout is the output date-time layout.
// This is in not parsing date-time layout.
const outputdatetimelayout string = "2006-01-02T15:04:05-07:00"
func main() {
var loc *time.Location
{
var err error
loc, err = time.LoadLocation(location)
if nil != err {
fmt.Fprintf(os.Stderr, "ERROR: could not load location %q: %s\n", location, err)
os.Exit(1)
return
}
}
if verbose {
fmt.Printf("--location=%s\n", loc)
fmt.Printf("Location: %s\n", loc)
}
var t time.Time
{
switch {
case "" == when:
fmt.Fprintln(os.Stderr, "ERROR: '--when' must be specified; ex: --when=2022-11-04T18:03:45Z or --when=2022-11-05T01:03:45-07:00 or --when=now")
os.Exit(1)
return
case "now" == when:
t = time.Now()
default:
var err error
var parsetimeformat string = time.RFC3339
t, err = time.Parse(parsetimeformat, when)
if nil != err {
fmt.Fprintf(os.Stderr, "ERROR: could not parse date-time %q using time-format %q: %s\n", when, parsetimeformat, err)
os.Exit(1)
return
}
}
t = t.In(loc)
}
if verbose {
fmt.Printf("Date-Time: %s\n", t.Format(outputdatetimelayout))
}
var dirname string
{
dirname += "log/"
{
var year int = t.Year()
dirname += fmt.Sprintf("%d", year)
}
dirname += "/"
{
var month int = int(t.Month())
if month < 10 {
dirname += "0"
}
dirname += fmt.Sprintf("%d", month)
}
dirname += "/"
{
var day int = t.Day()
if day < 10 {
dirname += "0"
}
dirname += fmt.Sprintf("%d", day)
}
}
if verbose {
fmt.Printf("Dir-Name: %s\n", dirname)
}
var unixtimestamp int64 = t.Unix()
if verbose {
fmt.Printf("Unix-Time-Stamp: %d\n", unixtimestamp)
}
var filename string = fmt.Sprintf("%d.wiki", unixtimestamp)
if verbose {
fmt.Printf("File-Name: %s\n", filename)
}
var wikipath string = path.Join(dirname, filename)
if verbose {
fmt.Printf("Path: %s\n", wikipath)
}
if !trial {
const permissions os.FileMode = 0755
var path string = dirname
err := os.MkdirAll(path, permissions)
if nil != err {
fmt.Fprintf(os.Stderr, "ERROR: could not create directory %q: %s\n", path, err)
os.Exit(1)
return
}
fmt.Printf("created directory: %s\n", path)
}
if !trial {
const permissions os.FileMode = 0644
var path string = wikipath
var data []byte = []byte(
"wiki/1"+"\n"+
""+"\n"+
"§ Hello World!"+"\n"+
""+"\n"+
"⸺ by Joe Blow"+"\n"+
""+"\n"+
"⸺ published "+t.Format(outputdatetimelayout)+"\n"+
""+"\n"+
"Hello world!"+"\n",
)
err := os.WriteFile(path, data, permissions)
if nil != err {
fmt.Fprintf(os.Stderr, "ERROR: could not create file %q: %s\n", path, err)
os.Exit(1)
return
}
fmt.Printf("created file: %s\n", path)
}
}