cleanup errors

This commit is contained in:
Balakrishnan Balasubramanian 2022-06-24 23:43:38 -04:00
parent 7784e58bae
commit 33fe018232

29
main.go
View File

@ -52,7 +52,8 @@ func parse() {
} }
func main() { func main() {
fmt.Println("vim-go") log.Print("Youtube UI")
log.SetFlags(log.Flags() | log.Lshortfile)
parse() parse()
tmpl := template.New("page") tmpl := template.New("page")
tmpl = tmpl.Funcs(map[string]any{ tmpl = tmpl.Funcs(map[string]any{
@ -60,11 +61,11 @@ func main() {
}) })
tmpl, err := tmpl.Parse(page) tmpl, err := tmpl.Parse(page)
if err != nil { if err != nil {
panic(err) log.Panic(err)
} }
d, err = db.Load(dbPath) d, err = db.Load(dbPath)
if err != nil { if err != nil {
panic(err) log.Panic(err)
} }
defer d.Save() defer d.Save()
seen := map[string]struct{}{} seen := map[string]struct{}{}
@ -77,17 +78,16 @@ func main() {
if r.Method == http.MethodGet { if r.Method == http.MethodGet {
d.Run(func(d *db.Jdb) { d.Run(func(d *db.Jdb) {
if err := tmpl.Execute(w, d); err != nil { if err := tmpl.Execute(w, d); err != nil {
panic(err) log.Panic(err)
} }
}) })
return return
} }
if err := r.ParseForm(); err != nil {
panic(err)
}
yturl := r.PostFormValue("youtube_url") yturl := r.PostFormValue("youtube_url")
if yturl == "" { if yturl == "" {
panic("yturl empty") log.Printf("yturl empty, postform:%v\n", r.PostForm)
w.WriteHeader(http.StatusInternalServerError)
return
} }
if _, ok := seen[yturl]; !ok { if _, ok := seen[yturl]; !ok {
seen[yturl] = struct{}{} seen[yturl] = struct{}{}
@ -104,18 +104,21 @@ func main() {
} }
http.Redirect(w, r, "/", http.StatusSeeOther) http.Redirect(w, r, "/", http.StatusSeeOther)
}) })
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil)) log.Panic(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
} }
func getTitle(id int, yturl string) { func getTitle(id int, yturl string) {
args := append(ytdlCmd, "--get-title", yturl) args := append(ytdlCmd, "--get-title", yturl)
cmd := exec.Command(args[0], args[1:]...) cmd := exec.Command(args[0], args[1:]...)
op, err := cmd.Output() var title string
if err != nil { if op, err := cmd.Output(); err != nil {
panic(err) log.Printf("command failed, cmd: %v, err: %v", cmd, err)
title = "ERROR"
} else {
title = string(op)
} }
d.Update(id, true, func(i *db.Item) { d.Update(id, true, func(i *db.Item) {
i.Title = string(op) i.Title = title
}) })
} }