-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmcli.lisp
executable file
·64 lines (57 loc) · 2.19 KB
/
nmcli.lisp
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
(defpackage #:pipeline.nmcli
(:use #:cl #:alexandria #:cl-ppcre #:pipeline)
(:nicknames :nmcli)
(:export #:connections
#:device-type
#:uuid
#:name))
(in-package #:pipeline.nmcli)
(defstruct
(connection
(:constructor connection (&key ((:type device-type)) name uuid))
(:conc-name))
name
uuid
device-type)
(defun slice (string start end)
(make-array (- (or end (length string)) start)
:element-type (array-element-type string)
:displaced-to string
:displaced-index-offset start))
(defun trim-bounds (bucket side string start end)
(setf end (or end (length string)))
(check-type side (member :both :right :left))
(let ((bucketp (etypecase bucket
(null (return-from trim-bounds (values start end)))
(string (lambda (c) (find c bucket)))
(character (lambda (c) (char= c bucket)))
(cons (lambda (c) (member c bucket :test #'char=))))))
(labels ((non-bucket-pos (from-end)
(position-if-not bucketp
string
:from-end from-end
:start start
:end end)))
(when (member side '(:both :left))
(setf start (non-bucket-pos nil)))
(if start
(values start
(if (member side '(:both :right))
(1+ (non-bucket-pos t))
end))
(values 0 0)))))
(defun trim (bucket string &optional (start 0) end (side :both))
(multiple-value-bind (s e) (trim-bounds bucket side string start end)
(slice string s e )))
(defun connections ()
(results
(with-pipeline ()
(program "nmcli" "--fields" "name,uuid,type" "connection")
(lambda-line (line)
(flet ((trim (s &optional e) (trim #\space line s e)))
(declare (inline trim))
(multiple-value-bind (start end) (scan "[0-9a-f-]{36}" line)
(when start
(signal-result (connection :name (trim 0 start)
:uuid (trim start end)
:type (trim end))))))))))