oks-cli 1.14__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
oks_cli/__init__.py ADDED
File without changes
oks_cli/cache.py ADDED
@@ -0,0 +1,63 @@
1
+ import click
2
+ from .utils import clear_cache, find_project_id_by_name, find_cluster_id_by_name, get_all_cache, get_expiration_date, ctx_update, login_profile, profile_completer
3
+ import prettytable
4
+
5
+ # DEFINE THE CACHE COMMAND GROUP
6
+ @click.group(help="Cache related commands.")
7
+ @click.option('--project-name', '-p', required = False, help="Project Name")
8
+ @click.option('--cluster-name', '-c', required = False, help="Cluster Name")
9
+ @click.option("--profile", help="Configuration profile to use", shell_complete=profile_completer)
10
+ @click.pass_context
11
+ def cache(ctx, project_name, cluster_name, profile):
12
+ """CLI command group for cache-related operations."""
13
+ ctx_update(ctx, project_name, cluster_name, profile)
14
+
15
+ @cache.command('clear', help="Clear cache")
16
+ @click.option('--force', is_flag=True, help="Force deletion without confirmation")
17
+ def delete_cache(force):
18
+ """Clear all cached data, optionally without confirmation."""
19
+ if force or click.confirm("Are you sure you want to clear all cache?", abort=True):
20
+ clear_cache()
21
+
22
+ @cache.command('kubeconfigs', help="List cached kubeconfigs")
23
+ @click.option('--project-name', '-p', required=False, help="Project Name")
24
+ @click.option('--cluster-name', '-c', required=False, help="Cluster Name")
25
+ @click.option('--plain', is_flag=True, help="Plain table format")
26
+ @click.option('--msword', is_flag=True, help="Microsoft Word table format")
27
+ @click.option('--profile', help="Configuration profile to use", shell_complete=profile_completer)
28
+ @click.pass_context
29
+ def list_kubeconfigs(ctx, project_name, cluster_name, plain, msword, profile):
30
+ """Display cached kubeconfigs with expiration dates in table format."""
31
+ project_name, cluster_name, profile = ctx_update(ctx, project_name, cluster_name, profile)
32
+ login_profile(profile)
33
+
34
+ project_id = find_project_id_by_name(project_name)
35
+ cluster_id = find_cluster_id_by_name(project_id, cluster_name)
36
+
37
+ result = get_all_cache(project_id, cluster_id, "kubeconfig")
38
+
39
+ table = prettytable.PrettyTable()
40
+ table.field_names = ["user", "group", "expiration date"]
41
+
42
+ if plain:
43
+ table.set_style(prettytable.PLAIN_COLUMNS)
44
+
45
+ if msword:
46
+ table.set_style(prettytable.MSWORD_FRIENDLY)
47
+
48
+ for element in result:
49
+ kubeconfig = None
50
+ user = click.style(element['user'], bold=True)
51
+ group = click.style(element['group'], bold=True)
52
+
53
+ if element.get("cache_path"):
54
+ with open(element.get("cache_path")) as f:
55
+ kubeconfig = f.read()
56
+
57
+ if kubeconfig:
58
+ exp = get_expiration_date(kubeconfig)
59
+ row = user, group, exp
60
+
61
+ table.add_row(row)
62
+
63
+ click.echo(table)