www.byjp.me/tools/shared/config.go
JP Hastings-Spital e7c8e60bcc Facebook import
I kinda screwed with everything else to homogenise all the functions - the FB stuff works, but I've definitely broken some of the other importers.

I can work on fixing them in the future if I need them; otherwise others can look at previous commits!
2024-04-04 17:11:14 +01:00

67 lines
1.3 KiB
Go

package shared
import (
"fmt"
"os"
"reflect"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
)
func LoadConfig(prefix string, config interface{}) error {
if reflect.TypeOf(config).Kind() != reflect.Ptr {
return fmt.Errorf("config must be a struct pointer")
}
configElem := reflect.ValueOf(config).Elem()
if configElem.Kind() != reflect.Struct {
return fmt.Errorf("config must be a struct pointer")
}
if err := godotenv.Load(); err != nil {
return err
}
if err := envconfig.Process(prefix, config); err != nil {
return err
}
// rootField, hasRoot := configElem.Type().FieldByName("Root")
// if !hasRoot {
// return nil
// }
// if rootField.Type.Kind() != reflect.String {
// return fmt.Errorf("config field 'Root' must be a string, not %s", rootField.Type)
// }
// root := rootField..String()
// fmt.Printf("%s, %T\n", root, root)
// if !isDir(path.Join(root, "content")) {
// return fmt.Errorf(
// "root must be the root of your hugo blog (%s/content/ is not a directory)",
// root,
// )
// }
return nil
}
func isFile(pathStr string) bool {
st, err := os.Stat(pathStr)
if os.IsNotExist(err) {
return false
}
return st.Mode().IsRegular()
}
func isDir(pathStr string) bool {
st, err := os.Stat(pathStr)
if os.IsNotExist(err) {
return false
}
return st.IsDir()
}