forked from fiam/max7456tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
41 lines (38 loc) · 791 Bytes
/
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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
var (
forceFlag = false
)
func openOutputFile(filename string) (*os.File, error) {
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
if !forceFlag {
flags |= os.O_EXCL
}
f, err := os.OpenFile(filename, flags, 0644)
if err != nil {
if os.IsExist(err) && !forceFlag {
for {
fmt.Printf("File %v already exists, would you like to overwrite it? [y/N/a]: ", filename)
r := bufio.NewReader(os.Stdin)
line, _ := r.ReadString('\n')
switch strings.ToLower(strings.TrimSpace(line)) {
case "a":
forceFlag = true
fallthrough
case "y":
flags &= ^os.O_EXCL
return os.OpenFile(filename, flags, 0644)
case "n", "":
return nil, err
}
}
}
return nil, err
}
return f, nil
}