193 lines
5.1 KiB
Python
193 lines
5.1 KiB
Python
import enum
|
|
import logging
|
|
from enum import Enum
|
|
from pprint import pformat, pprint
|
|
from types import SimpleNamespace
|
|
|
|
from iam.framework import (empty, get_app_logger, iterate_any, open_yaml,
|
|
prune, to_csv, to_json, to_yaml)
|
|
from rich import box
|
|
from rich.console import Console
|
|
from rich.pretty import pprint
|
|
from rich.prompt import Prompt
|
|
from rich.table import Table
|
|
|
|
from .lib.cli_views import ViewItem, ViewKind, ViewList
|
|
|
|
# Iam Implementations
|
|
# ===============================
|
|
|
|
|
|
MISSING_PKG = []
|
|
|
|
try:
|
|
import hcl2
|
|
except ModuleNotFoundError:
|
|
MISSING_PKG.append("hcl2")
|
|
|
|
try:
|
|
import yaml
|
|
except ModuleNotFoundError:
|
|
MISSING_PKG.append("yaml")
|
|
|
|
|
|
# Main views
|
|
# ======================
|
|
|
|
|
|
# def view_list(data, columns, title=None, fmt=None, **kwargs):
|
|
# "Show list view"
|
|
|
|
# fmt = fmt or OutputFormat.DEFAULT
|
|
# ret = None
|
|
# _kwargs = {
|
|
# key[len(fmt.value) + 1 :]: val
|
|
# for key, val in kwargs.items()
|
|
# if key.startswith(fmt.value)
|
|
# }
|
|
|
|
# # print ("YOOO", fmt, OutputFormat.YAML)
|
|
# if fmt == OutputFormat.YAML:
|
|
# ret = to_yaml(restructure_list_to_dict(data, columns))
|
|
|
|
# elif fmt == OutputFormat.JSON:
|
|
# ret = to_json(restructure_list_to_dict(data, columns), nice=True)
|
|
|
|
# elif fmt == OutputFormat.PYTHON:
|
|
# ret = pformat(restructure_list_to_dict(data, columns), indent=2)
|
|
|
|
# elif fmt == OutputFormat.CSV:
|
|
# ret = to_csv(restructure_list_to_csv(data, columns))
|
|
|
|
# elif fmt == OutputFormat.ENV:
|
|
# ret = to_vars(restructure_list_to_env(data, columns), export=True)
|
|
|
|
# elif fmt == OutputFormat.VARS:
|
|
# ret = to_vars(restructure_list_to_env(data, columns))
|
|
|
|
# elif fmt == OutputFormat.RICH_TABLE:
|
|
# ret = to_rich_table(data, columns=columns, title=title, **_kwargs)
|
|
|
|
# else:
|
|
# raise Exception(f"Unmanagable format: {fmt}")
|
|
|
|
# console.print(ret)
|
|
|
|
|
|
# def view_show(data, columns=None, title=None):
|
|
# "Show single view"
|
|
|
|
# ret = None
|
|
# _kwargs = {
|
|
# key[len(fmt.value) + 1 :]: val
|
|
# for key, val in kwargs.items()
|
|
# if key.startswith(fmt.value)
|
|
# }
|
|
|
|
# # print ("YOOO", fmt, OutputFormat.YAML)
|
|
# if fmt == OutputFormat.YAML:
|
|
# ret = to_yaml(data)
|
|
|
|
# elif fmt == OutputFormat.JSON:
|
|
# ret = to_json(data, indent=2)
|
|
|
|
# elif fmt == OutputFormat.PYTHON:
|
|
# ret = pformat(data, indent=2)
|
|
|
|
# elif fmt == OutputFormat.CSV:
|
|
# ret = to_csv(data)
|
|
|
|
# elif fmt == OutputFormat.RICH_TABLE:
|
|
# data = list(zip(*data))
|
|
# data.insert(0, ["Field", "Value"])
|
|
# ret = to_rich_table(data, **_kwargs)
|
|
|
|
# else:
|
|
# raise Exception(f"Unmanagable format: {fmt}")
|
|
|
|
# return ret
|
|
|
|
|
|
# # DEPRECATED
|
|
# # ======================
|
|
|
|
|
|
# def output_list(payload, fmt=None, **kwargs):
|
|
# "Render output format"
|
|
# assert False, "DEPRECATED"
|
|
# fmt = fmt or OutputFormat.YAML
|
|
|
|
# # Requested format
|
|
# # payload = [
|
|
# # ["Header1", "Header2"], # First line is always headers
|
|
# # [["val1", "val2"]], # First row
|
|
# # [["val1", "val2"]], # Second row, etc ...
|
|
# # ]
|
|
|
|
# ret = None
|
|
# # _kwargs = {key.lstrip(f"{fmt.value}_"): val for key, val in kwargs.items() if key.startswith(fmt.value)}
|
|
# _kwargs = {
|
|
# key[len(fmt.value) + 1 :]: val
|
|
# for key, val in kwargs.items()
|
|
# if key.startswith(fmt.value)
|
|
# }
|
|
|
|
# # print ("YOOO", fmt, OutputFormat.YAML)
|
|
# if fmt == OutputFormat.YAML:
|
|
# ret = to_yaml(payload)
|
|
# elif fmt == OutputFormat.JSON:
|
|
# ret = to_json(payload, indent=2)
|
|
# elif fmt == OutputFormat.PYTHON:
|
|
# ret = pformat(payload, indent=2)
|
|
# elif fmt == OutputFormat.CSV:
|
|
# ret = to_csv(payload)
|
|
# elif fmt == OutputFormat.RICH_TABLE:
|
|
# # pprint (kwargs)
|
|
# # pprint (_kwargs)
|
|
# return to_rich_table(payload, **_kwargs)
|
|
# else:
|
|
# raise Exception(f"Unmanagable format list: {fmt}")
|
|
|
|
# assert isinstance(ret, str)
|
|
# return ret
|
|
|
|
|
|
# def output_show(payload, fmt=None, **kwargs):
|
|
# "Show one item"
|
|
# assert False, "DEPRECATED"
|
|
|
|
# # Requested format
|
|
# # payload = [
|
|
# # ["Header1", "Header2"], # First line is always headers
|
|
# # [["val1", "val2"]], # First row ONLY
|
|
# # [["val1", "val2"]], # Second row, etc ...
|
|
# # ]
|
|
|
|
# ret = None
|
|
# _kwargs = {
|
|
# key[len(fmt.value) + 1 :]: val
|
|
# for key, val in kwargs.items()
|
|
# if key.startswith(fmt.value)
|
|
# }
|
|
|
|
# # print ("YOOO", fmt, OutputFormat.YAML)
|
|
# if fmt == OutputFormat.YAML:
|
|
# ret = to_yaml(payload)
|
|
|
|
# elif fmt == OutputFormat.JSON:
|
|
# ret = to_json(payload, indent=2)
|
|
# elif fmt == OutputFormat.PYTHON:
|
|
# ret = pformat(payload, indent=2)
|
|
# elif fmt == OutputFormat.CSV:
|
|
# ret = to_csv(payload)
|
|
# elif fmt == OutputFormat.RICH_TABLE:
|
|
# # Return data
|
|
# payload = list(zip(*payload))
|
|
# payload.insert(0, ["Field", "Value"])
|
|
# ret = to_rich_table(payload, **_kwargs)
|
|
|
|
# else:
|
|
# raise Exception(f"Unmanagable format show: {fmt}")
|
|
|
|
# return ret
|