aboutsummaryrefslogtreecommitdiff
path: root/yae.go
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-10-12 00:29:29 -0700
committerFuwn <[email protected]>2024-10-12 00:31:49 -0700
commit86b4d70fa5ea0c5215571e1af28b1b6f4ed1010a (patch)
tree8b8e2ca9df5dc351aeb65fe7187c286f19297367 /yae.go
parent4b6504a63da9dd8db8f62acb33371e63a2c2f696 (diff)
downloadyae-86b4d70fa5ea0c5215571e1af28b1b6f4ed1010a.tar.xz
yae-86b4d70fa5ea0c5215571e1af28b1b6f4ed1010a.zip
refactor(source): move source to separate file
Diffstat (limited to 'yae.go')
-rw-r--r--yae.go118
1 files changed, 4 insertions, 114 deletions
diff --git a/yae.go b/yae.go
index 258c45f..fa38c2d 100644
--- a/yae.go
+++ b/yae.go
@@ -213,8 +213,8 @@ func main() {
forcePinned := c.Bool("force-pinned")
if c.Args().Len() == 0 {
- for name, value := range sources {
- if updated, err := updateSource(&sources, name, value, showAll, force, forcePinned); err != nil {
+ for name, source := range sources {
+ if updated, err := source.Update(&sources, name, showAll, force, forcePinned); err != nil {
return err
} else if updated {
updates = append(updates, name)
@@ -222,8 +222,9 @@ func main() {
}
} else {
name := c.Args().Get(0)
+ source := sources[name]
- if updated, err := updateSource(&sources, name, sources[name], showAll, force, forcePinned); err != nil {
+ if updated, err := source.Update(&sources, name, showAll, force, forcePinned); err != nil {
return err
} else if updated {
updates = append(updates, name)
@@ -288,114 +289,3 @@ func command(name string, show bool, args ...string) (string, error) {
return string(out), err
}
-
-func fetchLatestGitTag(source Source, show bool) (string, error) {
- if source.Type == "git" {
- repository := "https://github.com/" + strings.Split(source.URL, "/")[3] + "/" + strings.Split(source.URL, "/")[4]
- remotes, err := command("bash", show, "-c", fmt.Sprintf("git ls-remote %s | awk -F'/' '{print $NF}' | sort -V", repository))
-
- if err != nil {
- return "", err
- }
-
- refs := strings.Split(remotes, "\n")
- var latest string
-
- if source.TagPredicate == "" {
- latest = refs[len(refs)-2]
- } else {
- for i := len(refs) - 2; i >= 0; i-- {
- if strings.Contains(refs[i], source.TagPredicate) {
- latest = refs[i]
-
- break
- }
- }
- }
-
- if source.TrimTagPrefix != "" {
- latest = strings.TrimPrefix(latest, source.TrimTagPrefix)
- }
-
- return latest, nil
- }
-
- return "", fmt.Errorf("source is not a git repository")
-}
-
-func updateSource(sources *Sources, name string, source Source, show bool, force bool, forcePinned bool) (bool, error) {
- updated := false
-
- if !sources.Exists(name) {
- return updated, fmt.Errorf("source does not exist")
- }
-
- if source.Pinned && !forcePinned {
- if show {
- fmt.Println("skipped update for", name, "because it is pinned")
- }
-
- return updated, nil
- }
-
- if source.Type == "git" {
- tag, err := fetchLatestGitTag(source, show)
-
- if err != nil {
- return updated, err
- }
-
- if tag != source.Version || force || source.Force {
- if show {
- fmt.Println("updated version for", name, "from", source.Version, "to", tag)
- }
-
- if tag != source.Version {
- updated = true
- }
-
- source.Version = tag
-
- if strings.Contains(source.URLTemplate, "{version}") {
- source.URL = strings.ReplaceAll(source.URLTemplate, "{version}", source.Version)
- }
- } else {
- if show {
- fmt.Println("skipped update for", name, "because the version is unchanged")
- }
-
- return updated, nil
- }
- }
-
- sha256, err := fetchSHA256(source.URL, source.Unpack, show)
-
- if err != nil {
- return updated, err
- }
-
- if sha256 != source.SHA256 {
- if show {
- fmt.Println("updated hash for", name, "from", source.SHA256, "to", sha256)
- }
-
- source.SHA256 = sha256
- updated = true
- }
-
- (*sources)[name] = source
-
- return updated, nil
-}
-
-func lister(items []string) string {
- if len(items) == 0 {
- return ""
- } else if len(items) == 1 {
- return items[0]
- } else if len(items) == 2 {
- return fmt.Sprintf("%s & %s", items[0], items[1])
- }
-
- return fmt.Sprintf("%s, & %s", strings.Join(items[:len(items)-1], ", "), items[len(items)-1])
-}