forked from timjwright/godoit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
114 lines (99 loc) · 2.73 KB
/
scanner.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
package main
import (
"log"
"path/filepath"
"os"
"path"
"io"
)
type GoDoItScanner struct {
executor JobExecutor
config *GoDoItConfig
jobSets map[string]*JobSet
}
func NewScanner(config *GoDoItConfig, output io.Writer) *GoDoItScanner {
return &GoDoItScanner{
JobExecutorFromScript(config.JobExecutorScript, output),
config,
make(map[string]*JobSet)}
}
func (scanner *GoDoItScanner) Run() {
if scanner.Scan() {
scanner.PrintJobs()
}
}
func (scanner *GoDoItScanner) Scan() bool {
log.Println("Scanning for changes...")
foundDirectories := make(map [string]bool)
// Scan all the patterns
addedJobSets := scanPatterns(scanner, foundDirectories)
// Remove any unwanted directories
removedJobSets := removeDirectories(scanner, foundDirectories)
// Scan directory entries
updatedJobSets := scanDirectory(scanner)
jobsChanged := addedJobSets || removedJobSets || updatedJobSets
log.Println(" Scanning for changes...done")
return jobsChanged
}
func scanPatterns(scanner *GoDoItScanner, foundDirectories map[string]bool) bool {
updated := false
for _,element := range scanner.config.Include {
path := path.Clean(os.ExpandEnv(element))
log.Printf(" Scanning directories matching %s", path)
directories,err := filepath.Glob(path)
if err != nil {
log.Printf(" Failed to scan, %s", path)
continue
}
thisUpdated := ensureDirectory(scanner, directories, foundDirectories)
updated = updated || thisUpdated
}
return updated
}
func ensureDirectory(scanner *GoDoItScanner, directories []string, foundDirectories map[string]bool) bool {
updated := false
for _,directory := range directories {
if info, err := os.Stat(directory); err != nil || ! info.IsDir() {
continue
}
foundDirectories[directory] = true
if _,ok := scanner.jobSets[directory]; ! ok {
log.Printf(" Adding directory, %s", directory)
jobSet := NewJobSet(scanner.executor, directory)
scanner.jobSets[directory] = jobSet
jobSet.Scan()
updated = true
}
}
return updated
}
func removeDirectories(scanner *GoDoItScanner, foundDirectories map[string]bool) bool {
updated := false
for directory,jobSet := range scanner.jobSets {
if _, ok := foundDirectories[directory]; ! ok {
jobSet.Stop()
delete(scanner.jobSets, directory)
updated = true
}
}
return updated
}
func scanDirectory(scanner *GoDoItScanner) bool {
updated := false
for _,jobSet := range scanner.jobSets {
thisUpdated := jobSet.Scan()
updated = updated || thisUpdated
}
return updated
}
func (scanner *GoDoItScanner) PrintJobs() {
for _,jobSet := range scanner.jobSets {
jobSet.printJobs()
}}
func (scanner *GoDoItScanner) Stop() {
log.Println("Stopping jobs...")
for _,jobSet := range scanner.jobSets {
jobSet.Stop()
}
log.Println(" Stopping jobs...done")
}