Skip to content

Commit

Permalink
feat: --columns flag
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt authored and lcaflc committed Dec 2, 2024
1 parent 7cd0bdb commit 2397102
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/pvecontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def add_table_related_arguments(parser, columns, default_sort):
filter_type = lambda x: next(filter_type_generator)(x)
parser.add_argument('--sort-by', action='store', help="Key used to sort items", default=default_sort, choices=columns)
parser.add_argument('--filter', action='append', nargs=2, type=filter_type, metavar=('COLUMN', 'REGEXP'), help="Regexp to filter items", default=[])
parser.add_argument('--columns', action='store', nargs='+', help="", default=columns, choices=columns)

def _parser():
## FIXME
Expand Down Expand Up @@ -115,6 +116,15 @@ def main():
# get cli arguments
args = _parser()

if hasattr(args, 'columns'):
if not args.sort_by is None and not args.sort_by in args.columns:
sys.stderr.write(f"error: cannot sort by column '{args.sort_by}' because it's not included in the --columns flag.\n")
sys.exit(1)
for (key, value) in args.filter:
if not key in args.columns:
sys.stderr.write(f"error: cannot filter on the column '{key}' because it's not included in the --columns flag.\n")
sys.exit(1)

# configure logging
logging.basicConfig(encoding='utf-8', level=logging.DEBUG if args.debug else logging.INFO)
logging.debug("Arguments: %s" % args)
Expand Down
4 changes: 2 additions & 2 deletions src/pvecontrol/actions/node.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import time

from pvecontrol.node import NodeStatus, columns
from pvecontrol.node import NodeStatus
from pvecontrol.vm import VmStatus
from pvecontrol.utils import (
filter_keys, print_tableoutput, print_task, print_taskstatus
Expand All @@ -10,7 +10,7 @@

def action_nodelist(proxmox, args):
"""List proxmox nodes in the cluster using proxmoxer api"""
print_tableoutput(proxmox.nodes, columns, sortby=args.sort_by, filters=args.filter)
print_tableoutput(proxmox.nodes, columns=args.columns, sortby=args.sort_by, filters=args.filter)

def action_nodeevacuate(proxmox, args):
"""Evacuate a node by migrating all it's VM out"""
Expand Down
3 changes: 1 addition & 2 deletions src/pvecontrol/actions/task.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from pvecontrol.utils import print_task, print_tableoutput, filter_keys
from pvecontrol.task import columns


def action_tasklist(proxmox, args):
print_tableoutput(proxmox.tasks, columns, sortby=args.sort_by, filters=args.filter)
print_tableoutput(proxmox.tasks, columns=args.columns, sortby=args.sort_by, filters=args.filter)

def action_taskget(proxmox, args):
print_task(proxmox, args.upid, args.follow, args.wait, show_logs = True)
3 changes: 1 addition & 2 deletions src/pvecontrol/actions/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from pvecontrol.utils import (
print_task, print_taskstatus, print_tableoutput, filter_keys
)
from pvecontrol.vm import columns


def _get_vm(proxmox, vmid):
Expand Down Expand Up @@ -65,4 +64,4 @@ def action_vmmigrate(proxmox, args):
def action_vmlist(proxmox, args):
"""List VMs in the Proxmox Cluster"""
vms = proxmox.vms()
print_tableoutput(vms, columns, sortby=args.sort_by, filters=args.filter)
print_tableoutput(vms, columns=args.columns, sortby=args.sort_by, filters=args.filter)

0 comments on commit 2397102

Please sign in to comment.