From 58bb23cbbd9db5adf471b5ffec79ec800c807a7a Mon Sep 17 00:00:00 2001 From: OTOBO user Date: Sun, 28 May 2023 18:18:26 +0200 Subject: [PATCH] Added reading from STDIN to DynamicField::Add, which enables piping config. Added example calls to README.md. --- .../Console/Command/Admin/DynamicField/Add.pm | 75 ++++++++++++------- README.md | 20 +++++ 2 files changed, 69 insertions(+), 26 deletions(-) diff --git a/Kernel/System/Console/Command/Admin/DynamicField/Add.pm b/Kernel/System/Console/Command/Admin/DynamicField/Add.pm index f3230e9..6cb1756 100644 --- a/Kernel/System/Console/Command/Admin/DynamicField/Add.pm +++ b/Kernel/System/Console/Command/Admin/DynamicField/Add.pm @@ -68,10 +68,11 @@ sub Configure { ); $Self->AddOption( Name => 'config', - Description => 'Config for dynamic field. Takes either an YAML or JSON string. See also Admin::DynamicField::ConfigDump and Admin::DynamicField::ConfigBuild.', - Required => 1, - HasValue => 1, - ValueRegex => qr/.*/smx, + Description => + 'Config for dynamic field. Takes either an YAML or JSON string. See also Admin::DynamicField::ConfigDump and Admin::DynamicField::ConfigBuild.', + Required => 0, + HasValue => 1, + ValueRegex => qr/.*/smx, ); $Self->AddOption( Name => 'namespace', @@ -82,10 +83,11 @@ sub Configure { ); $Self->AddOption( 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.', - Required => 0, - HasValue => 1, - ValueRegex => qr/\d+/smx, + 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.', + Required => 0, + HasValue => 1, + ValueRegex => qr/\d+/smx, ); $Self->AddOption( Name => 'reorder', @@ -101,6 +103,12 @@ sub Configure { HasValue => 1, ValueRegex => qr/\d/smx, ); + $Self->AddOption( + Name => 'stdin', + Description => 'Read config from STDIN.', + Required => 0, + HasValue => 0, + ); return; } @@ -113,17 +121,32 @@ sub Run { # get necessary objects my $DynamicFieldObject = $Kernel::OM->Get('Kernel::System::DynamicField'); - # check field order + # check field order my $DynamicFieldList = $DynamicFieldObject->DynamicFieldListGet( 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 - # trying to parse as yaml and as json - if both fails, not valid - my $PlainConfig = $Self->GetOption('config'); + my $PlainConfig; + # either fetch config from stdin or param + if ( $Self->GetOption('stdin') ) { + $Self->Print("Reading config from stdin...\n"); + while ( my $Line = ) { ## no critic qw(ProhibitExplicitStdin) + $PlainConfig .= $Line; + } + } + elsif ( $Self->GetOption('config') ) { + $PlainConfig = $Self->GetOption('config'); + } + else { + $Self->Print("Either param config or param stdin has to be provided!"); + $Self->ExitCodeError(); + } + + # trying to parse as yaml and as json - if both fails, not valid $Self->Print("Trying to parse config as YAML...\n"); my $ConfigHashRef = $Kernel::OM->Get('Kernel::System::YAML')->Load( Data => $PlainConfig, @@ -138,26 +161,26 @@ sub Run { $Self->Print("Parsing config as JSON failed also. Aborting.\n"); return $Self->ExitCodeError(); } - }; + } # check namespace my $Namespace = $Self->GetOption('namespace'); - my $Name = $Self->GetOption('name'); - if ( $Namespace ) { + my $Name = $Self->GetOption('name'); + if ($Namespace) { $Name = "$Namespace-$Name"; - } + } # add queue my $Success = $Kernel::OM->Get('Kernel::System::DynamicField')->DynamicFieldAdd( - Name => $Name, - Label => $Self->GetOption('label'), - FieldOrder => $FieldOrder, - FieldType => $Self->GetOption('field-type'), - ObjectType => $Self->GetOption('object-type'), - Config => $ConfigHashRef, - Reorder => $Self->GetOption('reorder') || 1, - ValidID => $Self->GetOption('valid') || 1, - UserID => 1, + Name => $Name, + Label => $Self->GetOption('label'), + FieldOrder => $FieldOrder, + FieldType => $Self->GetOption('field-type'), + ObjectType => $Self->GetOption('object-type'), + Config => $ConfigHashRef, + Reorder => $Self->GetOption('reorder') || 1, + ValidID => $Self->GetOption('valid') || 1, + UserID => 1, ); # error handling diff --git a/README.md b/README.md index 2be4dce..21e97b1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,26 @@ # consolecommands 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: - Kernel/System/Console/Command/Admin/DynamicField/Add.pm - Kernel/System/Console/Command/Admin/CustomerUser/List.pm