From 809a7e29516ffb8c8c35b8e434682d6eebf317c4 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 13 Sep 2022 21:38:44 -0400 Subject: [PATCH] fix(tools): add anvil-access-module switch to output $anvil->data --- tools/anvil-access-module | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/tools/anvil-access-module b/tools/anvil-access-module index 52b0cc31..52e92d85 100755 --- a/tools/anvil-access-module +++ b/tools/anvil-access-module @@ -18,14 +18,14 @@ my $anvil = Anvil::Tools->new(); sub db_access { - my $parameters = shift; - my $db_uuid = $parameters->{db_uuid}; - my $sql_query = $parameters->{sql_query}; - my $access_mode = $parameters->{access_mode}; + my $parameters = shift; + my $db_access_mode = $parameters->{db_access_mode}; + my $db_uuid = $parameters->{db_uuid}; + my $sql_query = $parameters->{sql_query}; my $access_parameters = { query => $sql_query, uuid => $db_uuid, source => $THIS_FILE, line => __LINE__ }; - return ($access_mode eq "write") + return ($db_access_mode eq "write") ? { write_code => $anvil->Database->write($access_parameters) } : $anvil->Database->query($access_parameters); } @@ -41,16 +41,17 @@ if (not $anvil->data->{sys}{database}{connections}) $anvil->nice_exit({ exit_code => 1 }); } +my $data_hash = $anvil->data->{switches}{'data'}; +my $db_access_mode = defined $anvil->data->{switches}{'mode'} ? $anvil->data->{switches}{'mode'} : ""; my $db_uuid = $anvil->data->{switches}{'uuid'}; -my $sub_name = defined $anvil->data->{switches}{'sub'} ? $anvil->data->{switches}{'sub'} : ""; +my $sql_query = $anvil->data->{switches}{'query'}; my $sub_module_name = defined $anvil->data->{switches}{'sub-module'} ? $anvil->data->{switches}{'sub-module'} : "Database"; +my $sub_name = defined $anvil->data->{switches}{'sub'} ? $anvil->data->{switches}{'sub'} : ""; my $sub_params = defined $anvil->data->{switches}{'sub-params'} ? $anvil->data->{switches}{'sub-params'} : "{}"; -my $sql_query = $anvil->data->{switches}{'query'}; -my $access_mode = defined $anvil->data->{switches}{'mode'} ? $anvil->data->{switches}{'mode'} : ""; if ($sql_query) { - my $results = db_access({ db_uuid => $db_uuid, sql_query => $sql_query, access_mode => $access_mode }); + my $results = db_access({ db_uuid => $db_uuid, sql_query => $sql_query, db_access_mode => $db_access_mode }); print JSON->new->utf8->encode($results)."\n"; } elsif ($anvil->${sub_module_name}->can($sub_name)) @@ -69,6 +70,10 @@ elsif ($anvil->${sub_module_name}->can($sub_name)) my $results = $anvil->${sub_module_name}->${sub_name}($decoded_sub_params); print JSON->new->utf8->encode({ sub_results => $results })."\n"; } +elsif ($data_hash) +{ + print JSON->new->utf8->allow_blessed->encode($anvil->data)."\n"; +} else { print "error: missing switches and perhaps their respective parameters; one of --query or --sub is required\n";