-
Notifications
You must be signed in to change notification settings - Fork 30
/
fileManager.go
73 lines (61 loc) · 1.63 KB
/
fileManager.go
1
2
3
4
5
6
7
8
9
10
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
67
68
69
70
71
72
73
package main
import (
"bufio"
"os"
"path/filepath"
)
// CreateDirectory creates directory if not exists.
func CreateDirectory() {
if _, err := os.Stat(Path); os.IsNotExist(err) {
err := os.MkdirAll(Path, os.ModePerm)
if err != nil {
Fatalf("Error creating directory: %v", err)
}
}
}
// WriteToFile creates a file ( if not exists ), append the content and then close the file
func WriteToFile(filename string, message string) error {
// open files r, w mode
file, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return err
}
// Close the file
defer file.Close()
// Append the message or content to be written
if _, err = file.WriteString(message); err != nil {
return err
}
return nil
}
// ListFile lists all the backup sql file to recreate the constraints
func ListFile(dir, suffix string) ([]string, error) {
return filepath.Glob(filepath.Join(dir, suffix))
}
// ReadFile reads the file content and send it across
func ReadFile(filename string) ([]string, error) {
var contentSaver []string
// Open th file
file, err := os.Open(filename)
if err != nil {
Fatalf("Error opening the file: %v", err)
}
defer file.Close()
// Read the file line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
contentSaver = append(contentSaver, scanner.Text())
}
if err := scanner.Err(); err != nil {
return contentSaver, err
}
return contentSaver, nil
}
// CurrentDir provides the current working directory
func CurrentDir() (cwd string) {
cwd, err := os.Getwd()
if err != nil {
Fatalf("Error when trying to get the current directory, err: %v", err)
}
return cwd
}