forked from aerospike/aerospike-kubernetes-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
118 lines (104 loc) · 2.74 KB
/
utils.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
74
75
76
77
78
79
80
81
82
83
84
85
86
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
118
package main
import (
"bufio"
"fmt"
"io"
"os"
)
// copyFile is safety wrapper around copyFileData.
// If the src file doesn't exist, return success.
// If src and dst files exist, and are the same, then return success.
// Else, copy the file contents from src to dst.
// Note that, it cannot copy non-regular files (e.g., directories, symlinks, devices, etc.)
func copyFile(src, dst string) (err error) {
// Stat source file
srcFileInfo, err := os.Stat(src)
if err != nil {
// If the file doesn't exist, return success.
if os.IsNotExist(err) {
return nil
}
return
}
// Check if the source file is regular
if !srcFileInfo.Mode().IsRegular() {
return fmt.Errorf("Cannot copy! Source is a non-regular file %s (%q)", srcFileInfo.Name(), srcFileInfo.Mode().String())
}
// Stat destination file
destFileInfo, err := os.Stat(dst)
if err != nil {
if !os.IsNotExist(err) {
return
}
} else {
// Check if the destination file is regular
if !(destFileInfo.Mode().IsRegular()) {
return fmt.Errorf("Cannot copy! Destination is a non-regular file %s (%q)", destFileInfo.Name(), destFileInfo.Mode().String())
}
if os.SameFile(srcFileInfo, destFileInfo) {
return
}
}
// Copy the source file to the destination
err = copyFileData(src, dst)
if err != nil {
return fmt.Errorf("Error copying! %v", err)
}
return
}
// copyFileData copies the contents of the source file to the destination file.
// The file will be created if it doesn't exist. Replace file contents on destination if the file already exists.
func copyFileData(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return fmt.Errorf("Cannot open source file %s: %v", src, err)
}
defer func() {
cerr := in.Close()
if err == nil {
err = cerr
}
}()
out, err := os.Create(dst)
if err != nil {
return fmt.Errorf("Cannot create destination file %s: %v", dst, err)
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
// Copy the file from in(src) to out(dst)
_, err = io.Copy(out, in)
if err != nil {
return fmt.Errorf("Cannot copy file: %v", err)
}
// Flush to storage
err = out.Sync()
if err != nil {
return fmt.Errorf("Cannot flush data to disk: %v", err)
}
return
}
// fileToLines opens a file and returns all the lines in the file
func fileToLines(filePath string) ([]string, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
return linesFromReader(f)
}
// linesFromReader returns all the lines from open file reader
func linesFromReader(r io.Reader) ([]string, error) {
var lines []string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}