]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - remote/sync/triggers.go
remote: treat paths as slash separated in triggerPaths()
[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         slashpath "path"
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         // Slash separated paths are used for the configuration
54         const sep = "/"
55         if path == sep || path == "." {
56                 return []string{path}
57         }
58         parts := strings.Split(path, sep)
59         if strings.HasPrefix(path, sep) {
60                 // Absolute path
61                 parts[0] = sep
62         } else {
63                 // Relative path
64                 parts = append([]string{"."}, parts...)
65         }
66
67         var res []string
68         for i := 0; i < len(parts); i++ {
69                 res = append(res, slashpath.Join(parts[:i+1]...))
70         }
71         return res
72 }