pavelsr/API-Google

Correct passing of tokensfile parameter to API::Google::Server

pavelsr opened this issue · 1 comments

Now code will not work if

  • there is more than one json file in current directory
  • file with tokens has another extension that .json

Need to implement passing command-line parameters to Mojolicioius::Lite app (API::Google::Server package)

Full story:

API::Google::Server reads and write token information from json file with help of Config::JSON module

my $config = Config::JSON->new($f);

Now $f is just a first file with .json extension:

sub return_json_filename {
  use Cwd;
  my $cwd = getcwd;
  opendir my $dir, $cwd or die "Cannot open directory: $!";
  my @files = readdir $dir;
  my @j = grep { $_ =~ /\w+.json/ } @files;
  return $j[0];
}

Mojolicious::Lite app is calling from goauth CLI tool like this:

Mojolicious::Commands->start_app('API::Google::Server', 'daemon', '-l', 'http://*:'.$port, 'file', $filename);

As I understood, to pass parameter to Mojolicious::Lite app it's needed to implement custom commmand (example can be Mojolicious::Command::get and extend commands namespace like

my $commands = app->commands;
push @{$commands->namespaces}, 'API::Google::Server::Command::j;

where API::Google::Server::Command::j is a package with code of command handler. Like

# Lowercase command name
package Mojolicious::Command::mycommand;
use Mojo::Base 'Mojolicious::Command';
 
# Short description
has description => 'My first Mojo command';
 
# Short usage message
has usage => <<EOF;
Usage: APPLICATION mycommand [OPTIONS]
 
Options:
  -s, --something   Does something
EOF
 
sub run {
  my ($self, @args) = @_;
 
  # Magic here! :)
}

(taken from Mojolicious::Command)

closed