Skip to content

Commit 82649b9

Browse files
committed
Refactor null checking, naming and f-string
1 parent 05d784f commit 82649b9

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

awscli/customizations/eks/kubeconfig.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ def has_cluster(self, name):
6262
Return true if this kubeconfig contains an entry
6363
For the passed cluster name.
6464
"""
65-
if 'clusters' not in self.content or \
66-
self.content['clusters'] is None:
65+
if self.content.get('clusters') is None:
6766
return False
6867
return name in [cluster['name']
6968
for cluster in self.content['clusters'] if 'name' in cluster]
@@ -189,7 +188,7 @@ def write_kubeconfig(self, config):
189188
except OSError as e:
190189
if e.errno != errno.EEXIST:
191190
raise KubeconfigInaccessableError(
192-
"Can't create directory for writing: {0}".format(e))
191+
"Can't create directory for writing: {0}".format(e))
193192
try:
194193
with os.fdopen(
195194
os.open(
@@ -206,34 +205,28 @@ def write_kubeconfig(self, config):
206205
class KubeconfigAppender(object):
207206
def insert_entry(self, config, key, entry):
208207
"""
209-
Insert entry into the array at content[key]
208+
Insert entry into the entries list at content[key]
210209
Overwrite an existing entry if they share the same name
211210
212211
:param config: The kubeconfig to insert an entry into
213212
:type config: Kubeconfig
214213
"""
215-
if key not in config.content or \
216-
config.content[key] is None:
217-
config.content[key] = []
218-
array = config.content[key]
219-
if not isinstance(array, list):
220-
raise KubeconfigError("Tried to insert into {0},"
221-
"which is a {1} "
222-
"not a {2}".format(key,
223-
type(array),
224-
list))
214+
config.content[key] = config.content.get(key) or []
215+
entries = config.content[key]
216+
if not isinstance(entries, list):
217+
raise KubeconfigError(f"Tried to insert into {key}, "
218+
f"which is a {type(entries)} "
219+
f"not a {list}")
225220
found = False
226-
for counter, existing_entry in enumerate(array):
227-
if "name" in existing_entry and\
228-
"name" in entry and\
229-
existing_entry["name"] == entry["name"]:
230-
array[counter] = entry
221+
for i, existing_entry in enumerate(entries):
222+
if "name" in existing_entry and "name" in entry \
223+
and existing_entry["name"] == entry["name"]:
224+
entries[i] = entry
231225
found = True
232226

233227
if not found:
234-
array.append(entry)
228+
entries.append(entry)
235229

236-
config.content[key] = array
237230
return config
238231

239232
def _make_context(self, cluster, user, alias=None):

0 commit comments

Comments
 (0)