github.com/jmooring/hugo@v0.47.1/config/configProvider.go (about) 1 // Copyright 2017-present The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package config 15 16 import ( 17 "strings" 18 19 "github.com/spf13/cast" 20 21 "github.com/spf13/viper" 22 ) 23 24 // Provider provides the configuration settings for Hugo. 25 type Provider interface { 26 GetString(key string) string 27 GetInt(key string) int 28 GetBool(key string) bool 29 GetStringMap(key string) map[string]interface{} 30 GetStringMapString(key string) map[string]string 31 GetStringSlice(key string) []string 32 Get(key string) interface{} 33 Set(key string, value interface{}) 34 IsSet(key string) bool 35 } 36 37 // FromConfigString creates a config from the given YAML, JSON or TOML config. This is useful in tests. 38 func FromConfigString(config, configType string) (Provider, error) { 39 v := viper.New() 40 v.SetConfigType(configType) 41 if err := v.ReadConfig(strings.NewReader(config)); err != nil { 42 return nil, err 43 } 44 return v, nil 45 } 46 47 // GetStringSlicePreserveString returns a string slice from the given config and key. 48 // It differs from the GetStringSlice method in that if the config value is a string, 49 // we do not attempt to split it into fields. 50 func GetStringSlicePreserveString(cfg Provider, key string) []string { 51 sd := cfg.Get(key) 52 if sds, ok := sd.(string); ok { 53 return []string{sds} 54 } else { 55 return cast.ToStringSlice(sd) 56 } 57 }