github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/common/config/config.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package config
    20  
    21  import (
    22  	"encoding/json"
    23  	"fmt"
    24  	"log"
    25  	"os"
    26  	"reflect"
    27  	"strconv"
    28  )
    29  
    30  // ReadConfig ...
    31  func ReadConfig(path, pref string, conf interface{}) {
    32  
    33  	file, err := os.ReadFile(path)
    34  	if err != nil {
    35  		fmt.Fprintln(os.Stderr, fmt.Sprintf("Error reading config file %s", path))
    36  	} else {
    37  		err = json.Unmarshal(file, conf)
    38  		if err != nil {
    39  			fmt.Fprintln(os.Stderr, "Error: wrong format of config file")
    40  		}
    41  	}
    42  	checkEnv(pref, conf)
    43  
    44  	return
    45  }
    46  
    47  func checkEnv(pref string, conf interface{}) {
    48  
    49  	v := reflect.ValueOf(conf)
    50  	i := reflect.Indirect(v)
    51  	t := i.Type()
    52  
    53  	for i := 0; i < t.NumField(); i++ {
    54  		// Get the field, returns https://golang.org/pkg/reflect/#StructField
    55  		field := t.Field(i)
    56  		f := reflect.Indirect(v).FieldByName(field.Name)
    57  		fieldType := t.Field(i).Type
    58  
    59  		if !f.CanSet() || field.Tag.Get("env") == "-" {
    60  			continue
    61  		}
    62  
    63  		fieldName := field.Tag.Get("env")
    64  		if pref != "" {
    65  			fieldName = fmt.Sprintf("%s_%s", pref, fieldName)
    66  		}
    67  
    68  		switch fieldType.String() {
    69  		case "string", "common.RunMode":
    70  			if val := os.Getenv(fieldName); val != "" {
    71  				f.SetString(val)
    72  			}
    73  		case "bool":
    74  			if val := os.Getenv(fieldName); val != "" {
    75  				b, _ := strconv.ParseBool(val)
    76  				f.SetBool(b)
    77  			}
    78  		case "int":
    79  			if val := os.Getenv(fieldName); val != "" {
    80  				i, _ := strconv.ParseInt(val, 10, 32)
    81  				f.SetInt(i)
    82  			}
    83  		case "time.Duration":
    84  			if val := os.Getenv(fieldName); val != "" {
    85  				i, _ := strconv.ParseInt(val, 10, 32)
    86  				f.SetInt(i)
    87  			}
    88  		default:
    89  			log.Fatalf("unknown field type %s\n", fieldType.String())
    90  		}
    91  	}
    92  }