go_conf/conf.go

32 lines
801 B
Go
Raw Normal View History

2020-09-12 01:25:44 -04:00
package conf
import (
"encoding/xml"
2021-07-04 03:46:17 -04:00
"fmt"
2020-09-12 01:25:44 -04:00
"io/ioutil"
2021-07-04 03:46:17 -04:00
"github.com/pkg/errors"
2020-09-12 01:25:44 -04:00
2021-07-04 03:46:17 -04:00
"r00t2.io/sysutils/paths"
2020-09-12 01:25:44 -04:00
)
// ParseConf parses the path of the XML config provided at confPath ptr by using the confStruct struct ptr.
// If an XSD is found in the struct, it will be validated against (returning pass/fail as bool), otherwise always return true unless error.
func ParseConf(confPath *string, confStruct *struct{}) (bool, error) {
exists, err := paths.RealPathExists(confPath)
if !exists {
return false, errors.New(fmt.Sprintf("Configuration path %v does not exist", *confPath))
} else if err != nil {
return false, err
}
xmlDoc, err := ioutil.ReadFile(*confPath)
if err != nil {
return false, err
}
err = xml.Unmarshal(xmlDoc, confStruct)
if err != nil {
return false, err
}
2021-07-04 03:46:17 -04:00
}