Skip to content

Commit 54a1973

Browse files
authored
Merge pull request #12 from lilatomic/feature/crd-visualiser
Add visualiser for Kubernetes CRDs
2 parents dc7261d + 51478cd commit 54a1973

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

alpacloud/crdvis/crd.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import requests
99
import yaml
10+
from pydantic import ValidationError
1011

1112
from alpacloud.crdvis.models import CustomResourceDefinition
1213

@@ -63,10 +64,26 @@ def read_path(path: str) -> CustomResourceDefinition:
6364
except subprocess.SubprocessError as e:
6465
error_msg = f"Failed to fetch CRDs {kubectl_crd}: {e}"
6566
raise CRDReadError(error_msg)
67+
elif "://" in path:
68+
raise CRDReadError(f"Unsupported URL scheme: {path}")
6669
else:
6770
content = path
6871

6972
if not content:
7073
raise CRDReadError("Empty content")
71-
doc = yaml.safe_load(content)
72-
return CustomResourceDefinition.model_validate(doc)
74+
75+
try:
76+
doc = yaml.safe_load(content)
77+
except yaml.YAMLError as e:
78+
raise CRDReadError(f"CRD could not be loaded as YAML: {e}")
79+
80+
try:
81+
return CustomResourceDefinition.model_validate(doc)
82+
except ValidationError as e:
83+
if isinstance(doc, str) and doc.count(".") >= 3:
84+
raise CRDReadError(f"CRD content looks like a name, did you mean `kubectl://{content}`")
85+
# escaping pydantic help message
86+
# like "Input should be a valid dictionary or instance of CustomResourceDefinition [type=model_type, input_value='applications.argoproj.io', input_type=str]"
87+
# for rich
88+
msg = str(e).replace("[", r"\[")
89+
raise CRDReadError(f"CRD could not be validated: {msg}")

alpacloud/crdvis/vis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ async def _focus_to_node(self, node: TreeNode) -> None:
405405
parent.expand()
406406
parent = parent.parent
407407

408-
tree.select_node(node)
408+
tree.move_cursor(node)
409409

410410
async def action_goto(self) -> None:
411411
findbox = self.query_one(FindBox)

0 commit comments

Comments
 (0)