aboutsummaryrefslogtreecommitdiff
path: root/.vim/plugin/k8s-edit-secret.vim
blob: 1a3e06348dab026bbba0d720834e68491fc3f00d (plain)
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
" 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