Skip to content

Commit

Permalink
Merge pull request #488 from mplsgrant/auto-switch-current-context
Browse files Browse the repository at this point in the history
auth: auto-switch current-context
  • Loading branch information
mplsgrant authored Aug 24, 2024
2 parents ddc6f09 + 3acb613 commit a3b5ae9
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/warnet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,10 @@ def auth(kube_config: str) -> None:
os.environ["KUBECONFIG"] = combined_kubeconfig
with open(kube_config) as file:
content = yaml.safe_load(file)
for elem in content:
print(elem)
content["clusters"][0]
user = content["users"][0]
user_name = user["name"]
user_token = user["user"]["token"]
content["contexts"][0]
current_context = content["current-context"]
flatten_cmd = "kubectl config view --flatten"
result_flatten = subprocess.run(
flatten_cmd, shell=True, check=True, capture_output=True, text=True
Expand Down Expand Up @@ -187,11 +184,14 @@ def auth(kube_config: str) -> None:

with open(current_kubeconfig) as file:
contents = yaml.safe_load(file)
print("\nUse the following command to switch to a new user:")
print(" kubectl config use-context [user]\n")
print("Available users:")
for c in contents["contexts"]:
print(f" {c['name']}")

with open(current_kubeconfig, "w") as file:
contents["current-context"] = current_context
yaml.safe_dump(contents, file)

with open(current_kubeconfig) as file:
contents = yaml.safe_load(file)
print(f"\nWarcli's current context is now set to: {contents['current-context']}")


if __name__ == "__main__":
Expand Down

0 comments on commit a3b5ae9

Please sign in to comment.