-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpkgrep.go
More file actions
175 lines (158 loc) · 4.79 KB
/
pkgrep.go
File metadata and controls
175 lines (158 loc) · 4.79 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Pkgrep queries multiple package repositories by package name.
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"runtime/debug"
"slices"
"strings"
"time"
"github.com/skewb1k/pkgrep/internal/alpine"
"github.com/skewb1k/pkgrep/internal/aosc"
"github.com/skewb1k/pkgrep/internal/archlinux"
"github.com/skewb1k/pkgrep/internal/aur"
"github.com/skewb1k/pkgrep/internal/chocolatey"
"github.com/skewb1k/pkgrep/internal/clojars"
"github.com/skewb1k/pkgrep/internal/cran"
"github.com/skewb1k/pkgrep/internal/cratesio"
"github.com/skewb1k/pkgrep/internal/debian"
"github.com/skewb1k/pkgrep/internal/dub"
"github.com/skewb1k/pkgrep/internal/fedora"
"github.com/skewb1k/pkgrep/internal/guix"
"github.com/skewb1k/pkgrep/internal/hackage"
"github.com/skewb1k/pkgrep/internal/hex"
"github.com/skewb1k/pkgrep/internal/homebrew"
"github.com/skewb1k/pkgrep/internal/julia"
"github.com/skewb1k/pkgrep/internal/kali"
"github.com/skewb1k/pkgrep/internal/luarocks"
"github.com/skewb1k/pkgrep/internal/macports"
"github.com/skewb1k/pkgrep/internal/melpa"
"github.com/skewb1k/pkgrep/internal/nixpkgs"
"github.com/skewb1k/pkgrep/internal/npm"
"github.com/skewb1k/pkgrep/internal/nuget"
"github.com/skewb1k/pkgrep/internal/opam"
"github.com/skewb1k/pkgrep/internal/pkggodev"
"github.com/skewb1k/pkgrep/internal/pubdev"
"github.com/skewb1k/pkgrep/internal/pypi"
"github.com/skewb1k/pkgrep/internal/rubygems"
"github.com/skewb1k/pkgrep/internal/scoop"
"github.com/skewb1k/pkgrep/internal/sisyphus"
"github.com/skewb1k/pkgrep/internal/snapcraft"
"github.com/skewb1k/pkgrep/internal/ubuntu"
"github.com/skewb1k/pkgrep/internal/voidlinux"
)
type Querier interface {
// Query accepts a search query string and returns a
// boolean indicating whether the package was found, or an error.
Query(query string) (bool, error)
// Name returns the name of the querier, usually a repository name.
Name() string
}
type repoList []string
func (rl *repoList) String() string {
return fmt.Sprint(*rl)
}
func (rl *repoList) Set(value string) error {
for p := range strings.SplitSeq(value, ",") {
*rl = append(*rl, strings.ToLower(p))
}
return nil
}
var (
flagDryRun = flag.Bool("dry-run", false, "do everything except actually send the requests")
flagList = flag.Bool("list", false, "list repositories")
flagVersion = flag.Bool("version", false, "print version")
flagTimeout = flag.Int64("timeout", 0, "time limit for a single request in seconds")
flagInclude repoList
flagExclude repoList
)
func init() {
flag.Var(&flagInclude, "include", "search in specified repositories only")
flag.Var(&flagExclude, "exclude", "skip specified repositories")
}
func main() {
log.SetPrefix("pkgrep: ")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
if *flagVersion {
if build, ok := debug.ReadBuildInfo(); ok {
fmt.Println(build.Main.Version)
} else {
fmt.Println("unknown version")
}
return
}
httpClient := &http.Client{
Transport: &UserAgentRoundTripper{
RoundTripper: http.DefaultTransport,
UserAgent: "pkgrep/0 (https://github.com/skewb1k/pkgrep; mailto:skewb1kunix@gmail.com)",
},
Timeout: time.Duration(*flagTimeout) * time.Second,
}
queriers := []Querier{
alpine.Client{httpClient},
aosc.Client{httpClient},
archlinux.Client{httpClient},
aur.Client{httpClient},
chocolatey.Client{httpClient},
clojars.Client{httpClient},
cran.Client{httpClient},
cratesio.Client{httpClient},
debian.Client{httpClient},
dub.Client{httpClient},
fedora.Client{httpClient},
guix.Client{httpClient},
hackage.Client{httpClient},
hex.Client{httpClient},
homebrew.Client{httpClient},
julia.Client{httpClient},
kali.Client{httpClient},
luarocks.Client{httpClient},
macports.Client{httpClient},
melpa.Client{httpClient},
nixpkgs.Client{httpClient},
npm.Client{httpClient},
nuget.Client{httpClient},
opam.Client{httpClient},
pkggodev.Client{httpClient},
pubdev.Client{httpClient},
pypi.Client{httpClient},
rubygems.Client{httpClient},
scoop.Client{httpClient},
sisyphus.Client{httpClient},
snapcraft.Client{httpClient},
ubuntu.Client{httpClient},
voidlinux.Client{httpClient},
}
if *flagList {
for _, querier := range queriers {
fmt.Println(querier.Name())
}
os.Exit(0)
}
if flag.NArg() != 1 {
log.Print("missing query")
usage()
}
query := flag.Arg(0)
foundAny := runQuery(queriers, query)
if !foundAny {
os.Exit(1)
}
}
func usage() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s QUERY\n", os.Args[0])
flag.PrintDefaults()
os.Exit(2)
}
// Checks if repository name is explicitly excluded or not included via flags.
func shouldSkipRepository(name string) bool {
nameLower := strings.ToLower(name)
excluded := slices.Contains(flagExclude, nameLower)
included := len(flagInclude) == 0 || slices.Contains(flagInclude, nameLower)
return excluded || !included
}