-
Notifications
You must be signed in to change notification settings - Fork 109
/
io.go
85 lines (73 loc) · 1.49 KB
/
io.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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package utils
import (
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"os"
)
// DirMissingOrEmpty 路径是否为空
func DirMissingOrEmpty(path string) (bool, error) {
dirExists, err := dirExists(path)
if err != nil {
return false, err
}
if !dirExists {
return true, nil
}
dirEmpty, err := dirEmpty(path)
if err != nil {
return false, err
}
if dirEmpty {
return true, nil
}
return false, nil
}
// DirExists 目录是否存在
func dirExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// DirEmpty 目录是否为空
func dirEmpty(path string) (bool, error) {
f, err := os.Open(path)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdir(1)
if err == io.EOF {
return true, nil
}
return false, err
}
// ReadFile 读取文件
func ReadFile(file string) ([]byte, error) {
fileCont, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("Could not read file %s, err %s", file, err)
}
return fileCont, nil
}
// ReadPemFile 读取pem文件
func ReadPemFile(file string) ([]byte, error) {
bytes, err := ReadFile(file)
if err != nil {
return nil, err
}
b, _ := pem.Decode(bytes)
if b == nil {
return nil, fmt.Errorf("No pem content for file %s", file)
}
return bytes, nil
}