From b2be2ee3cd8d5c0891d3615eafa232ab5b05e8ff Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 21 Jul 2023 01:25:21 +0000 Subject: [PATCH] Bug 34335: Add a script to run Koha plugin methods from the CLI This change adds a script which allows you to run a method from a Koha plugin. Test plan: 0. Apply patch 1. Upload test plugin 2. Run the following: perl misc/run_koha_plugin.pl --class Koha::Plugin::Prosentient::CustomMethodPlugin \ --method awesome_method --params '{"msg": "this is awesome"}' 3. Note the following CLI messages: This is a custom method that you're seeing on the CLI! We got a message: this is awesome --- misc/run_koha_plugin.pl | 82 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 misc/run_koha_plugin.pl diff --git a/misc/run_koha_plugin.pl b/misc/run_koha_plugin.pl new file mode 100755 index 0000000000..155642b330 --- /dev/null +++ b/misc/run_koha_plugin.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Copyright 2023 Koha Development Team +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + + +=head1 NAME + +run_koha_plugin.pl - Script to execute a Koha plugin from the CLI + +=head1 SYNOPSIS + +./run_koha_plugin --class Koha::Plugin::Awesome --method awesome_method [--params '{"msg": "this is awesome"}'] + +=head1 DESCRIPTION + +This script will load the specified Koha plugin class and run the specified method + +=head1 OPTIONS + +=over + +=item B<--class> + +This is the class/package name of the Koha plugin in Perl module syntax + +=item B<--method> + +This is the name of the method to run. It must already be registered in Koha's +database + +=item B<--params> + +You can optionally provide JSON-encoded parameters that will be passed to the plugin method + +=back + +=cut + +use Modern::Perl; +use Getopt::Long; +use Pod::Usage; +use JSON; + +use Koha::Script; +use Koha::Plugins::Handler; + +my $class; +my $method; +my $params; +my $json_params; + +GetOptions( + "class=s" => \$class, + "method=s" => \$method, + "params=s" => \$json_params, +) || pod2usage(1); + +if ($json_params) { + my $json = JSON->new; + $params = $json->decode($json_params); +} + +if ( $class && $method ) { + my $result = Koha::Plugins::Handler->run( { class => $class, method => $method, params => $params, } ); +} else { + pod2usage(1); +} -- 2.30.2