From c84446ba7f567a0c107b4202b6e7406760b00c16 Mon Sep 17 00:00:00 2001 From: Jean-Louis Huynen Date: Wed, 21 Oct 2020 17:01:34 +0200 Subject: [PATCH] add: [config] specify config line by line --- config/config.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/config/config.go b/config/config.go index 98718b7..ad4a862 100644 --- a/config/config.go +++ b/config/config.go @@ -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 +}