adding custom config library, can read/write TOML, started config validation
This commit is contained in:
87
client/clientconfig/config.go
Normal file
87
client/clientconfig/config.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// ValidateConfig will go through the entire config and do basic sanity checks
|
||||
func ValidateConfig(conf *Gvcconfig, version string) error {
|
||||
if conf.Version == "" { // No version found, should we update it?
|
||||
fmt.Printf("No version found, inputing current client version: %s", 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", 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 *Gvcconfig) 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]
|
||||
}
|
||||
// TODO: write more validation
|
||||
return nil
|
||||
}
|
||||
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 {
|
||||
fmt.Println("unable to find folder listed in array, removing it: ", folder)
|
||||
compress.Folders[i] = compress.Folders[len(compress.Folders)-1]
|
||||
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?
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateRemotes ranges through all of the remotes and validates them
|
||||
func validateRemotes(conf *Gvcconfig) error {
|
||||
remotes := conf.Remotes
|
||||
for _, remote := range remotes {
|
||||
if remote.Name == "" {
|
||||
return fmt.Errorf("Unnamed remote found in config, all remotes require name")
|
||||
}
|
||||
if remote.Host == "" {
|
||||
return fmt.Errorf("Host cannot be empty: %s in %s", remote.Name, remote.Host)
|
||||
} // Checking for a valid port // TODO: might need to move this function to common library, feel like we will need this in the future?
|
||||
if remote.Port == 0 || remote.Port == 1 || remote.Port > 65535 { // TODO: edit the config library to allow default options?
|
||||
fmt.Printf("Remote %s did not have a valid port set, setting it to the default port 8888", remote.Name)
|
||||
remote.Port = 8888
|
||||
conf.Remotes = append(conf.Remotes, remote)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
24
client/clientconfig/structures.go
Normal file
24
client/clientconfig/structures.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package config
|
||||
|
||||
//Gvcconfig will be the struct that holds the entire client settings
|
||||
type Gvcconfig struct {
|
||||
Version string `toml:"version"`
|
||||
RootPath string `toml:rootPath`
|
||||
Remotes []Remote `toml:"remote"` //The remote servers for the repo
|
||||
Ignores Ignore `toml:"ignore"` //These files will be ignored for all add functions
|
||||
NoCompress Ignore `toml:"nocompress"` //For binary compression some files should be ignored because the performance hit isn't worth the size savings
|
||||
}
|
||||
|
||||
//Remote will be a slice of remote server information
|
||||
type Remote struct {
|
||||
Name string `toml:"name"`
|
||||
Host string `toml:"host"`
|
||||
Port int `toml:"port"`
|
||||
}
|
||||
|
||||
//Ignore is for ignoring files to add or ignoring compress
|
||||
type Ignore struct {
|
||||
Files []string `toml:"files"`
|
||||
Exts []string `toml:"exts"`
|
||||
Folders []string `toml:"folders"`
|
||||
}
|
Reference in New Issue
Block a user