-
Notifications
You must be signed in to change notification settings - Fork 4
/
add.go
67 lines (61 loc) · 1.49 KB
/
add.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
package project
import (
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"projectforge.dev/projectforge/app/lib/filesystem"
"projectforge.dev/projectforge/app/util"
)
func (s *Service) add(path string, parent *Project, logger util.Logger) (*Project, error) {
if parent != nil && !strings.HasPrefix(path, "/") {
path = filepath.Join(parent.Path, path)
}
b, p, err := s.load(path, logger)
if err != nil {
return nil, err
}
if parent != nil {
p.Parent = parent.Key
}
s.cacheLock.Lock()
curr, ok := s.cache[p.Key]
s.cacheLock.Unlock()
if ok {
return nil, errors.Errorf("can't overwrite cache entry for project [%s] located at [%s]", curr.Key, curr.Path)
}
s.cacheLock.Lock()
s.cache[p.Key] = p
s.fileContent[p.Key] = b
s.cacheLock.Unlock()
return p, nil
}
func (s *Service) getAdditional(logger util.Logger) ([]string, bool) {
additionalContent, err := os.ReadFile(additionalFilename)
if err != nil {
return nil, false
}
var additional []string
err = util.FromJSON(additionalContent, &additional)
if err != nil {
logger.Warnf("unable to parse additional projects from [%s]: %+v", additionalFilename, err)
}
return additional, true
}
func (s *Service) addAdditional(path string, logger util.Logger) {
add, ok := s.getAdditional(logger)
if !ok {
return
}
hit := false
for _, a := range add {
if a == path {
hit = true
break
}
}
if !hit {
add = append(add, path)
_ = os.WriteFile(additionalFilename, util.ToJSONBytes(add, true), filesystem.DefaultMode)
}
}