-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfile_type.go
68 lines (61 loc) · 1.85 KB
/
file_type.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
package bun
import (
"fmt"
"sync"
)
// ContentType defines type of the content in the bundle file.
type ContentType string
const (
// CTJson represents CTJson files.
CTJson ContentType = "JSON"
// CTJournal represents CTJournal files.
CTJournal = "journal"
// CTDmesg represents dmesg files.
CTDmesg = "dmesg"
// CTOutput is a output of a command.
CTOutput = "output"
//CTOther file types
CTOther = "other"
)
// FileType Describes a kind of files in the bundle (e.g. dcos-marathon.service).
type FileType struct {
Name string `yaml:"name"`
ContentType ContentType `yaml:"contentType"`
Paths []string `yaml:"paths"`
Description string `yaml:"description"`
// DirTypes defines on which host types this file can be found.
// For example, dcos-marathon.service file can be found only on the masters.
DirTypes []DirType `yaml:"dirTypes"`
}
var (
fileTypes = make(map[string]FileType)
fileTypesMu sync.RWMutex
)
// RegisterFileType adds the file type to the filetype registry. It panics
// if the file type with the same name is already registered.
func RegisterFileType(f FileType) {
fileTypesMu.Lock()
defer fileTypesMu.Unlock()
if _, dup := fileTypes[f.Name]; dup {
panic(fmt.Sprintf("bun.RegisterFileType: called twice for file type %v", f.Name))
}
dirTypes := make(map[DirType]struct{})
for _, t := range f.DirTypes {
if _, ok := dirTypes[t]; ok {
panic(fmt.Sprintf("bun.RegisterFileType: duplicate DirType: %v in file type %v", t, f.Name))
}
dirTypes[t] = struct{}{}
}
fileTypes[f.Name] = f
}
// GetFileType returns a file type by its name. It panics if the file type
// is not in the registry.
func GetFileType(typeName string) FileType {
fileTypesMu.RLock()
defer fileTypesMu.RUnlock()
fileType, ok := fileTypes[typeName]
if !ok {
panic(fmt.Sprintf("bun.RegisterFileType: No such fileType: %v", typeName))
}
return fileType
}