Added reading from STDIN to DynamicField::Add, which enables piping config.

Added example calls to README.md.
This commit is contained in:
OTOBO user 2023-05-28 18:18:26 +02:00
parent be02d11c48
commit 58bb23cbbd
2 changed files with 69 additions and 26 deletions

View File

@ -68,10 +68,11 @@ sub Configure {
); );
$Self->AddOption( $Self->AddOption(
Name => 'config', Name => 'config',
Description => 'Config for dynamic field. Takes either an YAML or JSON string. See also Admin::DynamicField::ConfigDump and Admin::DynamicField::ConfigBuild.', Description =>
Required => 1, 'Config for dynamic field. Takes either an YAML or JSON string. See also Admin::DynamicField::ConfigDump and Admin::DynamicField::ConfigBuild.',
HasValue => 1, Required => 0,
ValueRegex => qr/.*/smx, HasValue => 1,
ValueRegex => qr/.*/smx,
); );
$Self->AddOption( $Self->AddOption(
Name => 'namespace', Name => 'namespace',
@ -82,10 +83,11 @@ sub Configure {
); );
$Self->AddOption( $Self->AddOption(
Name => 'field-order', Name => 'field-order',
Description => 'Field order to place the field at. Defaults to hightest order plus 1. Per default, other fields are reordered if an occupied order number is chosen.', Description =>
Required => 0, 'Field order to place the field at. Defaults to hightest order plus 1. Per default, other fields are reordered if an occupied order number is chosen.',
HasValue => 1, Required => 0,
ValueRegex => qr/\d+/smx, HasValue => 1,
ValueRegex => qr/\d+/smx,
); );
$Self->AddOption( $Self->AddOption(
Name => 'reorder', Name => 'reorder',
@ -101,6 +103,12 @@ sub Configure {
HasValue => 1, HasValue => 1,
ValueRegex => qr/\d/smx, ValueRegex => qr/\d/smx,
); );
$Self->AddOption(
Name => 'stdin',
Description => 'Read config from STDIN.',
Required => 0,
HasValue => 0,
);
return; return;
} }
@ -116,14 +124,29 @@ sub Run {
# check field order # check field order
my $DynamicFieldList = $DynamicFieldObject->DynamicFieldListGet( my $DynamicFieldList = $DynamicFieldObject->DynamicFieldListGet(
ObjectType => 'All', ObjectType => 'All',
Valid => 0, Valid => 0,
); );
my $FieldOrder = $Self->GetOption('field-order') || max map { $_->{FieldOrder} } $DynamicFieldList->@*; my $FieldOrder = $Self->GetOption('field-order') || max map { $_->{FieldOrder} } $DynamicFieldList->@*;
# check config string # check config string
# trying to parse as yaml and as json - if both fails, not valid my $PlainConfig;
my $PlainConfig = $Self->GetOption('config');
# either fetch config from stdin or param
if ( $Self->GetOption('stdin') ) {
$Self->Print("<green>Reading config from stdin...</green>\n");
while ( my $Line = <STDIN>) { ## no critic qw(ProhibitExplicitStdin)
$PlainConfig .= $Line;
}
}
elsif ( $Self->GetOption('config') ) {
$PlainConfig = $Self->GetOption('config');
}
else {
$Self->Print("<red>Either param config or param stdin has to be provided!</red>");
$Self->ExitCodeError();
}
# trying to parse as yaml and as json - if both fails, not valid
$Self->Print("<yellow>Trying to parse config as YAML...</yellow>\n"); $Self->Print("<yellow>Trying to parse config as YAML...</yellow>\n");
my $ConfigHashRef = $Kernel::OM->Get('Kernel::System::YAML')->Load( my $ConfigHashRef = $Kernel::OM->Get('Kernel::System::YAML')->Load(
Data => $PlainConfig, Data => $PlainConfig,
@ -138,26 +161,26 @@ sub Run {
$Self->Print("<red>Parsing config as JSON failed also. Aborting.</red>\n"); $Self->Print("<red>Parsing config as JSON failed also. Aborting.</red>\n");
return $Self->ExitCodeError(); return $Self->ExitCodeError();
} }
}; }
# check namespace # check namespace
my $Namespace = $Self->GetOption('namespace'); my $Namespace = $Self->GetOption('namespace');
my $Name = $Self->GetOption('name'); my $Name = $Self->GetOption('name');
if ( $Namespace ) { if ($Namespace) {
$Name = "$Namespace-$Name"; $Name = "$Namespace-$Name";
} }
# add queue # add queue
my $Success = $Kernel::OM->Get('Kernel::System::DynamicField')->DynamicFieldAdd( my $Success = $Kernel::OM->Get('Kernel::System::DynamicField')->DynamicFieldAdd(
Name => $Name, Name => $Name,
Label => $Self->GetOption('label'), Label => $Self->GetOption('label'),
FieldOrder => $FieldOrder, FieldOrder => $FieldOrder,
FieldType => $Self->GetOption('field-type'), FieldType => $Self->GetOption('field-type'),
ObjectType => $Self->GetOption('object-type'), ObjectType => $Self->GetOption('object-type'),
Config => $ConfigHashRef, Config => $ConfigHashRef,
Reorder => $Self->GetOption('reorder') || 1, Reorder => $Self->GetOption('reorder') || 1,
ValidID => $Self->GetOption('valid') || 1, ValidID => $Self->GetOption('valid') || 1,
UserID => 1, UserID => 1,
); );
# error handling # error handling

View File

@ -1,6 +1,26 @@
# consolecommands # consolecommands
Custom Console Commands for OTOBO - intended to make their way into standard or die in silence. Custom Console Commands for OTOBO - intended to make their way into standard or die in silence.
For convenience, I will try to provide example calls for commands. Feel free to suggest usability improvements or to introduce them yourselves.
```
~$ bin/otobo.Console.pl Admin::DynamicField::Add --name YourFieldName --label SomeLabel --valid 1 --object-type YourObjectType --field-type YourFieldType --config '---
DefaultValue: 'SomeDefaultValue'
Link: 'https://some.url.org'
LinkPreview: 'https://some.url.org/preview'
RegExList:
- ErrorMessage: 'some meaningful error message'
Value: '\d+'
- ErrorMessage: 'some other meaningful error message'
Value: '\w+'
Tooltip: 'Some helpful tooltip'
'
```
```
~$ mariadb -u otobo -p --execute="SELECT config FROM otobo.dynamic_field WHERE name = 'YourFieldName';" --silent --raw | bin/otobo.Console.pl Admin::DynamicField::Add --name YourNewFieldName --label SomeLabel --valid 1 --object-type YourObjectType --field-type YourFieldType --stdin
```
Currently included are the following commands: Currently included are the following commands:
- Kernel/System/Console/Command/Admin/DynamicField/Add.pm - Kernel/System/Console/Command/Admin/DynamicField/Add.pm
- Kernel/System/Console/Command/Admin/CustomerUser/List.pm - Kernel/System/Console/Command/Admin/CustomerUser/List.pm