configuration.go (3711B)
1 // This file is part of taler-go, the Taler Go implementation. 2 // Copyright (C) 2022 Martin Schanzenbach 3 // 4 // Taler Go is free software: you can redistribute it and/or modify it 5 // under the terms of the GNU Affero General Public License as published 6 // by the Free Software Foundation, either version 3 of the License, 7 // or (at your option) any later version. 8 // 9 // Taler Go is distributed in the hope that it will be useful, but 10 // WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 // Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 // 17 // SPDX-License-Identifier: AGPL3.0-or-later 18 19 package util 20 21 import ( 22 "errors" 23 "fmt" 24 "os" 25 "strings" 26 "time" 27 28 "gopkg.in/ini.v1" 29 ) 30 31 type TalerConfiguration struct { 32 // The ini file 33 Ini *ini.File 34 } 35 36 func (c *TalerConfiguration) GetString(section string, key string, fallback string) string { 37 value := c.Ini.Section(section).Key(strings.ToUpper(key)).String() 38 if len(value) == 0 { 39 return c.Ini.Section(section).Key(strings.ToLower(key)).MustString(fallback) 40 } 41 if len(value) == 0 { 42 return fallback 43 } 44 return value 45 } 46 47 func (c *TalerConfiguration) GetAmount(section string, key string, fallback *Amount) (*Amount, error) { 48 value := c.GetString(section, key, "") 49 d, err := ParseAmount(value) 50 if err != nil { 51 return fallback, fmt.Errorf("failed to parse %s as amount", value) 52 } 53 return d, nil 54 } 55 56 func (c *TalerConfiguration) GetDuration(section string, key string, fallback time.Duration) (time.Duration, error) { 57 value := c.GetString(section, key, "") 58 d, err := time.ParseDuration(value) 59 if err != nil { 60 return fallback, fmt.Errorf("failed to parse %s as duration", value) 61 } 62 return d, nil 63 } 64 65 func (c *TalerConfiguration) GetBool(section string, key string, fallback bool) bool { 66 value, err := c.Ini.Section(section).Key(strings.ToUpper(key)).Bool() 67 if err != nil { 68 return c.Ini.Section(section).Key(strings.ToLower(key)).MustBool(fallback) 69 } 70 return value 71 } 72 73 func (c *TalerConfiguration) GetInt64(section string, key string, fallback int64) int64 { 74 value, err := c.Ini.Section(section).Key(strings.ToUpper(key)).Int64() 75 if err != nil { 76 return c.Ini.Section(section).Key(strings.ToLower(key)).MustInt64(fallback) 77 } 78 return value 79 } 80 81 func (c *TalerConfiguration) GetInt(section string, key string, fallback int) int { 82 value, err := c.Ini.Section(section).Key(strings.ToUpper(key)).Int() 83 if err != nil { 84 return c.Ini.Section(section).Key(strings.ToLower(key)).MustInt(fallback) 85 } 86 return value 87 } 88 89 func getFileName(prefixPath string, relativeFileName string) string { 90 _, err := os.Stat(relativeFileName) 91 if errors.Is(err, os.ErrNotExist) { 92 _, err := os.Stat(prefixPath + "/" + relativeFileName) 93 if errors.Is(err, os.ErrNotExist) { 94 return "" 95 } 96 return prefixPath + "/" + relativeFileName 97 } 98 return relativeFileName 99 } 100 101 func (c *TalerConfiguration) GetFilename(section string, key string, fallback string, prefixPath string) string { 102 value := c.GetString(section, key, fallback) 103 return getFileName(prefixPath, value) 104 } 105 106 func (c *TalerConfiguration) IterateSections(sectionPrefix string) []string { 107 result := make([]string, 0) 108 for _, sec := range c.Ini.Sections() { 109 if !strings.HasPrefix(sec.Name(), sectionPrefix) { 110 continue 111 } 112 result = append(result, sec.Name()) 113 } 114 return result 115 } 116 117 func LoadConfiguration(file string) (TalerConfiguration, error) { 118 cfgIni, err := ini.LooseLoad(file) 119 if err != nil { 120 return TalerConfiguration{}, err 121 } 122 return TalerConfiguration{Ini: cfgIni}, nil 123 }