gocustomurls/conf.go

170 lines
3.6 KiB
Go
Raw Normal View History

2024-05-03 22:58:49 +00:00
package main
import (
"encoding/json"
"fmt"
"os"
2024-06-19 20:16:13 +00:00
"path/filepath"
"strings"
2024-05-03 22:58:49 +00:00
)
2024-06-19 20:16:13 +00:00
type parsedConf struct {
RulesFp string `json:"rulesPath"`
LogFp string `json:"logPath"`
Compression bool `json:"compress"`
SizeToRotate string `json:"sizeToRotate"`
Port string `json:"port"`
}
2024-05-06 07:17:11 +00:00
type Config struct {
MappingFilePath string
MappingRules ImportRulesMappings
}
2024-05-03 22:58:49 +00:00
2024-05-06 07:17:11 +00:00
type ImportRulesMappings struct {
2024-05-03 22:58:49 +00:00
Mappings []struct {
Protocol string `json:"protocol"`
VanityUrl string `json:"vanity_url"`
RealUrl string `json:"real_url"`
} `json:"mappings"`
}
2024-05-06 07:17:11 +00:00
type ImportRuleStruct struct {
VanityUrl string
Proto string
RepoUrl string
}
2024-05-03 22:58:49 +00:00
// isFile - check if fp is a valid file
func isFile(fp string) bool {
info, err := os.Stat(fp)
if os.IsNotExist(err) || !info.Mode().IsRegular() {
return false
}
return true
}
// load mapping file
2024-05-06 07:17:11 +00:00
func (c *Config) LoadMappingFile(fp string) error {
var mapping ImportRulesMappings
mappingFilePath := fp
if len(c.MappingFilePath) == 0 {
ok := isFile(mappingFilePath)
if !ok {
2024-05-28 23:30:59 +00:00
return fmt.Errorf("mappingfile %s is not found", mappingFilePath)
2024-05-06 07:17:11 +00:00
}
2024-05-28 23:30:59 +00:00
} else {
mappingFilePath = c.MappingFilePath
2024-05-03 22:58:49 +00:00
}
2024-05-06 07:17:11 +00:00
mappingFile, err := os.Open(mappingFilePath)
2024-05-03 22:58:49 +00:00
if err != nil {
2024-05-06 07:17:11 +00:00
return err
2024-05-03 22:58:49 +00:00
}
2024-05-06 07:17:11 +00:00
defer mappingFile.Close()
2024-05-03 22:58:49 +00:00
2024-05-06 07:17:11 +00:00
err = json.NewDecoder(mappingFile).Decode(&mapping)
2024-05-03 22:58:49 +00:00
if err != nil {
2024-05-06 07:17:11 +00:00
return err
}
c.MappingRules = mapping
if len(c.MappingFilePath) == 0 {
c.MappingFilePath = mappingFilePath
2024-05-03 22:58:49 +00:00
}
2024-05-06 07:17:11 +00:00
return nil
2024-05-03 22:58:49 +00:00
}
2024-06-19 20:16:13 +00:00
func getDefaults() (map[string]string, error) {
m := make(map[string]string)
confDir, err := os.UserConfigDir()
if err != nil {
return m, err
}
homeDir, err := os.UserHomeDir()
if err != nil {
return m, err
}
m["rulesFp"] = filepath.Join(confDir, "gocustomcurls", "rules.json")
m["confFp"] = filepath.Join(confDir, "gocustomcurls", "config.json")
m["logfp"] = filepath.Join(homeDir, ".gocustomurls", "logs", "app.log")
return m, nil
}
func generateDefaultConfigFile() (parsedConf, error) {
var p parsedConf
defaults, err := getDefaults()
if err != nil {
return p, err
}
parentDir := filepath.Dir(defaults["confFp"])
err = os.MkdirAll(parentDir, 0755)
if err != nil {
return p, err
}
f, err := os.OpenFile(defaults["confFp"], os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return p, err
}
defer f.Close()
p.RulesFp = defaults["rulesFp"]
p.LogFp = defaults["logFp"]
p.Port = "7070"
p.Compression = true
p.SizeToRotate = "5MiB"
jsonString, _ := json.Marshal(p)
err = os.WriteFile(defaults["confFp"], jsonString, 0666)
if err != nil {
return p, err
}
return p, nil
}
func checkIfSizeIsConfigured(fsize string) (bool, error) {
suffixes := []string{"KB", "MB", "GB"}
var found string
for _, suffix := range suffixes {
if strings.HasSuffix(fsize, suffix) {
found = suffix
}
}
if len(found) == 0 {
return false, fmt.Errorf("%s has the incorrect suffix, Please use one of this suffixes {\"K\", \"KB\",\"M\", \"MB\", \"G\", \"GB\"}", fsize)
}
return true, nil
}
// load the main config file
func (c *Config) LoadMainConfigFile(fp string) (parsedConf, error) {
var conf parsedConf
var err error
ok := isFile(fp)
if !ok {
// generate config file
errorLog.Println("Warning, generating default config file")
conf, err = generateDefaultConfigFile()
if err != nil {
return conf, err
}
c.MappingFilePath = conf.RulesFp
return conf, nil
}
f, err := os.Open(fp)
if err != nil {
return conf, err
}
defer f.Close()
err = json.NewDecoder(f).Decode(&conf)
if err != nil {
return conf, err
}
_, err = checkIfSizeIsConfigured(conf.SizeToRotate)
if err != nil {
return conf, err
}
c.MappingFilePath = conf.RulesFp
return conf, nil
}