Files
gvc/server/serverconfig/config.go

75 lines
2.2 KiB
Go

package config
import (
"fmt"
"os"
"github.com/deranjer/store"
)
// ConfigPath is the global path to the config that is injected from the main server package.
var ConfigPath string
// ValidateConfig will go through the entire config and do basic sanity checks
func ValidateConfig(conf *GvcServerConfig, version string) error {
if conf.Version == "" { // No version found, should we update it?
fmt.Printf("No version found, inputing current server version: %s\n", version)
conf.Version = version
}
if conf.RootPath == "" {
path, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to get current working directory, and rootpath of repo is not set: %s", err)
}
fmt.Printf("No root path found, inputting current working directory: %s\n", path)
}
err := validateRemotes(conf)
if err != nil {
return err
}
err = validateCompress(conf)
if err != nil {
return err
}
//TODO validate ignores (pretty similar to compress)
return nil
}
// validateCompress checks the compression settings to make sure they are valid //TODO return error needed?
func validateCompress(conf *GvcServerConfig) error {
compress := conf.NoCompress
for i, file := range compress.Files {
if file == "" {
fmt.Println("empty file in compress files, removing... ")
compress.Files[i] = compress.Files[len(compress.Files)-1]
continue
}
// TODO: write more validation
}
for i, folder := range compress.Folders {
file, err := os.Stat(folder) // TODO: check to see if it returns ABS or not, might need to convert
if err != nil {
continue
}
fileType := file.Mode()
if fileType.IsRegular() { // Not a folder
fmt.Println("compressed folder in array is not actually a folder, moving it to file compress: ", folder)
compress.Folders[i] = compress.Folders[len(compress.Folders)-1]
compress.Files = append(compress.Files, folder)
continue
}
}
for i, ext := range compress.Exts {
if ext == "" {
fmt.Println("empty ext in compress exts, removing... ")
compress.Exts[i] = compress.Exts[len(compress.Exts)-1]
continue
}
// TODO: validate there is a "." in front of the ext, if not add it?
}
err := store.Save(ConfigPath, &conf)
if err != nil {
fmt.Println("Error saving config back to toml file: ", err)
}
return nil
}