-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path.dir-locals.el
179 lines (178 loc) · 9.27 KB
/
.dir-locals.el
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
176
177
178
179
((
org-mode
.
(
(
eval
.
(progn
;; define our funs!
(message "START: ii/sql-org-hacks")
;; ensure tmpdir is in home dir so noexec / tmp doesn't fail
(set (make-local-variable 'temporary-file-directory)
(concat (getenv "HOME") "/tmp/"))
(mkdir temporary-file-directory t)
(set (make-local-variable 'org-babel-temporary-directory)
(concat temporary-file-directory "babel"))
(mkdir org-babel-temporary-directory t)
(setenv "GOTMPDIR" org-babel-temporary-directory)
(set (make-local-variable 'org-babel-default-header-args:sql-mode)
;; Set up all sql-mode blocks to be postgres and literate
'((:results . "replace code")
(:product . "postgres")
(:session . "none")
(:noweb . "yes")
(:comments . "no")
(:wrap . "SRC example")))
(set (make-local-variable 'sql-sqlite-program)
(executable-find "sqlite3"))
(set (make-local-variable 'sql-server)
(if (getenv "PGHOST")
(getenv "PGHOST")
(if (file-exists-p "/var/run/secrets/kubernetes.io/serviceaccount/namespace")
;"snoopdb.apisnoop"
"localhost"
"localhost"
)))
(set (make-local-variable 'sql-port)
(if (getenv "PGPORT")
(string-to-number (getenv "PGPORT"))
5432))
(set (make-local-variable 'sql-user)
(if (getenv "PGUSER")
(getenv "PGUSER")
"apisnoop"))
(set (make-local-variable 'sql-database)
(if (getenv "PGDATABASE")
(getenv "PGDATABASE")
"apisnoop"))
(set (make-local-variable 'sql-product)
'(quote postgres))
(set (make-local-variable 'sql-connection-alist)
(list
;; setting these allows for the connection to be
;; created on the fly
(list 'none
(list 'sql-product '(quote postgres))
(list 'sql-user sql-user)
(list 'sql-database sql-database)
(list 'sql-port sql-port)
(list 'sql-server sql-server))))
(message "END: ii/sql-org-hacks")
(setq-local RESOURCENAME "RESOURCENAME")
(defun apisnoop/insert-mock-template ()
"Inserts contents of current directory's mock-template.org file into current buffer."
(interactive)
(let ((mock-template "./mock-template.org"))
(if (file-exists-p mock-template)
(progn (insert-file-contents mock-template)
(normal-mode))
(message "No file named %s found in current directory" mock-template))))
(defun apisnoop/delete-live-events ()
"deletes all live events from audit_events table using psql. Requires you to have already
started up apisnoop."
(interactive)
(shell-command "psql -c \"DELETE FROM audit_event WHERE bucket='apisnoop' and job='live'; \""))
(defun apisnoop/set-resource-name ()
"Replace instances of RESOURCENAME with inputted resource name.
This will save your new resource name, so you can run this mulitple times,
changing from current resource name to new resource name each time."
(interactive)
(let ((from-string RESOURCENAME)
(to-string (read-string "Enter new resource: ")))
(save-excursion
(progn
(goto-char (point-min))
(replace-string from-string to-string)
(setq-local RESOURCENAME to-string)
(message (concat
"Replaced all instances of "
from-string
" to "
to-string))))))
(defun apisnoop/get-mock-test-value ()
"grabs value from code block title 'Mock Test In Go"
(save-excursion
(let* ((mock-test (car (org-element-map (org-element-parse-buffer) 'src-block
(lambda (src-block)
(let ((name (org-element-property :name src-block))
(value (org-element-property :value src-block)))
(if (and (stringp name) (string= name "Mock Test In Go"))
value))))))
(separator "// TEST \\(BEGINS\\|ENDS\\) HERE")
(good-stuff (cadr (s-split separator mock-test))))
good-stuff)))
(defun apisnoop/format-nil-errors (mock-test)
"Formats general purpose errors (when err != null) to fit within ginkgo framework"
(let* ((match " *if err != nil {\n *fmt.Println(err, \"[a-z A-Z]*\")\n *return\n *}")
(g-pre (s-concat (s-repeat 7 " ") "framework.ExpectNoError\(")))
(replace-regexp-in-string match
(lambda (match)
(save-match-data
(let* ((err (cadr (s-split "[()]" match)))
(g-err (s-concat g-pre err "\)")))
g-err)))
mock-test)))
(defun apisnoop/format-eq-errors (mock-test)
"Formats equality comparison errors (podA != podB) to fit within ginkgo framework"
(let* ((match "if \\([a-zA-Z0-9.,']+\\) != \\([a-zA-Z-0-9.,']+\\) *{\n *fmt.Println(\"[a-z A-Z]*\")\n[\t| ]+return\n *}")
(g-pre "framework.ExpectEqual\("))
(replace-regexp-in-string match
(lambda (match)
(save-match-data
(let* ((comparisons (s-split " *!= *"(s-chop-prefix "if "(s-trim(car(s-slice-at "{" match))))))
(c1 (car comparisons))
(c2 (cadr comparisons))
(err (cadr(s-split "[()]" match)))
(g-err (s-concat g-pre c1 ", " c2 ", " err "\)")))
g-err)))
mock-test)))
(defun apisnoop/format-comp-errors (mock-test)
"Formats numeric comparison errors (podA >= podB etc) to fit within ginkgo framework"
(let* ((match " *if \\([a-zA-Z0-9.]+\\) *\\(==\\|>=\\|<=\\|>\\|<\\) *[0-9]+ *{\n[ \ta-zA-Z()\".,'!\n]*}")
(g-pre " framework.Expect\("))
(replace-regexp-in-string match
(lambda (match)
(save-match-data
(let* ((comparison (s-chop-prefix "if "(s-trim(car(s-slice-at "{" match)))))
(err (cadr(s-split "[()]" match)))
(g-err (s-concat g-pre comparison ", true, " err "\)")))
g-err)))
mock-test)))
(defun apisnoop/mock->ginkgo-test ()
"Takes the contents of the code block named 'Mock Test in Go' and runs it through a set of conversions so its body closer matches the ginkgo test framework. It inputs this new code block beneath the heading named 'Ginkgo Test'.
This function assumes you have the appropriately named code block and heading, which, if you used our mock-template, you do. For more details on writing a mock-test to be easier to convert to ginkgo, check out our docs page: docs/writing-a-mock-test.org"
(interactive)
(save-excursion
(let* ((mock-test (apisnoop/get-mock-test-value))
(ginkgo-test (-> mock-test
apisnoop/format-nil-errors
apisnoop/format-eq-errors
apisnoop/format-comp-errors))
(src-code-block (concat "#+NAME: Ginkgo Test\n"
"#+begin_src go\n"
(s-trim-right ginkgo-test)
"\n#+end_src\n")))
(goto-char (org-find-entry-with-id "gt001z4ch1sc00l"))
(goto-char (org-element-property :contents-begin (org-element-at-point)))
(let ((first-element (org-element-at-point)))
(when (eq 'property-drawer (car first-element))
(goto-char (org-element-property :end first-element))))
(insert src-code-block))))
(defun apisnoop/open-babel-dir ()
"Copies the go.mod and go.sum files to the ob-go babel tmp"
(interactive)
(let ((babel-dir (concat org-babel-temporary-directory "/")))
(find-file babel-dir)
(message "Opened %s" babel-dir))
)
(defun apisnoop/go-mod-to-babel-dir ()
"Copies the go.mod and go.sum files to the ob-go babel tmp"
(interactive)
(let ((babel-dir (concat org-babel-temporary-directory "/")))
(copy-file "go.mod" babel-dir t)
(copy-file "go.sum" babel-dir t)
(message "Copied go.{mod,sum} to: %s" babel-dir))
)
(apisnoop/go-mod-to-babel-dir)
))))) ;; end org-mode eval progn