Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
121 lines (106 loc) · 3.19 KB
/
Copy pathmain.go
File metadata and controls
121 lines (106 loc) · 3.19 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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/go-pkgz/fileutils"
"github.com/go-pkgz/fileutils/enum"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
workDir, err := os.MkdirTemp("", "fileutils-example-*")
if err != nil {
return err
}
defer func() { _ = os.RemoveAll(workDir) }()
source := filepath.Join(workDir, "source.txt")
if writeErr := os.WriteFile(source, []byte("fileutils example\n"), 0o600); writeErr != nil {
return writeErr
}
// copy the file, then move the copy into a directory that does not exist yet
copied := filepath.Join(workDir, "copied.txt")
if copyErr := fileutils.CopyFile(source, copied); copyErr != nil {
return copyErr
}
moved := filepath.Join(workDir, "archive", "moved.txt")
if moveErr := fileutils.MoveFile(copied, moved); moveErr != nil {
return moveErr
}
fmt.Printf("IsFile(moved.txt): %v\n", fileutils.IsFile(moved))
fmt.Printf("IsDir(archive): %v\n", fileutils.IsDir(filepath.Dir(moved)))
// any algorithm from the enum package works the same way
md5sum, err := fileutils.Checksum(moved, enum.HashAlgMD5)
if err != nil {
return err
}
sha256sum, err := fileutils.Checksum(moved, enum.HashAlgSHA256)
if err != nil {
return err
}
fmt.Printf("MD5: %s\n", md5sum)
fmt.Printf("SHA-256: %s\n", sha256sum)
// a free name to write to, the file itself is not created
tempName, err := fileutils.TempFileName(workDir, "upload-*.tmp")
if err != nil {
return err
}
fmt.Printf("temp name: %s\n", filepath.Base(tempName))
return watch(workDir, source)
}
// watch starts both watcher variants, makes a change and waits for it to be reported.
func watch(workDir, source string) error {
// the callback runs on the watcher goroutine, so hand the event over instead of working here
events := make(chan fileutils.FileEvent, 16)
handleEvent := func(event fileutils.FileEvent) {
select {
case events <- event:
default: // the buffer is enough for this example, drop the rest
}
}
fileWatcher, err := fileutils.NewFileWatcher(source, handleEvent)
if err != nil {
return err
}
defer func() {
if closeErr := fileWatcher.Close(); closeErr != nil {
log.Printf("close file watcher: %v", closeErr)
}
}()
// paths can be added and removed while the watcher runs
extra := filepath.Join(workDir, "extra.txt")
if touchErr := fileutils.TouchFile(extra); touchErr != nil {
return touchErr
}
if addErr := fileWatcher.AddPath(extra); addErr != nil {
return addErr
}
if removeErr := fileWatcher.RemovePath(extra); removeErr != nil {
return removeErr
}
recursiveWatcher, err := fileutils.WatchRecursive(workDir, handleEvent)
if err != nil {
return err
}
defer func() {
if closeErr := recursiveWatcher.Close(); closeErr != nil {
log.Printf("close recursive watcher: %v", closeErr)
}
}()
// the paths are registered before the constructors return, so this change is already watched
if changeErr := os.WriteFile(source, []byte("changed\n"), 0o600); changeErr != nil {
return changeErr
}
select {
case event := <-events:
fmt.Printf("Event: %s, path: %s\n", event.Type, event.Path)
case <-time.After(5 * time.Second):
fmt.Println("no event within the timeout")
}
return nil
}