-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (118 loc) · 3.01 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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"time"
)
func parseDateTime(datetimeStr string) (time.Time, error) {
if unixTime, err := strconv.ParseInt(datetimeStr, 10, 64); err == nil {
return time.Unix(unixTime, 0), nil
}
// List of datetime formats to try
formats := []string{
time.Layout,
time.ANSIC,
time.UnixDate,
time.RubyDate,
time.RFC822,
time.RFC822Z,
time.RFC850,
time.RFC1123,
time.RFC1123Z,
time.RFC3339,
time.RFC3339Nano,
time.DateTime,
}
// Try parsing the datetime string using the predefined formats
for _, format := range formats {
if parsedTime, err := time.Parse(format, datetimeStr); err == nil {
return parsedTime, nil
}
}
// If none of the formats work, return an error
return time.Time{}, fmt.Errorf("unable to parse datetime: %s", datetimeStr)
}
func main() {
// Define flags
var atimeOnly bool
var cSuppressCreation bool
var dTime string
var mtimeOnly bool
var rReference string
flag.BoolVar(&atimeOnly, "a", false, "change only the access time")
flag.BoolVar(&cSuppressCreation, "c", false, "do not create any files")
flag.StringVar(&dTime, "d", "", "set the access and modification times using the specified string")
flag.BoolVar(&mtimeOnly, "m", false, "change only the modification time")
flag.StringVar(&rReference, "r", "", "use this file's times instead of the current time")
// Parse command-line arguments
flag.Parse()
if flag.NArg() == 0 {
fmt.Println("usage: touch [OPTION]... FILE...")
os.Exit(1)
}
// Process each filename provided
for _, filename := range flag.Args() {
// Check if the file exists
fileInfo, err := os.Stat(filename)
if os.IsNotExist(err) {
// File does not exist, handle -c flag
if cSuppressCreation {
continue
}
// Create the file
file, err := os.Create(filename)
if err != nil {
fmt.Printf("error creating file: %v\n", err)
os.Exit(1)
}
file.Close()
// Get file info for the newly created file
fileInfo, err = os.Stat(filename)
if err != nil {
fmt.Printf("error stating file: %v\n", err)
os.Exit(1)
}
} else if err != nil {
fmt.Printf("error checking file: %v\n", err)
os.Exit(1)
}
// Determine the time to set
var atime, mtime time.Time
if rReference != "" {
refInfo, err := os.Stat(rReference)
if err != nil {
fmt.Printf("error stating reference file: %v\n", err)
os.Exit(1)
}
atime = refInfo.ModTime()
mtime = refInfo.ModTime()
} else if dTime != "" {
parsedTime, err := parseDateTime(dTime)
if err != nil {
fmt.Printf("error parsing time: %v\n", err)
os.Exit(1)
}
atime = parsedTime
mtime = parsedTime
} else {
currentTime := time.Now().Local()
atime = currentTime
mtime = currentTime
}
// Adjust times based on flags
if atimeOnly {
mtime = fileInfo.ModTime()
}
if mtimeOnly {
atime = fileInfo.ModTime()
}
// Update the file's timestamps
err = os.Chtimes(filename, atime, mtime)
if err != nil {
fmt.Printf("error updating timestamps: %v\n", err)
os.Exit(1)
}
}
}