⛩️ Powerful yet Minimal Nix Dependency Manager
flake flakes home-manager nixos go nix dependency dependencies
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(source): move source to separate file

Fuwn cbd0c5c7 e22b1247

+134 -127
+130
source.go
··· 1 + package main 2 + 3 + import ( 4 + "fmt" 5 + "strings" 6 + ) 7 + 8 + type Source struct { 9 + URL string `json:"url"` 10 + SHA256 string `json:"sha256"` 11 + Unpack bool `json:"unpack"` 12 + Type string `json:"type"` 13 + Version string `json:"version,omitempty"` 14 + URLTemplate string `json:"url_template,omitempty"` 15 + TagPredicate string `json:"tag_predicate,omitempty"` 16 + TrimTagPrefix string `json:"trim_tag_prefix,omitempty"` 17 + Pinned bool `json:"pinned,omitempty"` 18 + Force bool `json:"force,omitempty"` 19 + } 20 + 21 + func (source *Source) Update(sources *Sources, name string, show bool, force bool, forcePinned bool) (bool, error) { 22 + updated := false 23 + 24 + if !sources.Exists(name) { 25 + return updated, fmt.Errorf("source does not exist") 26 + } 27 + 28 + if source.Pinned && !forcePinned { 29 + if show { 30 + fmt.Println("skipped update for", name, "because it is pinned") 31 + } 32 + 33 + return updated, nil 34 + } 35 + 36 + if source.Type == "git" { 37 + tag, err := fetchLatestGitTag(*source, show) 38 + 39 + if err != nil { 40 + return updated, err 41 + } 42 + 43 + if tag != source.Version || force || source.Force { 44 + if show { 45 + fmt.Println("updated version for", name, "from", source.Version, "to", tag) 46 + } 47 + 48 + if tag != source.Version { 49 + updated = true 50 + } 51 + 52 + source.Version = tag 53 + 54 + if strings.Contains(source.URLTemplate, "{version}") { 55 + source.URL = strings.ReplaceAll(source.URLTemplate, "{version}", source.Version) 56 + } 57 + } else { 58 + if show { 59 + fmt.Println("skipped update for", name, "because the version is unchanged") 60 + } 61 + 62 + return updated, nil 63 + } 64 + } 65 + 66 + sha256, err := fetchSHA256(source.URL, source.Unpack, show) 67 + 68 + if err != nil { 69 + return updated, err 70 + } 71 + 72 + if sha256 != source.SHA256 { 73 + if show { 74 + fmt.Println("updated hash for", name, "from", source.SHA256, "to", sha256) 75 + } 76 + 77 + source.SHA256 = sha256 78 + updated = true 79 + } 80 + 81 + (*sources)[name] = *source 82 + 83 + return updated, nil 84 + } 85 + 86 + func fetchLatestGitTag(source Source, show bool) (string, error) { 87 + if source.Type == "git" { 88 + repository := "https://github.com/" + strings.Split(source.URL, "/")[3] + "/" + strings.Split(source.URL, "/")[4] 89 + remotes, err := command("bash", show, "-c", fmt.Sprintf("git ls-remote %s | awk -F'/' '{print $NF}' | sort -V", repository)) 90 + 91 + if err != nil { 92 + return "", err 93 + } 94 + 95 + refs := strings.Split(remotes, "\n") 96 + var latest string 97 + 98 + if source.TagPredicate == "" { 99 + latest = refs[len(refs)-2] 100 + } else { 101 + for i := len(refs) - 2; i >= 0; i-- { 102 + if strings.Contains(refs[i], source.TagPredicate) { 103 + latest = refs[i] 104 + 105 + break 106 + } 107 + } 108 + } 109 + 110 + if source.TrimTagPrefix != "" { 111 + latest = strings.TrimPrefix(latest, source.TrimTagPrefix) 112 + } 113 + 114 + return latest, nil 115 + } 116 + 117 + return "", fmt.Errorf("source is not a git repository") 118 + } 119 + 120 + func lister(items []string) string { 121 + if len(items) == 0 { 122 + return "" 123 + } else if len(items) == 1 { 124 + return items[0] 125 + } else if len(items) == 2 { 126 + return fmt.Sprintf("%s & %s", items[0], items[1]) 127 + } 128 + 129 + return fmt.Sprintf("%s, & %s", strings.Join(items[:len(items)-1], ", "), items[len(items)-1]) 130 + }
-13
sources.go
··· 8 8 9 9 type Sources map[string]Source 10 10 11 - type Source struct { 12 - URL string `json:"url"` 13 - SHA256 string `json:"sha256"` 14 - Unpack bool `json:"unpack"` 15 - Type string `json:"type"` 16 - Version string `json:"version,omitempty"` 17 - URLTemplate string `json:"url_template,omitempty"` 18 - TagPredicate string `json:"tag_predicate,omitempty"` 19 - TrimTagPrefix string `json:"trim_tag_prefix,omitempty"` 20 - Pinned bool `json:"pinned,omitempty"` 21 - Force bool `json:"force,omitempty"` 22 - } 23 - 24 11 func (s *Sources) EnsureLoaded() error { 25 12 return nil 26 13 }
+4 -114
yae.go
··· 213 213 forcePinned := c.Bool("force-pinned") 214 214 215 215 if c.Args().Len() == 0 { 216 - for name, value := range sources { 217 - if updated, err := updateSource(&sources, name, value, showAll, force, forcePinned); err != nil { 216 + for name, source := range sources { 217 + if updated, err := source.Update(&sources, name, showAll, force, forcePinned); err != nil { 218 218 return err 219 219 } else if updated { 220 220 updates = append(updates, name) ··· 222 222 } 223 223 } else { 224 224 name := c.Args().Get(0) 225 + source := sources[name] 225 226 226 - if updated, err := updateSource(&sources, name, sources[name], showAll, force, forcePinned); err != nil { 227 + if updated, err := source.Update(&sources, name, showAll, force, forcePinned); err != nil { 227 228 return err 228 229 } else if updated { 229 230 updates = append(updates, name) ··· 288 289 289 290 return string(out), err 290 291 } 291 - 292 - func fetchLatestGitTag(source Source, show bool) (string, error) { 293 - if source.Type == "git" { 294 - repository := "https://github.com/" + strings.Split(source.URL, "/")[3] + "/" + strings.Split(source.URL, "/")[4] 295 - remotes, err := command("bash", show, "-c", fmt.Sprintf("git ls-remote %s | awk -F'/' '{print $NF}' | sort -V", repository)) 296 - 297 - if err != nil { 298 - return "", err 299 - } 300 - 301 - refs := strings.Split(remotes, "\n") 302 - var latest string 303 - 304 - if source.TagPredicate == "" { 305 - latest = refs[len(refs)-2] 306 - } else { 307 - for i := len(refs) - 2; i >= 0; i-- { 308 - if strings.Contains(refs[i], source.TagPredicate) { 309 - latest = refs[i] 310 - 311 - break 312 - } 313 - } 314 - } 315 - 316 - if source.TrimTagPrefix != "" { 317 - latest = strings.TrimPrefix(latest, source.TrimTagPrefix) 318 - } 319 - 320 - return latest, nil 321 - } 322 - 323 - return "", fmt.Errorf("source is not a git repository") 324 - } 325 - 326 - func updateSource(sources *Sources, name string, source Source, show bool, force bool, forcePinned bool) (bool, error) { 327 - updated := false 328 - 329 - if !sources.Exists(name) { 330 - return updated, fmt.Errorf("source does not exist") 331 - } 332 - 333 - if source.Pinned && !forcePinned { 334 - if show { 335 - fmt.Println("skipped update for", name, "because it is pinned") 336 - } 337 - 338 - return updated, nil 339 - } 340 - 341 - if source.Type == "git" { 342 - tag, err := fetchLatestGitTag(source, show) 343 - 344 - if err != nil { 345 - return updated, err 346 - } 347 - 348 - if tag != source.Version || force || source.Force { 349 - if show { 350 - fmt.Println("updated version for", name, "from", source.Version, "to", tag) 351 - } 352 - 353 - if tag != source.Version { 354 - updated = true 355 - } 356 - 357 - source.Version = tag 358 - 359 - if strings.Contains(source.URLTemplate, "{version}") { 360 - source.URL = strings.ReplaceAll(source.URLTemplate, "{version}", source.Version) 361 - } 362 - } else { 363 - if show { 364 - fmt.Println("skipped update for", name, "because the version is unchanged") 365 - } 366 - 367 - return updated, nil 368 - } 369 - } 370 - 371 - sha256, err := fetchSHA256(source.URL, source.Unpack, show) 372 - 373 - if err != nil { 374 - return updated, err 375 - } 376 - 377 - if sha256 != source.SHA256 { 378 - if show { 379 - fmt.Println("updated hash for", name, "from", source.SHA256, "to", sha256) 380 - } 381 - 382 - source.SHA256 = sha256 383 - updated = true 384 - } 385 - 386 - (*sources)[name] = source 387 - 388 - return updated, nil 389 - } 390 - 391 - func lister(items []string) string { 392 - if len(items) == 0 { 393 - return "" 394 - } else if len(items) == 1 { 395 - return items[0] 396 - } else if len(items) == 2 { 397 - return fmt.Sprintf("%s & %s", items[0], items[1]) 398 - } 399 - 400 - return fmt.Sprintf("%s, & %s", strings.Join(items[:len(items)-1], ", "), items[len(items)-1]) 401 - }