forked from ahmetb/kubectl-aliases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_aliases.py
executable file
·183 lines (150 loc) · 5.43 KB
/
generate_aliases.py
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
180
181
182
183
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import itertools
import os.path
import sys
try:
xrange # Python 2
except NameError:
xrange = range # Python 3
def main():
# (alias, full, allow_when_oneof, incompatible_with)
cmds = [('k', 'kubectl', None, None)]
globs = [('sys', '--namespace=kube-system', None, ['sys'])]
ops = [
('a', 'apply --recursive -f', None, None),
('ex', 'exec -i -t', None, None),
('lo', 'logs -f', None, None),
('lop', 'logs -f -p', None, None),
('p', 'proxy', None, ['sys']),
('g', 'get', None, None),
('d', 'describe', None, None),
('rm', 'delete', None, None),
('run', 'run --rm --restart=Never --image-pull-policy=IfNotPresent -i -t', None, None),
]
res = [
('po', 'pods', ['g', 'd', 'rm'], None),
('dep', 'deployment', ['g', 'd', 'rm'], None),
('svc', 'service', ['g', 'd', 'rm'], None),
('ing', 'ingress', ['g', 'd', 'rm'], None),
('cm', 'configmap', ['g', 'd', 'rm'], None),
('sec', 'secret', ['g', 'd', 'rm'], None),
('no', 'nodes', ['g', 'd'], ['sys']),
('ns', 'namespaces', ['g', 'd', 'rm'], ['sys']),
]
res_types = [r[0] for r in res]
args = [
('oyaml', '-o=yaml', ['g'], ['owide', 'ojson', 'sl']),
('owide', '-o=wide', ['g'], ['oyaml', 'ojson']),
('ojson', '-o=json', ['g'], ['owide', 'oyaml', 'sl']),
('all', '--all-namespaces', ['g', 'd'], ['rm', 'f', 'no', 'sys'
]),
('sl', '--show-labels', ['g'], ['oyaml', 'ojson']
+ diff(res_types, ['po', 'dep'])),
('all', '--all', ['rm'], None), # caution: reusing the alias
('w', '--watch', ['g'], ['oyaml', 'ojson', 'owide']),
]
# these accept a value, so they need to be at the end and
# mutually exclusive within each other.
positional_args = [('f', '--recursive -f', ['g', 'd', 'rm'], res_types + ['all'
, 'l', 'sys']), ('l', '-l', ['g', 'd', 'rm'], ['f',
'all']), ('n', '--namespace', ['g', 'd', 'rm',
'lo', 'ex'], ['ns', 'no', 'sys', 'all'])]
# [(part, optional, take_exactly_one)]
parts = [
(cmds, False, True),
(globs, True, False),
(ops, True, True),
(res, True, True),
(args, True, False),
(positional_args, True, True),
]
out = gen(parts)
out = filter(is_valid, out)
# prepare output
if not sys.stdout.isatty():
header_path = \
os.path.join(os.path.dirname(os.path.realpath(__file__)),
'license_header')
with open(header_path, 'r') as f:
print(f.read())
for cmd in out:
print("alias {}='{}'".format(''.join([a[0] for a in cmd]),
' '.join([a[1] for a in cmd])))
def gen(parts):
out = [()]
for (items, optional, take_exactly_one) in parts:
orig = list(out)
combos = []
if optional and take_exactly_one:
combos = combos.append([])
if take_exactly_one:
combos = combinations(items, 1, include_0=optional)
else:
combos = combinations(items, len(items), include_0=optional)
# permutate the combinations if optional (args are not positional)
if optional:
new_combos = []
for c in combos:
new_combos += list(itertools.permutations(c))
combos = new_combos
new_out = []
for segment in combos:
for stuff in orig:
new_out.append(stuff + segment)
out = new_out
return out
def is_valid(cmd):
for i in xrange(0, len(cmd)):
# check at least one of requirements are in the cmd
requirements = cmd[i][2]
if requirements:
found = False
for r in requirements:
for j in xrange(0, i):
if cmd[j][0] == r:
found = True
break
if found:
break
if not found:
return False
# check none of the incompatibilities are in the cmd
incompatibilities = cmd[i][3]
if incompatibilities:
found = False
for inc in incompatibilities:
for j in xrange(0, i):
if cmd[j][0] == inc:
found = True
break
if found:
break
if found:
return False
return True
def combinations(a, n, include_0=True):
l = []
for j in xrange(0, n + 1):
if not include_0 and j == 0:
continue
l += list(itertools.combinations(a, j))
return l
def diff(a, b):
return list(set(a) - set(b))
if __name__ == '__main__':
main()