Commit 51884195c8
Unsigned
Layout: unified · split
cmd/curd/main.go +14
| @@ -50,6 +50,7 @@ func main() { | |||
| 50 | flag.BoolVar(&userCurdConfig.SaveMpvSpeed, "save-mpv-speed", userCurdConfig.SaveMpvSpeed, "Save MPV speed setting (true/false)") | 50 | flag.BoolVar(&userCurdConfig.SaveMpvSpeed, "save-mpv-speed", userCurdConfig.SaveMpvSpeed, "Save MPV speed setting (true/false)") |
| 51 | flag.BoolVar(&userCurdConfig.DiscordPresence, "discord-presence", userCurdConfig.DiscordPresence, "Enable Discord presence (true/false)") | 51 | flag.BoolVar(&userCurdConfig.DiscordPresence, "discord-presence", userCurdConfig.DiscordPresence, "Enable Discord presence (true/false)") |
| 52 | continueLast := flag.Bool("c", false, "Continue last episode") | 52 | continueLast := flag.Bool("c", false, "Continue last episode") |
| 53 | updateScript := flag.Bool("u", false, "Update the script") | ||
| 53 | editConfig := flag.Bool("e", false, "Edit config") | 54 | editConfig := flag.Bool("e", false, "Edit config") |
| 54 | 55 | ||
| 55 | subFlag := flag.Bool("sub", false, "Watch sub version") | 56 | subFlag := flag.Bool("sub", false, "Watch sub version") |
| @@ -66,6 +67,19 @@ func main() { | |||
| 66 | 67 | ||
| 67 | anime.Ep.ContinueLast = *continueLast | 68 | anime.Ep.ContinueLast = *continueLast |
| 68 | 69 | ||
| 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 | |||
| 69 | if *editConfig { | 83 | if *editConfig { |
| 70 | internal.EditConfig(configFilePath) | 84 | internal.EditConfig(configFilePath) |
| 71 | return | 85 | return |
internal/curd.go +60 −1
| @@ -3,6 +3,8 @@ package internal | |||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| 5 | "fmt" | 5 | "fmt" |
| 6 | "io" | ||
| 7 | "net/http" | ||
| 6 | "os" | 8 | "os" |
| 7 | "os/exec" | 9 | "os/exec" |
| 8 | "path/filepath" | 10 | "path/filepath" |
| @@ -109,11 +111,68 @@ func RestoreScreen() { | |||
| 109 | } | 111 | } |
| 110 | 112 | ||
| 111 | func ExitCurd() { | 113 | func ExitCurd() { |
| 112 | // RestoreScreen() | 114 | RestoreScreen() |
| 113 | fmt.Println("Have a great day!") | 115 | fmt.Println("Have a great day!") |
| 114 | os.Exit(0) | 116 | os.Exit(0) |
| 115 | } | 117 | } |
| 116 | 118 | ||
| 119 | func 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 | |||
| 117 | func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime, logFile string) { | 176 | func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime, logFile string) { |
| 118 | var err error | 177 | var err error |
| 119 | 178 | ||