Commit 6b6cbc5f61

6b6cbc5f618b7c2560fec4dfb50df5e1eaf17549

parent: cf8f816242

Unsigned

Wraient <wraient.911@gmail.com> · 2024-10-23 14:35 UTC

Feat: Added Windows Support

Layout: unified · split

buildwindows added +4
@@ -0,0 +1,4 @@
1@echo off
2set GOOS=windows
3set GOARCH=amd64
4go build -o curd.exe -ldflags="-s -w" -trimpath cmd\curd\main.go
\ No newline at end of file
cmd/curd/main.go +9 −3
@@ -6,6 +6,7 @@ import (
66 "os"
77 "path/filepath"
88 "strconv"
9 "runtime"
910 "sync"
1011 "time"
1112 "github.com/wraient/curd/internal"
@@ -22,7 +23,14 @@ func main() {
2223 var anime internal.Anime
2324 var user internal.User
2425
25 configFilePath := os.ExpandEnv("$HOME/.config/curd/curd.conf")
26 var homeDir string
27 if runtime.GOOS == "windows" {
28 homeDir = os.Getenv("USERPROFILE")
29 } else {
30 homeDir = os.Getenv("HOME")
31 }
32
33 configFilePath := filepath.Join(homeDir, ".config", "curd", "curd.conf")
2634
2735 // load curd userCurdConfig
2836 userCurdConfig, err := internal.LoadConfig(configFilePath)
@@ -67,8 +75,6 @@ func main() {
6775
6876 anime.Ep.ContinueLast = *continueLast
6977
70
71
7278 if *updateScript{
7379 repo := "wraient/curd"
7480 fileName := "curd"
go.mod +1 −2
@@ -3,6 +3,7 @@ module github.com/wraient/curd
33go 1.23.2
44
55require (
6 github.com/Microsoft/go-winio v0.6.2
67 github.com/charmbracelet/bubbletea v1.1.1
78 github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2
89)
@@ -13,7 +14,6 @@ require (
1314 github.com/charmbracelet/x/ansi v0.2.3 // indirect
1415 github.com/charmbracelet/x/term v0.2.0 // indirect
1516 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
16 github.com/gorilla/websocket v1.4.2 // indirect
1717 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
1818 github.com/mattn/go-isatty v0.0.20 // indirect
1919 github.com/mattn/go-localereader v0.0.1 // indirect
@@ -22,7 +22,6 @@ require (
2222 github.com/muesli/cancelreader v0.2.2 // indirect
2323 github.com/muesli/termenv v0.15.2 // indirect
2424 github.com/rivo/uniseg v0.4.7 // indirect
25 golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
2625 golang.org/x/sync v0.8.0 // indirect
2726 golang.org/x/sys v0.24.0 // indirect
2827 golang.org/x/text v0.14.0 // indirect
go.sum +2 −11
@@ -1,7 +1,7 @@
1github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
2github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
13github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
24github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
3github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
4github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
55github.com/charmbracelet/bubbletea v1.1.1 h1:KJ2/DnmpfqFtDNVTvYZ6zpPFL9iRCRr0qqKOCvppbPY=
66github.com/charmbracelet/bubbletea v1.1.1/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4=
77github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=
@@ -12,8 +12,6 @@ github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4h
1212github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=
1313github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
1414github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
15github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
16github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
1715github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2 h1:9qOViOQGFIP5ar+2NorfAIsfuADEKXtklySC0zNnYf4=
1816github.com/hugolgst/rich-go v0.0.0-20240715122152-74618cc1ace2/go.mod h1:nGaW7CGfNZnhtiFxMpc4OZdqIexGXjUlBnlmpZmjEKA=
1917github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
@@ -33,20 +31,13 @@ github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1n
3331github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
3432github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
3533github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
36golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
37golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
38golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
3934golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
4035golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
41golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4236golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4337golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4438golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
4539golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
46golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
47golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
4840golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
4941golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
50golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
5142gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU=
5243gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c=
internal/anilist.go +3 −2
@@ -8,6 +8,7 @@ import (
88 "io/ioutil"
99 "net/http"
1010 "os"
11 "path/filepath"
1112 "strconv"
1213 "strings"
1314)
@@ -19,7 +20,7 @@ func FindKeyByValue(m map[string]string, value string) (string, error) {
1920 return key, nil // Return the key and true if the value is found
2021 }
2122 }
22 return "", fmt.Errorf("No key with value %v", value) // Return empty string and false if the value is not found
23 return "", fmt.Errorf("no key with value %v", value) // Return empty string and false if the value is not found
2324}
2425
2526// GetAnimeMap takes an AnimeList and returns a map with media.id as key and media.title.english as value.
@@ -272,7 +273,7 @@ func GetUserData(token string, userID int) (map[string]interface{}, error) {
272273
273274// Function to load a JSON file
274275func LoadJSONFile(filePath string) (map[string]interface{}, error) {
275 data, err := os.ReadFile(filePath)
276 data, err := os.ReadFile(filepath.Clean(filePath))
276277 if err != nil {
277278 return nil, fmt.Errorf("failed to read file: %w", err)
278279 }
internal/curd.go +62 −42
@@ -117,57 +117,65 @@ func ExitCurd() {
117117}
118118
119119func UpdateCurd(repo, fileName string) error {
120 // Get the path of the currently running executable
121 executablePath, err := os.Executable()
122 if err != nil {
123 return fmt.Errorf("unable to find current executable: %v", err)
124 }
120 // Get the path of the currently running executable
121 executablePath, err := os.Executable()
122 if err != nil {
123 return fmt.Errorf("unable to find current executable: %v", err)
124 }
125125
126 // GitHub release URL for curd
127 url := fmt.Sprintf("https://github.com/%s/releases/latest/download/%s", repo, fileName)
126 // Adjust file name for Windows
127 if runtime.GOOS == "windows" {
128 fileName += ".exe"
129 }
128130
129 // Temporary path for the downloaded curd.py
130 tmpPath := executablePath + ".tmp"
131 // GitHub release URL for curd
132 url := fmt.Sprintf("https://github.com/%s/releases/latest/download/%s", repo, fileName)
131133
132 // Download the curd.py file
133 resp, err := http.Get(url)
134 if err != nil {
135 return fmt.Errorf("failed to download file: %v", err)
136 }
137 defer resp.Body.Close()
134 // Temporary path for the downloaded curd executable
135 tmpPath := executablePath + ".tmp"
138136
139 // Check if the download was successful
140 if resp.StatusCode != http.StatusOK {
141 return fmt.Errorf("failed to download file: received status code %d", resp.StatusCode)
142 }
137 // Download the curd executable
138 resp, err := http.Get(url)
139 if err != nil {
140 return fmt.Errorf("failed to download file: %v", err)
141 }
142 defer resp.Body.Close()
143143
144 // Create a new temporary file
145 out, err := os.Create(tmpPath)
146 if err != nil {
147 return fmt.Errorf("failed to create temporary file: %v", err)
148 }
149 defer out.Close()
144 // Check if the download was successful
145 if resp.StatusCode != http.StatusOK {
146 return fmt.Errorf("failed to download file: received status code %d", resp.StatusCode)
147 }
150148
151 // Write the downloaded content to the temporary file
152 _, err = io.Copy(out, resp.Body)
153 if err != nil {
154 return fmt.Errorf("failed to save downloaded file: %v", err)
155 }
149 // Create a new temporary file
150 out, err := os.Create(tmpPath)
151 if err != nil {
152 return fmt.Errorf("failed to create temporary file: %v", err)
153 }
154 defer out.Close()
155
156 // Write the downloaded content to the temporary file
157 _, err = io.Copy(out, resp.Body)
158 if err != nil {
159 return fmt.Errorf("failed to save downloaded file: %v", err)
160 }
156161
157 // Close and rename the temporary file to replace the current executable
158 out.Close()
162 // Close and rename the temporary file to replace the current executable
163 out.Close()
159164
160 // Replace the current executable with the downloaded curd
161 if err := os.Rename(tmpPath, executablePath); err != nil {
162 return fmt.Errorf("failed to replace the current executable: %v", err)
163 }
164 fmt.Println("Downloaded curd executable to", executablePath)
165 // Ensure the new file has executable permissions
166 if err := os.Chmod(executablePath, 0755); err != nil {
167 return fmt.Errorf("failed to set permissions on the new file: %v", err)
168 }
165 // Replace the current executable with the downloaded curd
166 if err := os.Rename(tmpPath, executablePath); err != nil {
167 return fmt.Errorf("failed to replace the current executable: %v", err)
168 }
169 fmt.Println("Downloaded curd executable to", executablePath)
169170
170 return nil
171 if runtime.GOOS != "windows" {
172 // Ensure the new file has executable permissions
173 if err := os.Chmod(executablePath, 0755); err != nil {
174 return fmt.Errorf("failed to set permissions on the new file: %v", err)
175 }
176 }
177
178 return nil
171179}
172180
173181func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime, logFile string) {
@@ -207,7 +215,15 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
207215
208216 // Get user id, username and Anime list
209217 user.Id, user.Username, err = GetAnilistUserID(user.Token)
218 if err != nil {
219 Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile)
220 ExitCurd()
221 }
210222 anilistUserData, err := GetUserData(user.Token, user.Id)
223 if err != nil {
224 Log(fmt.Sprintf("Failed to get user data: %v", err), logFile)
225 ExitCurd()
226 }
211227 user.AnimeList = ParseAnimeList(anilistUserData)
212228 animeListMap := GetAnimeMap(user.AnimeList)
213229 var anilistSelectedOption SelectionOption
@@ -284,6 +300,10 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
284300
285301 // find anime in animeList
286302 anime.AllanimeId, err = FindKeyByValue(animeList, fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes))
303 if err != nil {
304 Log(fmt.Sprintf("Failed to find anime in animeList: %v", err), logFile)
305 ExitCurd()
306 }
287307
288308 // If unable to get Allanime id automatically get manually
289309 if anime.AllanimeId == "" {
internal/player.go +62 −11
@@ -6,11 +6,27 @@ import (
66 "encoding/json"
77 "fmt"
88 "net"
9 "os"
910 "os/exec"
11 "path/filepath"
12 "runtime"
13 "github.com/Microsoft/go-winio"
1014)
1115var logFile = "debug.log"
1216
17func getMPVPath() (string, error) {
18 exePath, err := os.Executable()
19 if err != nil {
20 return "", err
21 }
22 exeDir := filepath.Dir(exePath)
23 mpvPath := filepath.Join(exeDir, "bin", "mpv.exe") // Adjust the relative path
24 return mpvPath, nil
25}
26
1327func StartVideo(link string, args []string) (string, error) {
28 var command *exec.Cmd
29
1430 // Generate a random number for the socket path
1531 randomBytes := make([]byte, 4)
1632 _, err := rand.Read(randomBytes)
@@ -18,22 +34,46 @@ func StartVideo(link string, args []string) (string, error) {
1834 Log("Failed to generate random number", logFile)
1935 return "", fmt.Errorf("failed to generate random number: %w", err)
2036 }
37
2138 randomNumber := fmt.Sprintf("%x", randomBytes)
2239
2340 // Create the mpv socket path with the random number
24 mpvSocketPath := fmt.Sprintf("/tmp/curd/curd_mpvsocket_%s", randomNumber)
25 argsStr := ""
26 if len(args) > 0 {
27 argsStr = " " + joinArgs(args)
41 var mpvSocketPath string
42 if runtime.GOOS == "windows" {
43 mpvSocketPath = fmt.Sprintf(`\\.\pipe\curd_mpvsocket_%s`, randomNumber)
44 } else {
45 mpvSocketPath = fmt.Sprintf("/tmp/curd/curd_mpvsocket_%s", randomNumber)
2846 }
2947
30 // Build the complete command string
31 command := fmt.Sprintf("mpv%s --no-terminal --really-quiet --input-ipc-server=%s %s", argsStr, mpvSocketPath, link)
48 // Prepare arguments for mpv
49 var mpvArgs []string
50 mpvArgs = append(mpvArgs, "--no-terminal", "--really-quiet", fmt.Sprintf("--input-ipc-server=%s", mpvSocketPath), link)
51 // Add any additional arguments passed
52 if len(args) > 0 {
53 mpvArgs = append(mpvArgs, args...) }
54
55 if runtime.GOOS == "windows" {
56 // Get the path to mpv.exe for Windows
57 mpvPath, err := getMPVPath()
58 if err != nil {
59 fmt.Println("Error: Failed to get MPV path")
60 Log("Failed to get mpv path.", logFile)
61 return "", err
62 }
3263
33 // Execute the command using exec
34 cmd := exec.Command("bash", "-c", command)
35 cmd.Start()
64 // Create command for Windows
65 command = exec.Command(mpvPath, mpvArgs...)
66 } else {
67 // Create command for Unix-like systems
68 command = exec.Command("mpv", mpvArgs...)
69 }
3670
71 // Start the mpv process
72 err = command.Start()
73 if err != nil {
74 fmt.Println("Error: Failed to start mpv process")
75 return "", fmt.Errorf("failed to start mpv: %w", err)
76 }
3777 return mpvSocketPath, nil
3878}
3979
@@ -50,7 +90,15 @@ func joinArgs(args []string) string {
5090}
5191
5292func MPVSendCommand(ipcSocketPath string, command []interface{}) (interface{}, error) {
53 conn, err := net.Dial("unix", ipcSocketPath)
93 var conn net.Conn
94 var err error
95
96 if runtime.GOOS == "windows" {
97 // Use named pipe for Windows
98 conn, err = winio.DialPipe(ipcSocketPath, nil)
99 } else {
100 conn, err = net.Dial("unix", ipcSocketPath)
101 }
54102 if err != nil {
55103 return nil, err
56104 }
@@ -64,7 +112,10 @@ func MPVSendCommand(ipcSocketPath string, command []interface{}) (interface{}, e
64112 }
65113
66114 // Send the command
67 conn.Write(append(commandStr, '\n'))
115 _, err = conn.Write(append(commandStr, '\n'))
116 if err != nil {
117 return nil, err
118 }
68119
69120 // Receive the response
70121 buf := make([]byte, 4096)