aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2023-08-05 22:18:55 +0200
committerPatrick Spek <p.spek@tyil.nl>2023-08-05 22:18:55 +0200
commit9f808fd514d65dae59bf2c1bc1ecda7edbbba87f (patch)
treeab9783cee64eb19f96d5484abe200044af2f6efe
parent4b09d3f89c1037e495c582e733024006ec8d5c43 (diff)
Add vim plugin to make dealing with k8s secrets easier
-rw-r--r--.vim/plugin/k8s-edit-secret.vim58
1 files changed, 58 insertions, 0 deletions
diff --git a/.vim/plugin/k8s-edit-secret.vim b/.vim/plugin/k8s-edit-secret.vim
new file mode 100644
index 0000000..26b67df
--- /dev/null
+++ b/.vim/plugin/k8s-edit-secret.vim
@@ -0,0 +1,58 @@
+" This program is free software: you can redistribute it and/or modify it under
+" the terms of the GNU Affero General Public License as published by the Free
+" Software Foundation, either version 3 of the License, or (at your option) any
+" later version.
+"
+" This program is distributed in the hope that it will be useful, but WITHOUT
+" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+" FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
+" details.
+
+" Ensure this plugin is only loaded once
+if (exists("g:k8s_edit_secret") || &cp || exists("#K8sEditSecret"))
+ finish
+endif
+
+let g:k8s_edit_secret = "1"
+
+" Define the file pattern
+if (!exists("g:k8s_edit_secret_file_pattern"))
+ let g:k8s_edit_secret_file_pattern = "/tmp/kubectl-edit-*.yaml"
+endif
+
+" Define the decode function
+function K8sEditSecretDecode (...)
+ " Check if the file is really a secret
+ silent exe "!grep -q \"^kind: Secret$\" \"%:p\""
+
+ " Not a secret, just read the file as usual
+ if (v:shell_error)
+ return
+ endif
+
+ " Pipe buffer through kubectl secret decode
+ silent exe "%!kubectl secret decode"
+endfunction
+
+" Define the encode function
+function K8sEditSecretEncode (...)
+ " Check if the file is really a secret
+ silent exe "!grep -q \"^kind: Secret$\" \"%:p\""
+
+ " Not a secret, just read the file as usual
+ if (v:shell_error)
+ return
+ endif
+
+ " Pipe buffer through kubectl secret encode
+ silent exe "%!kubectl secret encode"
+endfunction
+
+" Define the autocmd
+augroup K8sEditSecret
+ autocmd!
+
+ " Register autocmd on read/write
+ exe "autocmd BufWritePre " . g:k8s_edit_secret_file_pattern . " call K8sEditSecretEncode()"
+ exe "autocmd BufReadPost,BufWritePost " . g:k8s_edit_secret_file_pattern . " call K8sEditSecretDecode()"
+augroup END