diff options
Diffstat (limited to '.vim/plugin/k8s-edit-secret.vim')
-rw-r--r-- | .vim/plugin/k8s-edit-secret.vim | 60 |
1 files changed, 60 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..1a3e063 --- /dev/null +++ b/.vim/plugin/k8s-edit-secret.vim @@ -0,0 +1,60 @@ +" 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 +let g:k8s_edit_secret_file_pattern = get(g:, "k8s_edit_secret_file_pattern", "/tmp/kubectl-edit-*.yaml") + +" 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 + let view = winsaveview() + silent exe "%!kubectl secret decode" + call winrestview(view) +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 + let view = winsaveview() + silent exe "%!kubectl secret encode" + call winrestview(view) +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 |