本文整理汇总了Golang中github.com/kylelemons/go-gypsy/yaml.ReadFile函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadFile函数的具体用法?Golang ReadFile怎么用?Golang ReadFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: assembleConfigFiles
func assembleConfigFiles(configFile string) (map[string]interface{}, error) {
logs.Info("Reading config file : %s", configFile)
doc, err := yaml.ReadFile(configFile)
if err != nil {
return nil, err
}
mapping := YamlUnmarshal(doc.Root).(map[string]interface{})
confD_path := path.Dir(configFile) + "/conf.d/*.yaml"
logs.Info("Reading config folder : %s", confD_path)
entries, err := filepath.Glob(confD_path)
if err != nil {
logs.Warn("Can't read relevant conf.d: %s", err)
} else {
for _, path := range entries {
logs.Info("Reading config file : %s", path)
doc, err := yaml.ReadFile(path)
if err != nil {
logs.Warn("Can't read relevant conf.d: %s", err)
} else {
inner := YamlUnmarshal(doc.Root).(map[string]interface{})
RecursiveMergeNoConflict(mapping, inner, "")
}
}
}
return mapping, nil
}
示例2: ReadConfig
func ReadConfig(filename string) (*Config, error) {
config, err := yaml.ReadFile(filename)
if err != nil {
return nil, err
}
return ParseConfig(config)
}
示例3: init
func init() {
Objects = make(map[string]*Object)
Keys = []string{}
file, _ := yaml.ReadFile("models/variant/data.yml")
for key, node := range file.Root.(yaml.Map) {
name := node.(yaml.Map)["name"].(yaml.Scalar).String()
source, err := ioutil.ReadFile(fmt.Sprintf("models/variant/data/%s", key))
snippet := ""
if err == nil {
snippet = string(source[:])
}
variants := []string{}
variants = append(variants, key)
if list, ok := node.(yaml.Map)["variants"]; ok {
for _, variant := range list.(yaml.List) {
variants = append(variants, variant.(yaml.Scalar).String())
}
}
Objects[key] = &Object{key, name, snippet, variants, []*Option{}}
Keys = append(Keys, key)
}
}
示例4: LoadConf
func LoadConf() {
var err error
conf, err = yaml.ReadFile("botconf.yaml")
if err != nil {
log.Panic(err)
}
Categories = List2SliceInConf("catagoris")
CategoriesSet = set.New(set.NonThreadSafe)
Groups = []Group{}
for _, v := range Categories {
CategoriesSet.Add(v)
for _, i := range List2SliceInConf(v) {
reg := regexp.MustCompile("^(.+) (http(s)?://(.*))$")
strs := reg.FindAllStringSubmatch(i, -1)
if !reg.MatchString(i) {
Groups = append(Groups,
Group{GroupName: i,
GroupURL: ""})
}
if len(strs) > 0 {
Groups = append(Groups,
Group{GroupName: strs[0][1],
GroupURL: strs[0][2]})
}
}
}
}
示例5: init
// init function, load the configs
// fill english_ignore_words_map
func init() {
// load config file
cfg_filename := "config.yaml"
config, err := yaml.ReadFile(cfg_filename)
if err != nil {
log.Fatalf("readfile(%s): %s", cfg_filename, err)
}
// get english ignore entire string
english_ignore, err := config.Get("english_ignore")
if err != nil {
log.Fatalf("%s parse error: %s\n", english_ignore, err)
}
// get each separated words
english_ignore_words_list := strings.Fields(english_ignore)
for _, word := range english_ignore_words_list {
word = strings.TrimSpace(word)
english_ignore_words_map[word] = 1
}
// get redis connection info
redis_config, err := yaml.Child(config.Root, "redis_server")
if err != nil {
log.Fatalf("redis config parse error: %s\n", err)
}
redis_config_m := redis_config.(yaml.Map)
host, port := redis_config_m["host"], redis_config_m["port"]
redis_conn, err = redis.Dial("tcp", fmt.Sprintf("%s:%s", host, port))
//defer redis_conn.Close()
if err != nil {
log.Fatalf("Can not connect to Redis Server: %s", err)
}
}
示例6: MakeFileInfo
func MakeFileInfo(path, ns, name string) *FileInfo {
file, err := yaml.ReadFile(filepath.Join(path, ns, name, "index.yaml"))
if err != nil {
return nil
}
return yamlToInfo(ns, name, file.Root)
}
示例7: LoadConfig
func LoadConfig() {
configPath := flag.String("config", "./config.yml", "config file path")
port := flag.String("port", "default", "port to bind to")
duration := flag.Int64("duration", 0, "duration to operation on")
cpuprofile := flag.Bool("cpuprofile", false, "write cpu profile to file")
flag.Parse()
absolutePath, _ := filepath.Abs(*configPath)
c, err := yaml.ReadFile(absolutePath)
if err != nil {
panic(err)
}
root, _ := c.Get("root")
if *port == "default" {
*port, _ = c.Get("port")
}
numRetentions, _ := c.Count("retentions")
retentions := make([]Retention, numRetentions)
for i := 0; i < numRetentions; i++ {
retention, _ := c.Get("retentions[" + strconv.Itoa(i) + "]")
parts := strings.Split(retention, " ")
d, _ := strconv.ParseInt(parts[0], 0, 64)
n, _ := strconv.ParseInt(parts[1], 0, 64)
retentions[i] = Retention{d, n, d * n}
}
p, _ := c.Get("redis.port")
redisPort, _ := strconv.Atoi(p)
redisHost, _ := c.Get("redis.host")
Config = Configuration{*port, root, retentions, redisHost, redisPort, *duration}
ProfileCPU = *cpuprofile
}
示例8: newDataSourceFromPath
func newDataSourceFromPath(path string) *DataSource {
file, _ := yaml.ReadFile(path)
fName := filepath.Base(path)
extName := filepath.Ext(path)
bName := fName[:len(fName)-len(extName)]
id := bName
name, _ := file.Get("name")
length, _ := file.Count("webhooks")
if length < 0 {
length = 0
}
webhooks := make([]string, length)
for i := 0; i < length; i++ {
it := strconv.Itoa(i)
webhooks[i], _ = file.Get("webhooks[" + it + "]")
}
dataSource := new(DataSource)
dataSource.ID = id
dataSource.Name = name
dataSource.Webhooks = webhooks
dataSource.propagateTimedEvent = make(chan TimedEvent)
dataSource.registerPersistor = make(chan *Persistor)
dataSource.registerConnection = make(chan *connection)
dataSource.unregisterConnection = make(chan *connection)
dataSource.persistors = make(map[*Persistor]bool)
dataSource.connections = make(map[*connection]bool)
return dataSource
}
示例9: main
func main() {
// parse config file
file := "config.yaml"
config, err := yaml.ReadFile(file)
if err != nil {
log.Fatal("Error load config", err)
}
wsaddr, _ = config.Get("ws.address")
zmqaddr, _ = config.Get("zmq.address")
zmqsubject, _ = config.Get("zmq.subject")
// connect to ZMQ broker
responder, _ = zmq.NewSocket(zmq.SUB)
defer responder.Close()
responder.Connect(zmqaddr)
responder.SetSubscribe(zmqsubject)
// start handle ws
http.HandleFunc("/ws", handler)
err = http.ListenAndServe(wsaddr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
示例10: GetSQLConfFromFile
// Attempts to read from the passed in filename and creates a SQLconf as described therin.
// Retruns the DefaultSQLConf if the file cannot be found, or an error
// if one arises during the process of parsing the configuration file.
func GetSQLConfFromFile(filename string) (*SQLConf, error) {
DefaultSQLConf := sqlConfFromEnv()
configPath := path.Join(filename)
_, err := os.Stat(configPath)
if err != nil && os.IsNotExist(err) {
return DefaultSQLConf, nil
} else {
// Defaults to development environment, you can override
// by changing the $GO_ENV variable:
// `$ export GO_ENV=environment` (where environment
// can be "production", "test", "staging", etc.)
// TODO: Potentially find a better solution to handling environments
// Perhaps: https://github.com/adeven/goenv ?
goEnv := os.Getenv("GO_ENV")
if goEnv == "" {
goEnv = "development"
}
config, readYamlErr := yaml.ReadFile(configPath)
if readYamlErr != nil {
return nil, readYamlErr
}
return confFromYamlFile(config, goEnv)
}
return nil, err
}
示例11: main
func main() {
config, err := yaml.ReadFile("conf.yaml")
if err != nil {
fmt.Println(err)
}
fmt.Println(config.Get("path"))
fmt.Println(config.GetBool("enabled"))
}
示例12: GetConfig
// 获取配置信息
func GetConfig(configFile string, env string) (map[string]string, map[string]map[string]string) {
// 装在配置文件对象
file := flag.String("file", configFile, "(Simple) YAML file to read")
// 读取配置文件内容
configHandle, err := yaml.ReadFile(*file)
if err != nil {
log.Fatalf("readfile %q: %s", *file, err)
}
serviceTotal, err := configHandle.Count("services")
mainConfig := make(map[string]string)
serviceConfig := map[string]map[string]string{}
for i := 0; i < serviceTotal; i++ {
serviceConf := make(map[string]string)
// 循环获取Service配置字段值.
for key, defaultVal := range serviceFieldNames {
serviceVal, err := configHandle.Get("services[" + fmt.Sprintf("%d", i) + "]." + key)
if err != nil {
serviceVal = defaultVal
}
if serviceVal == "" && defaultVal != "" {
serviceVal = defaultVal
}
serviceConf[key] = serviceVal
}
serviceConfig[serviceConf["name"]] = serviceConf
}
// 获取Env环境变量.
for key, defaultVal := range configFieldName {
configVal, err := configHandle.Get(key)
if err != nil {
if key == "env" {
mainConfig[key] = env
} else {
mainConfig[key] = defaultVal
}
} else {
if key == "env" {
env = configVal
}
mainConfig[key] = configVal
}
}
log.Printf("current ENV: %s", env)
return mainConfig, serviceConfig
}
示例13: LoadConf
func LoadConf() {
var err error
conf, err = yaml.ReadFile("botconf.yaml")
if err != nil {
log.Panic(err)
}
turingAPI, _ = conf.Get("turingBotKey")
ydTransAPI, _ = conf.Get("yandexTransAPI")
}
示例14: NewDBConf
func NewDBConf(conf, p, env string) (*DBConf, error) {
if !filepath.IsAbs(conf) {
dir, file := filepath.Split(conf)
if dir == "" {
// Path is neither relative nor absolute (just filename)
conf = filepath.Join(p, file)
}
}
f, err := yaml.ReadFile(conf)
if err != nil {
return nil, err
}
drv, err := f.Get(fmt.Sprintf("%s.driver", env))
if err != nil {
return nil, err
}
open, err := f.Get(fmt.Sprintf("%s.open", env))
if err != nil {
return nil, err
}
open = os.ExpandEnv(open)
// Automatically parse postgres urls
if drv == "postgres" {
// Assumption: If we can parse the URL, we should
if parsedURL, err := pq.ParseURL(open); err == nil && parsedURL != "" {
open = parsedURL
}
}
d := newDBDriver(drv, open)
// allow the configuration to override the Import for this driver
if imprt, err := f.Get(fmt.Sprintf("%s.import", env)); err == nil {
d.Import = imprt
}
// allow the configuration to override the Dialect for this driver
if dialect, err := f.Get(fmt.Sprintf("%s.dialect", env)); err == nil {
d.Dialect = dialectByName(dialect)
}
if !d.IsValid() {
return nil, errors.New(fmt.Sprintf("Invalid DBConf: %v", d))
}
return &DBConf{
MigrationsDir: filepath.Join(p, "migrations"),
Env: env,
Driver: d,
}, nil
}
示例15: main
/**
* Main program.
*/
func main() {
log.Print("Starting up concentrator")
var file string = "config.yml"
log.Print("Loading config file: ", file)
config, err := yaml.ReadFile("config.yml")
if err != nil {
log.Fatalf("Error reading config.yml (%q): %s", file, err)
}
// Get the backends config list.
servers, err := yaml.Child(config.Root, "backends")
server_lst, ok := servers.(yaml.List)
if !ok {
log.Fatalf("Could not parse backends list")
return
}
// Load the stats backends.
for i := 0; i < server_lst.Len(); i++ {
node := server_lst.Item(i)
vals := node.(yaml.Map)
for index, element := range vals {
backend_host := fmt.Sprintf("%s", index)
backend_port := fmt.Sprintf("%s", element)
log.Print(fmt.Sprintf("Adding backend %s:%s", backend_host, backend_port))
Backends = append(Backends, fmt.Sprintf("%s:%s", backend_host, backend_port))
}
}
for _, backserver := range Backends {
log.Print(fmt.Sprintf("New server is: %s", backserver))
}
port, err := config.GetInt("port")
host, err := config.Get("host")
log.Print(fmt.Sprintf("Trying to listen on %s:%v", host, port))
relay_method, err := config.Get("relay_method")
log.Print(fmt.Sprintf("We want to relay using %s", relay_method))
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%v", host, port))
if err != nil {
println("Error starting net.Listen: ", err.Error())
os.Exit(1)
}
log.Print("Server started, awaiting connections...")
conns := clientConnections(listener)
for {
go handleConnections(<-conns)
}
}