-
Notifications
You must be signed in to change notification settings - Fork 4
/
add.go
71 lines (64 loc) · 1.76 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
68
69
70
71
package project
import (
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/lib/filesystem"
"projectforge.dev/projectforge/app/util"
)
func (s *Service) add(path string, parent *Project) (*Project, error) {
if parent != nil && !strings.HasPrefix(path, "/") {
path = filepath.Join(parent.Path, path)
}
b, p, err := s.load(path)
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) getAdditionalFilename(fs filesystem.FileLoader) string {
ret := ".projectforge/additional-projects.json"
if _, err := fs.Stat(ret); err == nil {
return ret
}
return s.additional
}
func (s *Service) getAdditional(fs filesystem.FileLoader, logger util.Logger) ([]string, bool) {
additionalContent, err := fs.ReadFile(s.getAdditionalFilename(fs))
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", s.getAdditionalFilename(fs), err)
}
return additional, true
}
func (s *Service) addAdditional(path string, fs filesystem.FileLoader, logger util.Logger) {
add, ok := s.getAdditional(fs, logger)
if !ok {
return
}
hit := lo.ContainsBy(add, func(a string) bool {
return a == path
})
if !hit {
add = append(add, path)
_ = fs.WriteFile(s.getAdditionalFilename(fs), util.ToJSONBytes(add, true), filesystem.DefaultMode, true)
}
}