Add autocompleter for CLI command show

This commit is contained in:
Benjamin Renard 2020-06-15 11:26:57 +02:00
parent 559f9d9475
commit 4ba584e8c6
2 changed files with 92 additions and 2 deletions

View file

@ -600,6 +600,41 @@ class LScli extends LSlog_staticLoggerClass {
return self :: autocomplete_opts($types, $prefix, false); return self :: autocomplete_opts($types, $prefix, false);
} }
/**
* Autocomplete LSobject DN option
*
* @param[in] $objType string LSobject type
* @param[in] $prefix string Option prefix (optional, default=empty string)
*
* @retval array List of available options
**/
public static function autocomplete_LSobject_dn($objType, $prefix='') {
if (!$prefix || !LSsession ::loadLSobject($objType, false))
return array();
self :: need_ldap_con();
$rdn_attr = LSconfig :: get("LSobjects.$objType.rdn");
if (!$rdn_attr || strlen($prefix) < (strlen($rdn_attr)+1) || substr($prefix, 0, (strlen($rdn_attr)+1)) != "$rdn_attr=")
return array();
// Split prefix by comma to keep only RDN
$prefix_parts = explode(',', $prefix);
$prefix_rdn = $prefix_parts[0];
// Search objects
$obj = new $objType();
$objs = $obj -> listObjectsName("($prefix_rdn*)");
if (is_array($objs)) {
$dns = array_keys($objs);
self :: log_debug("Matching $objType DNs with prefix '$prefix_rdn': ".implode(', ', $dns));
// If prefix have been reduced for the search, use self :: autocomplete_opts() to keep only
// full match
if ($prefix_rdn != $prefix)
return self :: autocomplete_opts($dns, $prefix);
return $dns;
}
return array();
}
} }
/* /*

View file

@ -1836,7 +1836,7 @@ class LSldapObject extends LSlog_staticLoggerClass {
* Check if object type have a specified attribute * Check if object type have a specified attribute
* *
* @param[in] $attr_name string The attribute name * @param[in] $attr_name string The attribute name
* *
* @teval boolean True if object type have a attribute of this name, False otherwise * @teval boolean True if object type have a attribute of this name, False otherwise
*/ */
public static function hasAttr($attr_name) { public static function hasAttr($attr_name) {
@ -1945,6 +1945,58 @@ class LSldapObject extends LSlog_staticLoggerClass {
return true; return true;
} }
/**
* Args autocompleter for CLI show search
*
* @param[in] $command_args array List of already typed words of the command
* @param[in] $comp_word_num int The command word number to autocomplete
* @param[in] $comp_word string The command word to autocomplete state
* @param[in] $opts array List of global available options
*
* @retval array List of available options for the word to autocomplete
**/
public static function cli_show_args_autocompleter($command_args, $comp_word_num, $comp_word, $opts) {
$opts = array_merge($opts, array ('-r', '--raw-values'));
// Handle positional args
$objType = null;
$objType_arg_num = null;
$dn = null;
$dn_arg_num = null;
for ($i=0; $i < count($command_args); $i++) {
if (!in_array($command_args[$i], $opts)) {
// If object type not defined
if (is_null($objType)) {
// Check object type exists
$objTypes = LScli :: autocomplete_LSobject_types($command_args[$i]);
// Load it if exist and not trying to complete it
if (in_array($command_args[$i], $objTypes) && $i != $comp_word_num) {
LSsession :: loadLSobject($command_args[$i], false);
}
// Defined it
$objType = $command_args[$i];
$objType_arg_num = $i;
}
elseif (is_null($dn)) {
$dn = $command_args[$i];
$dn_arg_num = $i;
}
}
}
// If objType not already choiced (or currently autocomplete), add LSobject types to available options
if (!$objType || $objType_arg_num == $comp_word_num)
$opts = array_merge($opts, LScli :: autocomplete_LSobject_types($comp_word));
// If dn not alreay choiced (or currently autocomplete), try autocomplete it
elseif (!$dn || $dn_arg_num == $comp_word_num)
$opts = array_merge($opts, LScli :: autocomplete_LSobject_dn($objType, $comp_word));
return LScli :: autocomplete_opts($opts, $comp_word);
}
/** /**
* CLI helper to show the object info * CLI helper to show the object info
* *
@ -2605,7 +2657,10 @@ LScli :: add_command(
'show', 'show',
array('LSldapObject', 'cli_show'), array('LSldapObject', 'cli_show'),
'Show an LSobject', 'Show an LSobject',
'[object type] [dn] [-r|--raw-values]' '[object type] [dn] [-r|--raw-values]',
null,
true,
array('LSldapObject', 'cli_show_args_autocompleter')
); );
LScli :: add_command( LScli :: add_command(