forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
49 lines (38 loc) · 887 Bytes
/
file.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
package util
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/logrusorgru/aurora"
)
func CreateIfNotExist(file string) (*os.File, error) {
_, err := os.Stat(file)
if !os.IsNotExist(err) {
return nil, fmt.Errorf("%s already exist", file)
}
return os.Create(file)
}
func RemoveIfExist(filename string) error {
if !FileExists(filename) {
return nil
}
return os.Remove(filename)
}
func RemoveOrQuit(filename string) error {
if !FileExists(filename) {
return nil
}
fmt.Printf("%s exists, overwrite it?\nEnter to overwrite or Ctrl-C to cancel...",
aurora.BgRed(aurora.Bold(filename)))
bufio.NewReader(os.Stdin).ReadBytes('\n')
return os.Remove(filename)
}
func FileExists(file string) bool {
_, err := os.Stat(file)
return err == nil
}
func FileNameWithoutExt(file string) string {
return strings.TrimSuffix(file, filepath.Ext(file))
}