Skip to content

Commit 3acb613

Browse files
committed
auth: auto-switch current-context
and clean up errant items
1 parent ddc6f09 commit 3acb613

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/warnet/main.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,10 @@ def auth(kube_config: str) -> None:
146146
os.environ["KUBECONFIG"] = combined_kubeconfig
147147
with open(kube_config) as file:
148148
content = yaml.safe_load(file)
149-
for elem in content:
150-
print(elem)
151-
content["clusters"][0]
152149
user = content["users"][0]
153150
user_name = user["name"]
154151
user_token = user["user"]["token"]
155-
content["contexts"][0]
152+
current_context = content["current-context"]
156153
flatten_cmd = "kubectl config view --flatten"
157154
result_flatten = subprocess.run(
158155
flatten_cmd, shell=True, check=True, capture_output=True, text=True
@@ -187,11 +184,14 @@ def auth(kube_config: str) -> None:
187184

188185
with open(current_kubeconfig) as file:
189186
contents = yaml.safe_load(file)
190-
print("\nUse the following command to switch to a new user:")
191-
print(" kubectl config use-context [user]\n")
192-
print("Available users:")
193-
for c in contents["contexts"]:
194-
print(f" {c['name']}")
187+
188+
with open(current_kubeconfig, "w") as file:
189+
contents["current-context"] = current_context
190+
yaml.safe_dump(contents, file)
191+
192+
with open(current_kubeconfig) as file:
193+
contents = yaml.safe_load(file)
194+
print(f"\nWarcli's current context is now set to: {contents['current-context']}")
195195

196196

197197
if __name__ == "__main__":

0 commit comments

Comments
 (0)