code refactoring

This commit is contained in:
2023-07-26 16:21:10 +02:00
parent a4d586a3a0
commit d4f656fd87
6 changed files with 20 additions and 70 deletions

View File

@@ -1,45 +0,0 @@
package updater
import (
"io"
"log"
"net/http"
"github.com/goccy/go-json"
)
const (
gitHubAPILatest = "https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest"
gitHubAPIDownload = "https://api.github.com/repos/yt-dlp/yt-dlp/releases/download"
)
var (
client = &http.Client{
CheckRedirect: http.DefaultClient.CheckRedirect,
}
)
func getLatestReleaseTag() (string, error) {
res, err := client.Get(gitHubAPILatest)
if err != nil {
log.Println("Cannot get release tag from GitHub API")
return "", err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
log.Println("Cannot parse response from GitHub API")
return "", err
}
tag := ReleaseLatestResponse{}
json.Unmarshal(body, &tag)
return tag.TagName, nil
}
func ForceUpdate() {
getLatestReleaseTag()
}

View File

@@ -1,6 +0,0 @@
package updater
type ReleaseLatestResponse struct {
Name string `json:"name"`
TagName string `json:"tag_name"`
}

View File

@@ -6,13 +6,14 @@ import (
"github.com/marcopeocchi/yt-dlp-web-ui/server/config"
)
var path = config.Instance().GetConfig().DownloaderPath
// Update using the builtin function of yt-dlp
func UpdateExecutable() error {
cmd := exec.Command(path, "-U")
cmd.Start()
cmd := exec.Command(config.Instance().GetConfig().DownloaderPath, "-U")
err := cmd.Wait()
return err
err := cmd.Start()
if err != nil {
return err
}
return cmd.Wait()
}