-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rkt
88 lines (77 loc) · 2.74 KB
/
main.rkt
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
#lang racket/base
(require racket/cmdline
racket/contract
racket/list
racket/port
racket/string)
(require net/http-easy)
(require text-table)
(require json)
(require "convert.rkt")
(define/contract (subscription-file path)
(-> path-string? input-port?)
(let ([exn->user-error (λ (e) (raise-user-error (exn-message e)))])
(with-handlers ([exn:fail:filesystem:errno? exn->user-error]
[exn:fail:filesystem? exn->user-error])
(open-input-file path #:mode 'text))))
(define/contract (online-data)
(-> jsexpr?)
(response-json
(let ([res
(get
"https://raw.githubusercontent.com/AOSC-Dev/anicca/main/pkgsupdate.json")])
(if (= (response-status-code res) 200)
res
(raise-user-error 'anicca-subscribe
"failed to fetch anicca package update list")))))
(define/contract (format-table res)
(-> (listof (listof string?)) void?)
(print-table #:row-sep? '(#t #f ...)
#:col-sep? '(#t #f ...)
(cons '(Name Category Before After Warnings) res)))
(define/contract (search ps anicca-data)
(-> (listof string?) (listof (listof string?)) (listof (listof string?)))
(foldl (lambda (p acc)
(let ([entry (assoc p anicca-data)])
(if entry (cons entry acc) acc)))
'()
ps))
(define/contract package-names
(parameter/c (listof string?))
(make-parameter null))
(define/contract local-data
(parameter/c jsexpr?)
(make-parameter #f))
(define/contract roll?
(parameter/c boolean?)
(make-parameter #f))
(define (cli)
(command-line #:program "anicca-subscribe"
#:once-each
[("-l" "--local")
path
"Use a local aosc-findupdate json file"
(local-data (read-json (open-input-file path)))]
#:once-any
[("-f" "--file")
path
"Use a subscription file"
(package-names (port->lines (subscription-file path)))]
[("-p" "--packages")
packages
"Use package names"
(package-names (string-split packages ","))]
[("-r" "--roll")
"Roll 10 packages"
(roll? #t)]))
(cli)
(define/contract anicca-data
(listof (listof string?))
(convert (if (local-data) (local-data) (online-data))))
(if (roll?)
(let ([roll-result (take (shuffle anicca-data) (min 10 (length anicca-data)))])
(format-table (sort roll-result string<? #:key car)))
(let ([search-result (search (package-names) anicca-data)])
(if (null? search-result)
(exit)
(format-table (sort search-result string<? #:key car)))))