Skip to content

Commit 8a7788b

Browse files
committed
provide automatic login when unauthenticated
1 parent a8a7970 commit 8a7788b

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/edge_containers_cli/cmds/argo_commands.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
Relies on the Helm class for deployment aspects.
55
"""
66

7+
import os
78
import re
89
import webbrowser
910
from datetime import datetime
1011
from pathlib import Path
1112
from time import sleep
1213

1314
import polars
15+
import typer
1416
from ruamel.yaml import YAML
1517

1618
from edge_containers_cli import globals
@@ -20,7 +22,7 @@
2022
ServicesDataFrame,
2123
ServicesSchema,
2224
)
23-
from edge_containers_cli.definitions import ECContext
25+
from edge_containers_cli.definitions import ENV, ECContext
2426
from edge_containers_cli.git import check_exists, del_key, set_value
2527
from edge_containers_cli.logging import log
2628
from edge_containers_cli.shell import ShellError, shell
@@ -279,12 +281,26 @@ def _validate_target(self):
279281
"""
280282
Verify we have a good namespace that exists in the cluster
281283
"""
284+
retries = 2
285+
282286
cmd = f"argocd app get {self._target}"
283287
try:
284288
shell.run_command(cmd, error_OK=False)
285289
except ShellError as e:
286-
if "code = Unauthenticated" in str(e):
287-
raise CommandError("Not authenticated to argocd server") from e
290+
if "Unauthenticated" in str(e) or "unspecified" in str(e):
291+
retries -= 1
292+
login = os.environ.get(ENV.login.value)
293+
if retries <= 0 or not login:
294+
raise CommandError("Not authenticated to argocd server") from e
295+
296+
# try to log in
297+
if not login or not typer.confirm("Login to ArgoCD?", default=True):
298+
raise typer.Abort() from e
299+
shell.run_command(login, error_OK=False, skip_on_dryrun=True)
300+
301+
# retry validation
302+
self._validate_target()
303+
288304
elif "code = PermissionDenied" in str(e):
289305
raise CommandError(f"Target '{self._target}' not found") from e
290306
else:

src/edge_containers_cli/definitions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ECLogLevels(str, Enum):
1919
class ENV(str, Enum):
2020
repo = "EC_SERVICES_REPO"
2121
target = "EC_TARGET"
22+
login = "EC_LOGIN"
2223
backend = "EC_CLI_BACKEND"
2324
verbose = "EC_VERBOSE"
2425
dryrun = "EC_DRYRUN"

0 commit comments

Comments
 (0)