-
Notifications
You must be signed in to change notification settings - Fork 1
/
write.go
67 lines (62 loc) · 1.23 KB
/
write.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
package csvi
import (
"flag"
"io"
"os"
"path/filepath"
"github.com/hymkor/csvi/uncsv"
)
var overWritten = map[string]struct{}{}
func dump(app *_Application, w io.Writer) {
cursor := app.Front()
app.Config.Mode.DumpBy(
func() *uncsv.Row {
if cursor == nil {
return nil
}
row := cursor.Row
cursor = cursor.Next()
return row
}, w)
}
func cmdWrite(app *_Application) error {
fname := "-"
var err error
if args := flag.Args(); len(args) >= 1 {
fname, err = filepath.Abs(args[0])
if err != nil {
return err
}
}
fname, err = app.GetFilename(app, "write to>", fname)
if err != nil {
return nil
}
if fname == "-" {
dump(app, os.Stdout)
return nil
}
fd, err := os.OpenFile(fname, os.O_WRONLY|os.O_EXCL|os.O_CREATE, 0666)
if os.IsExist(err) {
if _, ok := overWritten[fname]; ok {
os.Remove(fname)
} else {
if !app.YesNo("Overwrite as \"" + fname + "\" [y/n] ?") {
return nil
}
backupName := fname + "~"
os.Remove(backupName)
os.Rename(fname, backupName)
overWritten[fname] = struct{}{}
}
fd, err = os.OpenFile(fname, os.O_WRONLY|os.O_EXCL|os.O_CREATE, 0666)
}
if err != nil {
return err
}
dump(app, fd)
if err := fd.Close(); err != nil {
return err
}
return nil
}