252 lines
6.9 KiB
Go
252 lines
6.9 KiB
Go
/*
|
|
* Copyright (C) 2017 eschao <esc.chao@gmail.com>
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Default configuration file
|
|
const (
|
|
DefaultJSONConfig = "config.json"
|
|
DefaultYamlConfig = "config.yaml"
|
|
DefaultPropConfig = "config.properties"
|
|
)
|
|
|
|
const (
|
|
JSONConfigType = "json"
|
|
YamlConfigType = "yaml"
|
|
PropConfigType = "properties"
|
|
)
|
|
|
|
// ParseDefault parses the given structure, extract default value from its tag
|
|
// and set structure with these values.
|
|
// Normally, ParseDefault should be called before any other parsing functions
|
|
// to set default values for structure.
|
|
func ParseDefault(i interface{}) error {
|
|
ptrRef := reflect.ValueOf(i)
|
|
|
|
if ptrRef.IsNil() || ptrRef.Kind() != reflect.Ptr {
|
|
return fmt.Errorf("expect a structure pointer type instead of %s",
|
|
ptrRef.Kind().String())
|
|
}
|
|
|
|
valueOfStruct := ptrRef.Elem()
|
|
if valueOfStruct.Kind() != reflect.Struct {
|
|
return fmt.Errorf("expect a structure pointer type instead of %s",
|
|
valueOfStruct.Kind().String())
|
|
}
|
|
|
|
return parseValue(valueOfStruct)
|
|
}
|
|
|
|
func parseValue(v reflect.Value) error {
|
|
typeOfStruct := v.Type()
|
|
var err error
|
|
for i := 0; i < v.NumField() && err == nil; i++ {
|
|
valueOfField := v.Field(i)
|
|
kindOfField := valueOfField.Kind()
|
|
structOfField := typeOfStruct.Field(i)
|
|
|
|
if kindOfField == reflect.Ptr {
|
|
if !valueOfField.IsNil() && valueOfField.CanSet() {
|
|
err = ParseDefault(valueOfField.Interface())
|
|
} else {
|
|
continue
|
|
}
|
|
} else if kindOfField == reflect.Struct {
|
|
err = parseValue(valueOfField)
|
|
}
|
|
|
|
defValue, ok := structOfField.Tag.Lookup("default")
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
kind := valueOfField.Kind()
|
|
switch kind {
|
|
case reflect.Bool:
|
|
err = SetValueWithBool(valueOfField, defValue)
|
|
case reflect.String:
|
|
valueOfField.SetString(defValue)
|
|
case reflect.Int8:
|
|
err = SetValueWithIntX(valueOfField, defValue, 8)
|
|
case reflect.Int16:
|
|
err = SetValueWithIntX(valueOfField, defValue, 16)
|
|
case reflect.Int, reflect.Int32:
|
|
err = SetValueWithIntX(valueOfField, defValue, 32)
|
|
case reflect.Int64:
|
|
err = SetValueWithIntX(valueOfField, defValue, 64)
|
|
case reflect.Uint8:
|
|
err = SetValueWithUintX(valueOfField, defValue, 8)
|
|
case reflect.Uint16:
|
|
err = SetValueWithUintX(valueOfField, defValue, 16)
|
|
case reflect.Uint, reflect.Uint32:
|
|
err = SetValueWithUintX(valueOfField, defValue, 32)
|
|
case reflect.Uint64:
|
|
err = SetValueWithUintX(valueOfField, defValue, 64)
|
|
case reflect.Float32:
|
|
err = SetValueWithFloatX(valueOfField, defValue, 32)
|
|
case reflect.Float64:
|
|
err = SetValueWithFloatX(valueOfField, defValue, 64)
|
|
case reflect.Slice:
|
|
sp, ok := structOfField.Tag.Lookup("separator")
|
|
if !ok {
|
|
sp = ":"
|
|
}
|
|
err = SetValueWithSlice(valueOfField, defValue, sp)
|
|
|
|
default:
|
|
return fmt.Errorf("unsupported type: %s", kind.String())
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// ParseCli parses given structure interface and set it with command line input
|
|
func ParseCli(i interface{}) error {
|
|
cli := NewCLI(os.Args[0])
|
|
if err := cli.Init(i); err != nil {
|
|
return err
|
|
}
|
|
if err := cli.Parse(os.Args[1:]); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ParseConfig parses given structure interface and set it with default
|
|
// configuration file.
|
|
// configFlag is a command line flag to tell where to locate configure file.
|
|
// If the config file doesn't exist, the default config fill will be searched
|
|
// under the same folder with the fixed order: config.json, config.yaml and
|
|
// config.properties
|
|
func ParseConfig(i interface{}, configFlag string) error {
|
|
configFile := flag.String(configFlag, "", "Specify configuration file")
|
|
flag.Parse()
|
|
return ParseConfigFile(i, *configFile)
|
|
}
|
|
|
|
// ParseConfigFile parses given structure interface and set its value with
|
|
// the specified configuration file
|
|
func ParseConfigFile(i interface{}, configFile string) error {
|
|
var err error
|
|
if configFile == "" {
|
|
configFile, err = getDefaultConfigFile()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
configType, err := getConfigFileType(configFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
switch configType {
|
|
case JSONConfigType:
|
|
err = parseJSON(i, configFile)
|
|
case YamlConfigType:
|
|
err = parseYaml(i, configFile)
|
|
case PropConfigType:
|
|
err = parseProp(i, configFile)
|
|
default:
|
|
err = fmt.Errorf("unsupported config file: %s", configFile)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// parseJSON parses JSON file and set structure with its value
|
|
func parseJSON(i interface{}, jsonFile string) error {
|
|
raw, err := os.ReadFile(jsonFile)
|
|
if err != nil {
|
|
return fmt.Errorf("open json config file: %s", err.Error())
|
|
}
|
|
|
|
return json.Unmarshal(raw, i)
|
|
}
|
|
|
|
// parseYaml parses Yaml file and set structure with its value
|
|
func parseYaml(i interface{}, yamlFile string) error {
|
|
raw, err := os.ReadFile(yamlFile)
|
|
if err != nil {
|
|
return fmt.Errorf("open yaml config file: %s", err.Error())
|
|
}
|
|
|
|
return yaml.Unmarshal(raw, i)
|
|
}
|
|
|
|
// parseProp parses Properties file and set structure with its value
|
|
func parseProp(_ interface{}, _ /*propFile*/ string) error {
|
|
return fmt.Errorf("properties config is not implemented")
|
|
}
|
|
|
|
// getDefaultConfigFile returns a existing default config file. The checking
|
|
// order is fixed with beginning from: config.json to config.yaml and
|
|
// config.properties
|
|
func getDefaultConfigFile() (string, error) {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
return "", fmt.Errorf("find default config file: %s", err.Error())
|
|
}
|
|
|
|
path := filepath.Dir(exe) + string(filepath.Separator)
|
|
|
|
// check json config
|
|
jsonConfig := path + DefaultJSONConfig
|
|
if _, err := os.Stat(jsonConfig); err == nil {
|
|
return jsonConfig, nil
|
|
}
|
|
|
|
// check yaml config
|
|
yamlConfig := path + DefaultYamlConfig
|
|
if _, err := os.Stat(yamlConfig); err == nil {
|
|
return yamlConfig, nil
|
|
}
|
|
|
|
// check prop config
|
|
propConfig := path + DefaultPropConfig
|
|
if _, err := os.Stat(propConfig); err == nil {
|
|
return propConfig, nil
|
|
}
|
|
|
|
return "", fmt.Errorf("default config file not found in path: %s", path)
|
|
}
|
|
|
|
// getConfigFileType analyzes config file extension name and return
|
|
// corresponding type: json, yaml or properties
|
|
func getConfigFileType(configFile string) (string, error) {
|
|
ext := filepath.Ext(configFile)
|
|
if ext == ".json" {
|
|
return JSONConfigType, nil
|
|
} else if ext == ".yaml" || ext == ".yml" {
|
|
return YamlConfigType, nil
|
|
} else if ext == ".properties" || ext == ".prop" {
|
|
return PropConfigType, nil
|
|
}
|
|
|
|
return "", fmt.Errorf("unsupported file type: %s", configFile)
|
|
}
|