Skip to content

Commit

Permalink
[crmsh-4.6] Dev: ui_configure: Add completer for 'configure schema' c…
Browse files Browse the repository at this point in the history
…ommand (#1678)

To complete existing pacemaker-*.rng files under /usr/share/pacemaker
directory
backport #1677
  • Loading branch information
liangxin1300 authored Feb 8, 2025
2 parents 0b194a6 + c556be1 commit ffa80c5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
17 changes: 13 additions & 4 deletions crmsh/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,7 @@ def complete(self, context, args):
'''
ret = []
if self.completer is not None:
if sort_completion_inst is not None and sort_completion_inst.value == 0:
# Restore the original value of rl_sort_completion_matches
# before calling the completer again
sort_completion_inst.value = orig_sort_completion_value
disable_custom_sort_order()
specs = inspect.getfullargspec(self.completer)
if 'context' in specs.args:
ret = self.completer([self.name] + args, context)
Expand Down Expand Up @@ -607,6 +604,18 @@ def _check_args(fn, expected):
": Expected method with signature " + repr(expected))


def enable_custom_sort_order():
if sort_completion_inst is not None:
sort_completion_inst.value = 0


def disable_custom_sort_order():
if sort_completion_inst is not None and sort_completion_inst.value == 0:
# Restore the original value of rl_sort_completion_matches
# before calling the completer again
sort_completion_inst.value = orig_sort_completion_value


readline_path = ctypes.util.find_library('readline')
sort_completion_inst = None
if readline_path:
Expand Down
24 changes: 21 additions & 3 deletions crmsh/ui_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# Copyright (C) 2013 Kristoffer Gronlund <[email protected]>
# See COPYING for license information.

import os
import re
import time
from packaging import version
from . import command
from . import completers as compl
from . import config
Expand Down Expand Up @@ -191,6 +193,23 @@ def ra_agent_for_cpt(cpt):
return agent


def schema_completer(args):
complete_results = []

directory = '/usr/share/pacemaker/'
version_pattern = re.compile(r'^pacemaker-(\d+\.\d+)')
for filename in os.listdir(directory):
if version_pattern.match(filename):
complete_results.append(filename.strip('.rng'))

if complete_results:
# Sort files using packaging.version
complete_results.sort(key=lambda x: version.parse(version_pattern.match(x).group(1)))
command.enable_custom_sort_order()

return complete_results


class CompletionHelp(object):
'''
Print some help on whatever last word in the line.
Expand Down Expand Up @@ -231,9 +250,7 @@ def _prim_params_completer(agent, args):
return []
elif '=' in completing:
return []
if command.sort_completion_inst is not None:
# Set the value to 0 to use the costum sort order
command.sort_completion_inst.value = 0
command.enable_custom_sort_order()
return utils.filter_keys(agent.params(), args)


Expand Down Expand Up @@ -952,6 +969,7 @@ def do_upgrade(self, context, force=None):
return cib_factory.upgrade_validate_with(force=config.core.force or force)

@command.skill_level('administrator')
@command.completers(schema_completer)
def do_schema(self, context, schema_st=None):
"usage: schema [<schema>]"
if not schema_st:
Expand Down

0 comments on commit ffa80c5

Please sign in to comment.