-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
109 lines (94 loc) · 1.97 KB
/
cli.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
)
func main() {
args := os.Args
var query string
if len(args) == 1 {
query = PromptSearchSong()
} else {
query = strings.Join(args[1:], " ")
}
raw := Search(query)
songs := ToSongs(raw)
i := PromptDownloadSong(songs)
DownloadSong(songs.Songs[i])
}
func PromptFileOverwrite() bool {
resp := prompt("You have this chart. Do you want to overwrite it? (y/n)")
if resp == "y" {
return true
}
return false
}
func PromptDownloadSong(songs Songs) int {
var data [][]string
for i := range songs.Songs {
song := songs.Songs[i]
index := strconv.Itoa(i+1)
var nrow []string
nrow = append(nrow, index)
nrow = append(nrow, song.Name)
nrow = append(nrow, song.Artist)
nrow = append(nrow, song.Charter)
data = append(data, nrow)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"#", "Name", "Artist", "Charter"})
table.AppendBulk(data)
table.Render()
resp := prompt("Pick a song to download (1-20) (default 1)")
var iresp int
if resp != "" {
var err error
iresp, err = strconv.Atoi(resp)
HandleErr(err)
iresp--
} else {
iresp = 0
}
return iresp
}
func PromptSearchSong() string {
resp := prompt("Enter a search term")
return resp
}
func PromptSongDir() {
resp := prompt("Please enter your songs directory")
err := ioutil.WriteFile("./songsdir", []byte(resp), 0644)
HandleErr(err)
}
func Message(s string) {
fmt.Println(s)
}
func HandleErr(err error) string {
if err != nil {
if _, ok := err.(*os.PathError); ok {
if PromptFileOverwrite() {
return "file_exists_overwrite"
} else {
return "file_exists_keep"
}
} else if os.IsNotExist(err) {
PromptSongDir()
} else {
panic(err)
}
}
return ""
}
func prompt(s string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s: ", s)
resp, err := reader.ReadString('\n')
HandleErr(err)
clean := strings.TrimSpace(resp)
return clean
}