From 037d58e2b92faca8eaec6f06ae4249f334f3b9aa Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Sun, 10 May 2020 01:34:03 -0400 Subject: added endpoint to query for all installed packages --- cache.go | 2 ++ pkgs.go | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/cache.go b/cache.go index 283f698..fde11ee 100644 --- a/cache.go +++ b/cache.go @@ -87,6 +87,8 @@ func (cache *cacheWrapper) bap(requestPath string) error { bytes, err = uptimeQuery(format) case "usercount": bytes, err = userCountQuery(format) + case "pkgs": + bytes, err = pkgsQuery(format) default: if requestPath == "/" { bytes, err = ioutil.ReadFile("web/index.txt") diff --git a/pkgs.go b/pkgs.go index 991ba53..9674916 100644 --- a/pkgs.go +++ b/pkgs.go @@ -1,10 +1,40 @@ package main -import "net/http" +import ( + "fmt" + "os/exec" + "strings" +) -// Pkgs handles the //pkgs endpoint. -// Sends a list of installed packages. -func Pkgs(w http.ResponseWriter, r *http.Request, format string) error { +// Returns a list of packages installed on the system +func pkgsQuery(format string) ([]byte, error) { + raw, err := exec.Command("pkg_info", "-a").Output() + if err != nil { + return nil, err + } - return nil + if format == "plain" { + return raw, nil + } + + json := `{ + "packages": [` + rawlines := strings.Split(string(raw), "\n") + for _, line := range rawlines { + split := strings.Fields(line) + if len(split) < 2 { + continue + } + desc := strings.Join(split[1:], " ") + json = fmt.Sprintf(`%s + { + "package": "%s", + "description": "%s" + },`, json, split[0], desc) + } + json = fmt.Sprintf(`%s + ] +} +`, json[:len(json)-1]) + return []byte(json), nil } -- cgit 1.4.1-2-gfad0 be397c53e392f732'>diff stats
blob: 3f8d8403b58bff6628d30734439517265dd105fe (plain) (blame)
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