]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - remote/sync/triggers.go
d201835e7e7271b9729da033a409c53b004edce9
[safcm/safcm.git] / remote / sync / triggers.go
1 // MsgSyncReq: run triggers for changed files
2
3 // Copyright (C) 2021  Simon Ruderich
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 package sync
19
20 import (
21         "path/filepath"
22         "strings"
23
24         "ruderich.org/simon/safcm"
25 )
26
27 // queueTriggers queues all triggers applying to file.
28 func (s *Sync) queueTriggers(file *safcm.File) {
29         for _, path := range triggerPaths(file.Path) {
30                 if s.req.Files[path].TriggerCommands == nil {
31                         continue
32                 }
33                 // Queue each trigger only once
34                 if s.triggersActive[path] {
35                         s.log.Debugf(
36                                 "files: %q: skipping trigger on %q, already active",
37                                 file.Path, path)
38                         continue
39                 }
40
41                 s.log.Verbosef("files: %q: queuing trigger on %q",
42                         file.Path, path)
43                 s.triggers = append(s.triggers, path)
44                 s.triggersActive[path] = true
45         }
46 }
47
48 // triggerPaths returns all possible trigger paths for path, that is the path
49 // itself and all parent paths. The paths are returned in reverse order so
50 // more specific triggers can override effects of less specific ones (first
51 // "/" or ".", then the parents and finally path itself).
52 func triggerPaths(path string) []string {
53         sep := string(filepath.Separator)
54         if path == sep || path == "." {
55                 return []string{path}
56         }
57         parts := strings.Split(path, sep)
58         if strings.HasPrefix(path, sep) {
59                 // Absolute path
60                 parts[0] = sep
61         } else {
62                 // Relative path
63                 parts = append([]string{"."}, parts...)
64         }
65
66         var res []string
67         for i := 0; i < len(parts); i++ {
68                 res = append(res, filepath.Join(parts[:i+1]...))
69         }
70         return res
71 }