-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
86 lines (71 loc) · 1.75 KB
/
fs.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
86
package main
import (
"encoding/csv"
"io"
"log"
"os"
"os/exec"
)
const (
filename = "playlists.temp"
defaultEditor = "vim"
)
// WriteToFile writes the playlists to a CSV for user editing
func WriteToFile(playlists []Playlist) {
log.Println(len(playlists))
file, err := os.Create(filename)
if err != nil {
log.Fatal("Unable to create temp file: ", err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
writer.Write([]string{"-- Delete the playlists you wish to retain. Everything else will be killed! --", ""})
for _, playlist := range playlists {
err := writer.Write([]string{playlist.Name, playlist.ID})
if err != nil {
log.Fatal("Unable to write to temp file: ", err)
}
}
}
// OpenInEditor opens the temp file in an editor for user editing
func OpenInEditor() {
editor := os.Getenv("EDITOR")
if editor == "" {
editor = defaultEditor
}
// Get the full executable path for the editor.
executable, err := exec.LookPath(editor)
if err != nil {
log.Fatal("Unable to locate editor executable: ", err)
}
cmd := exec.Command(executable, filename)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Fatal("Error occurred in opening file for edit: ", err)
}
}
// ReadIDsToDelete reads the edited temp file and extracts the ID strings
func ReadIDsToDelete() []string {
toDelete := []string{}
file, err := os.Open(filename)
if err != nil {
log.Fatal("Unable to read temp file: ", err)
}
defer file.Close()
reader := csv.NewReader(file)
for i := 0; ; i++ {
line, err := reader.Read()
if err == io.EOF {
break
}
if err != nil && i != 0 {
log.Fatal("Error reading CSV: ", err)
}
toDelete = append(toDelete, line[1])
}
return toDelete
}