forked from alfredodeza/python-cli-example
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathframework.py
More file actions
54 lines (46 loc) · 1.23 KB
/
framework.py
File metadata and controls
54 lines (46 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import subprocess
import shlex
import json
import click
def run_command(command):
"""
Runs a shell command and returns the output.
Args:
command (str): The command to run.
Returns:
str: The output of the command.
"""
cmd = shlex.split(command)
output = subprocess.check_output(cmd)
return output
def run_lsblk(device):
"""
Runs lsblk command and produces JSON output.
Args:
device (str): The device name.
Returns:
dict: The device information.
"""
command = 'lsblk -J -o NAME,SIZE,TYPE,MOUNTPOINT'
output = run_command(command)
devices = json.loads(output)['blockdevices']
for parent in devices:
if parent['name'] == device:
return parent
for child in parent.get('children', []):
if child['name'] == device:
return child
return None
@click.command()
@click.option('--verbose', '-v', is_flag=True)
@click.argument('device')
def main(device, verbose):
"""
Main function to run the lsblk command.
Args:
device (str): The device name.
verbose (bool): Verbose flag.
"""
print(f"Device: {device}")
print(f"Verbose: {verbose}")
print(f"{run_lsblk(device)}")