Commit 51884195c8

51884195c80750f2974acf2bb03f9fa5a6b70b49

parent: 50cc09675a

Unsigned

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

Add: Function to update script

Layout: unified · split

cmd/curd/main.go +14
@@ -50,6 +50,7 @@ func main() {
5050 flag.BoolVar(&userCurdConfig.SaveMpvSpeed, "save-mpv-speed", userCurdConfig.SaveMpvSpeed, "Save MPV speed setting (true/false)")
5151 flag.BoolVar(&userCurdConfig.DiscordPresence, "discord-presence", userCurdConfig.DiscordPresence, "Enable Discord presence (true/false)")
5252 continueLast := flag.Bool("c", false, "Continue last episode")
53 updateScript := flag.Bool("u", false, "Update the script")
5354 editConfig := flag.Bool("e", false, "Edit config")
5455
5556 subFlag := flag.Bool("sub", false, "Watch sub version")
@@ -66,6 +67,19 @@ func main() {
6667
6768 anime.Ep.ContinueLast = *continueLast
6869
70 if *updateScript{
71 repo := "wraient/curd"
72 fileName := "curd"
73
74 if err := internal.UpdateCurd(repo, fileName); err != nil {
75 fmt.Printf("Error updating executable: %v\n", err)
76 os.Exit(1)
77 } else {
78 fmt.Println("Program Updated!")
79 internal.ExitCurd()
80 }
81 }
82
6983 if *editConfig {
7084 internal.EditConfig(configFilePath)
7185 return
internal/curd.go +60 −1
@@ -3,6 +3,8 @@ package internal
33import (
44 "encoding/json"
55 "fmt"
6 "io"
7 "net/http"
68 "os"
79 "os/exec"
810 "path/filepath"
@@ -109,11 +111,68 @@ func RestoreScreen() {
109111}
110112
111113func ExitCurd() {
112 // RestoreScreen()
114 RestoreScreen()
113115 fmt.Println("Have a great day!")
114116 os.Exit(0)
115117}
116118
119func 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 }
125
126 // GitHub release URL for curd
127 url := fmt.Sprintf("https://github.com/%s/releases/latest/download/%s", repo, fileName)
128
129 // Temporary path for the downloaded curd.py
130 tmpPath := executablePath + ".tmp"
131
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()
138
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 }
143
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()
150
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 }
156
157 // Close and rename the temporary file to replace the current executable
158 out.Close()
159
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 }
169
170 fmt.Println("Program Updated!")
171
172 return nil
173}
174
175
117176func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime, logFile string) {
118177 var err error
119178