-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
148 lines (134 loc) · 4.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"flag"
"fmt"
"net/http"
"os"
"path"
"sort"
"strings"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
"github.com/mikkeloscar/aur"
"github.com/simon04/aur-out-of-date/action"
"github.com/simon04/aur-out-of-date/config"
"github.com/simon04/aur-out-of-date/pkg"
"github.com/simon04/aur-out-of-date/status"
"github.com/simon04/aur-out-of-date/upstream"
)
var conf *config.Config
var statistics status.Statistics
var commandline struct {
user string
config string
remote bool
local bool
includeVcsPkgs bool
printJSON bool
printStatistics bool
flagOnAur bool
updatePKGBUILD bool
}
func version(pkg pkg.Pkg) (upstream.Version, error) {
if script, ok := conf.Scripts[pkg.Name()]; ok {
return upstream.VersionForScript(script)
}
return upstream.VersionForPkg(pkg)
}
func handlePackage(pkg pkg.Pkg) status.Status {
pkgVersion := pkg.Version()
s := status.Status{
Package: pkg.Name(),
FlaggedOutOfDate: pkg.OutOfDate(),
Version: pkgVersion.String(),
}
upstreamVersion, err := version(pkg)
if err != nil {
s.Status = status.Unknown
s.Message = err.Error()
statistics.Unknown++
return s
}
s.Ignored = conf.IsIgnored(pkg.Name(), upstreamVersion)
s.Compare(upstreamVersion)
statistics.Update(s.Status)
return s
}
func handlePackages(vcsPackages bool, packages []pkg.Pkg, err error) {
if err != nil {
panic(err)
}
sort.Slice(packages, func(i, j int) bool { return strings.Compare(packages[i].Name(), packages[j].Name()) == -1 })
for _, pkg := range packages {
if vcsPackages == pkg.IsVcs() {
s := handlePackage(pkg)
if commandline.printJSON {
s.PrintJSONTextSequence()
} else {
s.Print()
}
if s.Status == status.OutOfDate && commandline.flagOnAur {
action.FlagOnAur(pkg, s.Upstream)
}
if s.Status == status.OutOfDate && commandline.updatePKGBUILD {
action.UpdatePKGBUILD(pkg, s.Upstream)
}
}
}
}
func main() {
configDir, _ := os.UserConfigDir()
defaultConfigFile := path.Join(configDir, "aur-out-of-date", "config.json")
flag.StringVar(&commandline.user, "user", "", "AUR username")
flag.StringVar(&commandline.config, "config", defaultConfigFile, "Config file")
flag.BoolVar(&commandline.remote, "pkg", false, "AUR package name(s)")
flag.BoolVar(&commandline.local, "local", false, "Local .SRCINFO files")
flag.BoolVar(&commandline.includeVcsPkgs, "devel", false, "Check -git/-svn/-hg packages")
flag.BoolVar(&commandline.printStatistics, "statistics", false, "Print summary statistics")
flag.BoolVar(&commandline.flagOnAur, "flag", false, "Flag out-of-date on AUR")
flag.BoolVar(&commandline.updatePKGBUILD, "update", false, "Update pkgver/pkgrel in local PKGBUILD files")
flag.BoolVar(&commandline.printJSON, "json", false, "Generate JSON Text Sequences (RFC 7464)")
flag.Parse()
// cache HTTP requests (RFC 7234)
cacheDir, _ := os.UserCacheDir()
cacheDir = path.Join(cacheDir, "aur-out-of-date")
http.DefaultClient = httpcache.NewTransport(diskcache.New(cacheDir)).Client()
if c, err := config.FromFile(commandline.config); err != nil {
fmt.Fprintln(os.Stderr, "Failed to read config:", err)
os.Exit(1)
} else {
conf = c
}
if commandline.user != "" {
packages, err := aur.SearchBy(commandline.user, aur.Maintainer)
handlePackages(commandline.includeVcsPkgs, pkg.NewRemotePkgs(packages), err)
} else if commandline.remote {
pkgs := flag.Args()
for len(pkgs) > 0 {
limit := 100
if len(pkgs) < limit {
limit = len(pkgs)
}
packages, err := aur.Info(pkgs[:limit])
handlePackages(false, pkg.NewRemotePkgs(packages), err)
handlePackages(true, pkg.NewRemotePkgs(packages), err)
pkgs = pkgs[limit:]
}
} else if commandline.local {
packages, err := pkg.NewLocalPkgs(flag.Args(), commandline.includeVcsPkgs)
handlePackages(false, packages, err)
handlePackages(true, packages, err)
} else {
fmt.Fprintln(os.Stderr, "Either -user or -pkg or -local is required!")
flag.Usage()
os.Exit(1)
}
if commandline.printStatistics && commandline.printJSON {
statistics.PrintJSONTextSequence()
} else if commandline.printStatistics {
statistics.Print()
}
if statistics.OutOfDate > 0 {
os.Exit(4)
}
}