-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
77 lines (65 loc) · 1.64 KB
/
main.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
package main
import (
"fmt"
"net/url"
"os"
"os/exec"
"strings"
player_path "github.com/tgdrive/player-protocol/pkg/path"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: program <protocol://link>")
os.Exit(1)
}
arg := os.Args[1]
var player, protocol string
if strings.HasPrefix(arg, "vlc://") {
player = "vlc"
protocol = "vlc://"
} else if isWindows() && strings.HasPrefix(arg, "potplayer://") {
player = "potplayer"
protocol = "potplayer://"
} else {
fmt.Println("Invalid protocol. Use vlc:// or potplayer:// (PotPlayer is only supported on Windows)")
os.Exit(1)
}
link := strings.TrimPrefix(arg, protocol)
if !strings.HasPrefix(link, "http://") && !strings.HasPrefix(link, "https://") && !strings.HasPrefix(link, "file://") {
fmt.Println("Invalid link. Must start with http://, https://, or file://")
os.Exit(1)
}
isFileProtocol := strings.HasPrefix(link, "file://")
if isFileProtocol {
link = strings.TrimPrefix(link, "file://")
link = strings.TrimPrefix(link, "/")
if isWindows() {
link = strings.Replace(link, "/", "\\", -1)
}
link, _ = url.QueryUnescape(link)
}
playerPath, err := player_path.FindPlayerPath(player)
if err != nil {
fmt.Printf("Error finding %s path: %v\n", player, err)
os.Exit(1)
}
var cmd *exec.Cmd
switch player {
case "vlc":
if isFileProtocol {
cmd = exec.Command(playerPath, link)
} else {
cmd = exec.Command(playerPath, "--open", link)
}
case "potplayer":
cmd = exec.Command(playerPath, link)
}
err = cmd.Start()
if err != nil {
fmt.Println("Error starting player:", err)
os.Exit(1)
}
}
func isWindows() bool {
return os.PathSeparator == '\\'
}