Files
gvc/common/commonlib.go

53 lines
1.7 KiB
Go

package common
import (
"fmt"
"path/filepath"
)
// CheckFileTypes allows the server and client to do some basic validation on adding/removing file types to the toml file
// Since Ignore, NoCompress, and Locked have the same structure, you can provide the exclude list for any of them and this func will check that list to see if it already exists
func CheckFileTypes(input string, inputType string, excludeList FileTypes) error {
switch inputType {
case "file":
fileExt := filepath.Ext(input) // TODO more sanity checking on ext
for _, suppliedExt := range excludeList.Exts {
if fileExt == suppliedExt { //Check to make sure the file is not already on the list via ext
return fmt.Errorf("file ext is on excludeList, cannot add file ext %s", fileExt)
}
}
for _, suppliedFile := range excludeList.Files {
if input == suppliedFile {
return fmt.Errorf("file name is on excludeList, cannot add file with name %s", input)
}
}
case "folder":
for _, suppliedFolder := range excludeList.Folders {
if input == suppliedFolder {
return fmt.Errorf("folder name is on the excludeList, cannot add folder with name %s", input)
}
}
case "wildcard":
for _, suppliedExt := range excludeList.Exts {
if input == suppliedExt {
return fmt.Errorf("cannot add wildcard, since that ext is already added to the ignore config %s", input)
}
}
}
return nil
}
// RemoveDuplicatesFromSlice is used when merging configs, just purges duplicates from slice
func RemoveDuplicatesFromSlice(slice []string) []string {
seen := make(map[string]bool)
result := []string{}
for _, entry := range slice {
if _, value := seen[entry]; !value {
seen[entry] = true
result = append(result, entry)
}
}
return result
}