From 9f808fd514d65dae59bf2c1bc1ecda7edbbba87f Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sat, 5 Aug 2023 22:18:55 +0200 Subject: Add vim plugin to make dealing with k8s secrets easier --- .vim/plugin/k8s-edit-secret.vim | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .vim/plugin/k8s-edit-secret.vim 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 -- cgit v1.1