]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - remote/sync/triggers.go
Use SPDX license identifiers
[safcm/safcm.git] / remote / sync / triggers.go
1 // MsgSyncReq: run triggers for changed files
2
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 // Copyright (C) 2021-2024  Simon Ruderich
5
6 package sync
7
8 import (
9         slashpath "path"
10         "strings"
11
12         "ruderich.org/simon/safcm"
13 )
14
15 // queueTriggers queues all triggers applying to file.
16 func (s *Sync) queueTriggers(file *safcm.File) {
17         for _, path := range triggerPaths(file.Path) {
18                 if s.req.Files[path].TriggerCommands == nil {
19                         continue
20                 }
21                 // Queue each trigger only once
22                 if s.triggersActive[path] {
23                         s.log.Debugf(
24                                 "files: %q: skipping trigger on %q, already active",
25                                 file.Path, path)
26                         continue
27                 }
28
29                 s.log.Verbosef("files: %q: queuing trigger on %q",
30                         file.Path, path)
31                 s.triggers = append(s.triggers, path)
32                 s.triggersActive[path] = true
33         }
34 }
35
36 // triggerPaths returns all possible trigger paths for path, that is the path
37 // itself and all parent paths. The paths are returned in reverse order so
38 // more specific triggers can override effects of less specific ones (first
39 // "/" or ".", then the parents and finally path itself).
40 func triggerPaths(path string) []string {
41         // Slash separated paths are used for the configuration
42         const sep = "/"
43         if path == sep || path == "." {
44                 return []string{path}
45         }
46         parts := strings.Split(path, sep)
47         if strings.HasPrefix(path, sep) {
48                 // Absolute path
49                 parts[0] = sep
50         } else {
51                 // Relative path
52                 parts = append([]string{"."}, parts...)
53         }
54
55         var res []string
56         for i := 0; i < len(parts); i++ {
57                 res = append(res, slashpath.Join(parts[:i+1]...))
58         }
59         return res
60 }