Blame view

file_util.go 2.2 KB
18fcd6d6b   Marko Tikvić   merged with util ...
1
  package webutility
cacf57bd4   Marko Tikvić   merging with /uti...
2
3
4
  
  import (
  	"bytes"
6a5c6931e   Marko Tikvić   added database ma...
5
  	"fmt"
cacf57bd4   Marko Tikvić   merging with /uti...
6
7
8
9
10
  	"io"
  	"io/ioutil"
  	"os"
  	"strings"
  )
46b2215eb   Marko Tikvić   lintfix
11
  // ReadFileContent ...
cacf57bd4   Marko Tikvić   merging with /uti...
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
67
  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
68

8c6dd0499   Marko Tikvić   GetContent
69
70
71
  func WriteFile(path string, content []byte) error {
  	return ioutil.WriteFile(path, content, 0644) // drw-r--r--
  }
ec4067ed8   Marko Tikvić   accessories
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  
  func ListDir(path string) (fnames []string, err error) {
  	finfo, err := ioutil.ReadDir(path)
  	if err != nil {
  		return nil, err
  	}
  
  	for _, f := range finfo {
  		fnames = append(fnames, f.Name())
  	}
  
  	return fnames, nil
  }
  
  func WorkingDir() string {
6a5c6931e   Marko Tikvić   added database ma...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  	path, err := os.Getwd()
  	if err != nil {
  		fmt.Printf("couldn't get working directory: %s
  ", err.Error())
  	}
  	return path
  }
  
  func FileExtension(path string) string {
  	parts := strings.Split(path, ".") // because name can contain dots
  	if len(parts) < 2 {
  		return ""
  	}
  	return "." + parts[len(parts)-1]
  }
  
  func DeleteFile(path string) error {
  	return os.Remove(path)
  }
  
  // DirectoryFromPath ...
  func DirectoryFromPath(path string) (dir string) {
  	parts := strings.Split(path, "/")
  	if len(parts) == 1 {
  		return ""
  	}
  
  	dir = parts[0]
  	for _, p := range parts[1 : len(parts)-1] {
  		dir += "/" + p
  	}
ec4067ed8   Marko Tikvić   accessories
118
119
  	return dir
  }
6a5c6931e   Marko Tikvić   added database ma...
120
121
122
123
124
125
126
127
128
129
130
131
  
  // FileExists ...
  func FileExists(path string) bool {
  	temp, err := os.Open(path)
  	defer temp.Close()
  
  	if err != nil {
  		return false
  	}
  
  	return true
  }