more server work on structs and config
This commit is contained in:
@@ -3,73 +3,92 @@ 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
|
||||
|
||||
// default config path
|
||||
var defaultConfigPath = "config" + string(os.PathListSeparator) + "serverConfig.toml"
|
||||
|
||||
// FindConfig Search for the config it 2 places, root and in the config/ folder
|
||||
func FindConfig() (string, error) {
|
||||
configFile, err := os.Stat("serverConfig.toml") //First checking root directory
|
||||
if err != nil {
|
||||
configFile, err := os.Stat(defaultConfigPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !configFile.IsDir() {
|
||||
return fmt.Sprintf(defaultConfigPath), nil
|
||||
}
|
||||
return "", fmt.Errorf("config not found: %s", err)
|
||||
}
|
||||
if !configFile.IsDir() {
|
||||
return "serverConfig.toml", nil
|
||||
}
|
||||
return "", fmt.Errorf("serverConfig.toml does not appear to be in the correct file format")
|
||||
}
|
||||
|
||||
// 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 == "" {
|
||||
if conf.RepoRootPath == "" {
|
||||
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)
|
||||
return fmt.Errorf("unable to get current working directory, and rootpath for the repos is not set: %s", err)
|
||||
}
|
||||
fmt.Printf("No root path found, inputting current working directory: %s\n", path)
|
||||
fmt.Printf("No repo 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)
|
||||
// Range through the repos and run some basic validation on all of them
|
||||
// for i, repo := range conf.Repos {
|
||||
// err := validateCompress(repo)
|
||||
// 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
|
||||
}
|
||||
// func validateCompress(repo RepoConfig) error {
|
||||
// compress := repo.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
|
||||
// }
|
||||
|
@@ -1,13 +1,19 @@
|
||||
package config
|
||||
|
||||
// GvcServerConfig will hold the base server settings
|
||||
type GvcServerConfig struct {
|
||||
Version string `toml:"version"` // The server version
|
||||
Repos []RepoConfig `toml:"repos"` // A struct of all the repos and their settings for the serve
|
||||
Version string `toml:"version"` // The server version
|
||||
Port int `toml:"port"` // The port that the server will listed on
|
||||
BindIP string `toml:"ip"` // What IP to bind the server to. If empty will bind to all interfaces
|
||||
RawPort int `toml:"rawport"` // The optional TCP port that the server will send raw files over
|
||||
RepoRootPath string `toml:"reporootpath"` // This will be the root path where (by default) all new repos will be stored at
|
||||
Repos []RepoConfig `toml:"repos"` // A struct of all the repos and their settings for the serve
|
||||
}
|
||||
|
||||
//GvcServerConfig will be the struct that holds the entire server settings
|
||||
// RepoConfig will be the struct that holds the config for a single repo
|
||||
type RepoConfig struct {
|
||||
KnownClients []Clients `toml:"clients"` //The remote servers for the repo
|
||||
KnownClients []Clients `toml:"clients"` //The remote servers for the repo
|
||||
RootPath string `toml:"rootpath"` // The absolute path to the root of this particular repo
|
||||
DefaultBranch string `toml:"defaultbranch"`
|
||||
LocalBranches []string `toml:"localbranches"` // LocalBranches constains a string list of branches on the server. Names must be unique. \\TODO: someday add folders like git for branches
|
||||
Locked FileTypes `toml:"locked"`
|
||||
|
Reference in New Issue
Block a user