1 // MsgSyncReq: run triggers for changed files
3 // Copyright (C) 2021 Simon Ruderich
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.
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.
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/>.
24 "ruderich.org/simon/safcm"
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 {
33 // Queue each trigger only once
34 if s.triggersActive[path] {
36 "files: %q: skipping trigger on %q, already active",
41 s.log.Verbosef("files: %q: queuing trigger on %q",
43 s.triggers = append(s.triggers, path)
44 s.triggersActive[path] = true
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
55 if path == sep || path == "." {
58 parts := strings.Split(path, sep)
59 if strings.HasPrefix(path, sep) {
64 parts = append([]string{"."}, parts...)
68 for i := 0; i < len(parts); i++ {
69 res = append(res, slashpath.Join(parts[:i+1]...))