This repository has been archived by the owner on Mar 13, 2019. It is now read-only.
forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glob_manager.go
167 lines (145 loc) · 3.77 KB
/
glob_manager.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package cfgfile
import (
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
// GlobManager allows to manage a directory of conf files. Using a glob pattern
// to match them, this object will allow to switch their state between enabled
// and disabled
type GlobManager struct {
glob string
enabledExtension string
disabledExtension string
files []*CfgFile
}
type CfgFile struct {
Name string
Path string
Enabled bool
}
// NewGlobManager takes a glob and enabled/disabled extensions and returns a GlobManager object.
// Parameters:
// - glob - matching conf files (ie: modules.d/*.yml)
// - enabledExtension - extension for enabled confs, must match the glob (ie: .yml)
// - disabledExtension - extension to append for disabled confs (ie: .disabled)
func NewGlobManager(glob, enabledExtension, disabledExtension string) (*GlobManager, error) {
if !strings.HasSuffix(glob, enabledExtension) {
return nil, errors.New("Glob should have the enabledExtension as suffix")
}
g := &GlobManager{
glob: glob,
enabledExtension: enabledExtension,
disabledExtension: disabledExtension,
}
if err := g.load(); err != nil {
return nil, err
}
return g, nil
}
func (g *GlobManager) load() error {
// empty previous data
g.files = nil
// Load enabled
watcher := NewGlobWatcher(g.glob)
files, _, err := watcher.Scan()
if err != nil {
return err
}
for _, path := range files {
// Trim cfg file name
g.files = append(g.files, &CfgFile{
Name: strings.TrimSuffix(filepath.Base(path), g.enabledExtension),
Enabled: true,
Path: path,
})
}
// Load disabled
watcher = NewGlobWatcher(g.glob + g.disabledExtension)
files, _, err = watcher.Scan()
if err != nil {
return err
}
for _, path := range files {
// Trim cfg file name
g.files = append(g.files, &CfgFile{
Name: strings.TrimSuffix(filepath.Base(path), g.enabledExtension+g.disabledExtension),
Enabled: false,
Path: path,
})
}
return nil
}
// ListEnabled conf files
func (g *GlobManager) ListEnabled() []*CfgFile {
var enabled []*CfgFile
for _, file := range g.files {
if file.Enabled {
enabled = append(enabled, file)
}
}
return enabled
}
// ListDisabled conf files
func (g *GlobManager) ListDisabled() []*CfgFile {
var disabled []*CfgFile
for _, file := range g.files {
if !file.Enabled {
disabled = append(disabled, file)
}
}
return disabled
}
// Enabled returns true if given conf file is enabled
func (g *GlobManager) Enabled(name string) bool {
for _, file := range g.files {
if name == file.Name {
return file.Enabled
}
}
return false
}
// Exists return true if the given conf exists (enabled or disabled)
func (g *GlobManager) Exists(name string) bool {
for _, file := range g.files {
if name == file.Name {
return true
}
}
return false
}
// Enable given conf file, does nothing if it's enabled already
func (g *GlobManager) Enable(name string) error {
for _, file := range g.files {
if name == file.Name {
if !file.Enabled {
newPath := strings.TrimSuffix(file.Path, g.disabledExtension)
if err := os.Rename(file.Path, newPath); err != nil {
return errors.Wrap(err, "enable failed")
}
file.Enabled = true
file.Path = newPath
}
return nil
}
}
return errors.Errorf("module %s not found", name)
}
// Disable given conf file, does nothing if it's disabled already
func (g *GlobManager) Disable(name string) error {
for _, file := range g.files {
if name == file.Name {
if file.Enabled {
newPath := file.Path + g.disabledExtension
if err := os.Rename(file.Path, newPath); err != nil {
return errors.Wrap(err, "disable failed")
}
file.Enabled = false
file.Path = newPath
}
return nil
}
}
return errors.Errorf("module %s not found", name)
}