-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.go
133 lines (111 loc) · 3.7 KB
/
metadata.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
package domain
import (
"path/filepath"
"reflect"
"strings"
"github.com/gookit/color"
"github.com/skuid/domain/errors"
"github.com/skuid/domain/logging"
"github.com/skuid/domain/util"
)
type NlxMetadata struct {
Apps []string `json:"apps"`
AuthProviders []string `json:"authproviders"`
ComponentPacks []string `json:"componentpacks"`
DataServices []string `json:"dataservices"`
DataSources []string `json:"datasources"`
DesignSystems []string `json:"designsystems"`
Variables []string `json:"variables"`
Files []string `json:"files"`
Pages []string `json:"pages"`
PermissionSets []string `json:"permissionsets"`
Profiles []string `json:"profiles"`
Site []string `json:"site"`
Themes []string `json:"themes"`
}
func GetFieldValueByNameError(target string) error {
return errors.Error("GetFieldValueByName('%v') failed", target)
}
func (from NlxMetadata) GetFieldValueByName(target string) (names []string, err error) {
mType := reflect.TypeOf(NlxMetadata{})
var name string
for i := 0; i < mType.NumField(); i++ {
field := mType.Field(i)
if field.Tag.Get("json") == target {
name = field.Name
break
}
}
if name == "" {
err = GetFieldValueByNameError(target)
return
}
value := reflect.ValueOf(from)
field := value.FieldByName(name)
if field.IsValid() {
names = field.Interface().([]string)
return
}
logging.Get().Tracef("Somehow able to find field name %v but not its value as []string in the metadata", name)
err = GetFieldValueByNameError(target)
return
}
func (m NlxMetadata) FilterItem(item string) (keep bool) {
cleanRelativeFilePath := util.FromWindowsPath(item)
directory := filepath.Dir(cleanRelativeFilePath)
baseName := filepath.Base(cleanRelativeFilePath)
// Find the lowest level folder
dirSplit := strings.Split(directory, string(filepath.Separator))
metadataType, subFolders := dirSplit[0], dirSplit[1:]
filePathArray := append(subFolders, baseName)
filePath := strings.Join(filePathArray, string(filepath.Separator))
validMetadataNames, err := m.GetFieldValueByName(metadataType)
if validMetadataNames == nil || len(validMetadataNames) == 0 {
logging.Get().Tracef("No valid names for this directory: %v", color.Gray.Sprint(item))
return
}
if err != nil {
logging.Get().Errorf("Metadata Filter Error: %v", err)
return
}
if util.StringSliceContainsAnyKey(validMetadataNames, []string{
// Most common case --- check for our metadata with .json stripped
strings.TrimSuffix(filePath, ".json"),
// See if our filePath is in the valid metadata, if so, we're done
filePath,
}) {
keep = true
return
}
// Check for children of a component pack
if metadataType == "componentpacks" {
filePathParts := strings.Split(filePath, string(filepath.Separator))
if len(filePathParts) == 2 && util.StringSliceContainsKey(validMetadataNames, filePathParts[0]) {
logging.Get().Tracef("Keeping componentpack metadata file: %v", filePath)
keep = true
return
}
}
if util.StringSliceContainsAnyKey(validMetadataNames, []string{
// Check for our metadata with .xml stripped
strings.TrimSuffix(filePath, ".xml"),
// Check for our metadata with .skuid.json stripped
strings.TrimSuffix(filePath, ".skuid.json"),
// Check for theme inline css
strings.TrimSuffix(filePath, ".inline.css"),
}) {
logging.Get().Tracef("Keeping metadata file: %v", filePath)
keep = true
return
}
return
}
// GetMetadataTypeDirNames returns the directory names for a type
func GetMetadataTypeDirNames() (types []string) {
metadataType := reflect.TypeOf(NlxMetadata{})
for i := 0; i < metadataType.NumField(); i++ {
field := metadataType.Field(i)
types = append(types, field.Tag.Get("json"))
}
return types
}