/
play.go
76 lines (64 loc) · 1.27 KB
/
play.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
package commands
import (
"os"
"os/exec"
"os/signal"
"runtime"
"sync"
"syscall"
"github.com/Sirupsen/logrus"
)
func runPlay() {
if Link == "" {
logrus.Error("link is empty")
return
}
glod := getGlod(Link)
if glod == nil {
logrus.Error("source not yet supported")
return
}
logrus.Println("Retrieving metadata ...")
listResponse, err := glod.GetDirectLink(Link)
if err != nil {
logrus.WithError(err).Error("failed to get direct link")
return
}
objs, err := downloadWithoutProgressBar(Link, listResponse, PlayDir)
if err != nil {
logrus.WithError(err).Error("failed to download")
return
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
defer cleanUp()
play(objs)
}()
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
select {
case <-c:
cleanUp()
}
wg.Wait()
logrus.Println("Finish.")
}
func play(objs []ObjectResponse) {
if runtime.GOOS != "darwin" {
logrus.Info("sorry, this functionality only supports MacOS currently")
return
}
logrus.Println("Playing...")
for _, v := range objs {
cmd := exec.Command("afplay", v.Name)
cmd.Start()
cmd.Wait()
}
}
func cleanUp() {
if err := os.RemoveAll(PlayDir); err != nil {
logrus.WithError(err).Error("failed to remove dir")
}
}