add: [config] specify config line by line

master v0.1.7
Jean-Louis Huynen 2020-10-21 17:01:34 +02:00
parent b333ed9208
commit c84446ba7f
1 changed files with 24 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package config
import (
"bufio"
"bytes"
"fmt"
"io"
@ -80,6 +81,7 @@ func validPort(port string) bool {
}
// ReadConfigFile takes two argument: folder and fileName.
// Create if not exist
// It reads its content, trims\n and \r, and return []byte
// All errors are Fatal.
func ReadConfigFile(folder string, fileName string) []byte {
@ -103,3 +105,25 @@ func ReadConfigFile(folder string, fileName string) []byte {
r := bytes.TrimSuffix(data[:count], []byte("\n"))
return bytes.TrimSuffix(r, []byte("\r"))
}
// ReadConfigFileLines takes two argument: folder and fileName.
// It reads its content line by line, trims\n and \r,
// and return [][]byte
// All errors are Fatal.
func ReadConfigFileLines(folder string, fileName string) [][]byte {
res := [][]byte{}
f, err := os.Open(folder+"/"+fileName)
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
res = append(res, []byte(scanner.Text()))
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return res
}