Skip to content

Commit

Permalink
parse snap info as yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
addyess committed Jan 3, 2024
1 parent abe395e commit 38ccf1f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
10 changes: 4 additions & 6 deletions charms/kubernetes_snaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,12 +671,10 @@ def is_channel_available(snap_name: str, target_channel: str) -> bool:
"""
cmd = ["snap", "info", snap_name]
result = check_output(cmd)
output = result.decode().strip()
for line in output.splitlines():
if target_channel in line:
match = re.search(r"\s+\(\d+\)\s+", line)
return bool(match)
return False
output = yaml.safe_load(result.decode())
channels = output.get("channels") or ()
target = channels.get(target_channel) or None
return target and target != "--"


def is_snap_installed(snap_name) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_kubernetes_snaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_is_snap_available(subprocess_check_output):
name: my-snap
publisher: Canonical✓
channels:
latest/stable: -
latest/stable: --
1.29/stable: 1.29.0 2024-01-03 (22606) 12MB -
"""
subprocess_check_output.return_value = snap_info.encode()
Expand Down

0 comments on commit 38ccf1f

Please sign in to comment.