Browse Source

add file tool

Paul 5 years ago
parent
commit
585e57994f
1 changed files with 53 additions and 0 deletions
  1. 53
    0
      utils/file.go

+ 53
- 0
utils/file.go View File

@@ -0,0 +1,53 @@
1
+package utils
2
+
3
+import (
4
+	"io"
5
+	"os"
6
+	"os/exec"
7
+	"strconv"
8
+)
9
+
10
+// FileExists check file is exists
11
+func FileExists(path string) bool {
12
+	_, err := os.Stat(path)
13
+	if err != nil {
14
+		if os.IsExist(err) {
15
+			return true
16
+		}
17
+		return false
18
+	}
19
+	return true
20
+}
21
+
22
+// PidExists checkou process id is exists
23
+func PidExists(pid int) bool {
24
+	out, _ := exec.Command("kill", "-s", "0", strconv.Itoa(pid)).CombinedOutput()
25
+	if string(out) == "" {
26
+		return true // pid exist
27
+	}
28
+	return false
29
+}
30
+
31
+// CopyFile copy file from src to dest
32
+func CopyFile(srcFile, destFile string) error {
33
+	file, err := os.Open(srcFile)
34
+	if err != nil {
35
+		return err
36
+	}
37
+
38
+	defer file.Close()
39
+
40
+	dest, err := os.Create(destFile)
41
+	if err != nil {
42
+		return err
43
+	}
44
+
45
+	defer dest.Close()
46
+
47
+	_, err = io.Copy(dest, file)
48
+	if err != nil {
49
+		return err
50
+	}
51
+
52
+	return nil
53
+}