diff options
author | Sina Ghaderi <32870524+Sina-Ghaderi@users.noreply.github.com> | 2021-03-19 08:06:20 +0330 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-19 08:06:20 +0330 |
commit | b8bb371ca118aa63785adffd98bb24cc1113f1c5 (patch) | |
tree | 00bab7b7194f92bee0e831958e34c099efbf80ff /vendor/config | |
parent | e16ec4f65419aefa0bb401d6af2e30be0d6bfa25 (diff) |
pushing to git services
Diffstat (limited to 'vendor/config')
-rw-r--r-- | vendor/config/config.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/vendor/config/config.go b/vendor/config/config.go new file mode 100644 index 0000000..525a120 --- /dev/null +++ b/vendor/config/config.go @@ -0,0 +1,50 @@ +package config + +import ( + "bufio" + "fmt" + "os" + "regexp" + "strings" + "syslog" +) + +const literalRegex string = `^(\s*)((?i)(socket_path|sql_database|sql_username|sql_address|sql_tcpport|sql_password|default_ratelimit|socket_perm|listener_type|listen_addr)(?i))(\s*)=` + +type Config struct{} + +var mapconf = make(map[string][]string) + +func ReadConfigFromFile(path string) *Config { + file, err := os.Open(path) + if err != nil { + panic(syslog.BigError{Why: err, Cod: 1}) + } + defer file.Close() + re := regexp.MustCompile(literalRegex) + scanner := bufio.NewScanner(file) + for scanner.Scan() { + if find := re.MatchString(scanner.Text()); find { + literal := re.FindString(scanner.Text()) + mapconf[strings.TrimSpace(literal[:len(literal)-1])] = strings.Fields(strings.Split(scanner.Text(), literal)[1]) + } + } + if err := scanner.Err(); err != nil { + panic(syslog.BigError{Why: err, Cod: 1}) + } + return &Config{} +} + +func (*Config) GetConf(str string, index int) string { + if args, owkey := mapconf[str]; owkey { + if len(args) == 0 { + panic(syslog.BigError{Why: fmt.Errorf("literal %v have no args in config file", str), Cod: 1}) + } + if index < len(args) { + return args[index] + } + + panic(syslog.BigError{Why: fmt.Errorf("not enough args in literal %v, can not reach arg %v, it's empty", str, index), Cod: 1}) + } + panic(syslog.BigError{Why: fmt.Errorf("literal %v can not be found in config file", str), Cod: 1}) +} |