Blame view

file_util.go 1.09 KB
18fcd6d6b   Marko Tikvić   merged with util ...
1
  package webutility
cacf57bd4   Marko Tikvić   merging with /uti...
2
3
4
5
6
7
8
9
  
  import (
  	"bytes"
  	"io"
  	"io/ioutil"
  	"os"
  	"strings"
  )
46b2215eb   Marko Tikvić   lintfix
10
  // ReadFileContent ...
cacf57bd4   Marko Tikvić   merging with /uti...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  func ReadFileContent(path string) ([]byte, error) {
  	f, err := os.Open(path)
  	if err != nil {
  		return nil, err
  	}
  	defer f.Close()
  
  	buf := &bytes.Buffer{}
  	if _, err = io.Copy(buf, f); err != nil {
  		return nil, err
  	}
  
  	return buf.Bytes(), nil
  }
  
  // ReadFileLines ...
  func ReadFileLines(path string) ([]string, error) {
  	f, err := os.Open(path)
  	if err != nil {
  		return nil, err
  	}
  	defer f.Close()
  
  	var s strings.Builder
  
  	if _, err = io.Copy(&s, f); err != nil {
  		return nil, err
  	}
  
  	lines := strings.Split(s.String(), "
  ")
  	for i := range lines {
  		lines[i] = strings.TrimRight(lines[i], "\r
  ")
  	}
  
  	return lines, nil
  }
  
  // LinesToFile ...
  func LinesToFile(path string, lines []string) error {
  	content := ""
  	for _, l := range lines {
  		content += l + "
  "
  	}
  
  	return ioutil.WriteFile(path, []byte(content), 0644) // drw-r--r--
  }
  
  // InsertLine ...
  func InsertLine(lines *[]string, pos int64, l string) {
  	tail := append([]string{l}, (*lines)[pos:]...)
  
  	*lines = append((*lines)[:pos], tail...)
  }
46b2215eb   Marko Tikvić   lintfix
67