Peace/bin/peace

112 lines
3.8 KiB
Perl
Executable File

#!/usr/bin/env perl
use v5.30.0;
use strict;
use warnings;
use Data::Dumper;
use Mojolicious::Commands;
use Term::ReadLine;
use Getopt::Long::Descriptive;
use Crypt::URandom q/urandom/;
use JSON;
use Path::Tiny;
use Peace;
use Peace::DB;
my ( $opt, $usage ) = describe_options(
'peace %o',
[ 'dbname|d=s', 'The database name for config generation.' ],
[ 'host|H=s', 'The database host for config generation.' ],
[ 'username|u=s', 'The database username for config generation.' ],
[ 'password|P=s', 'The database password for config generation.' ],
[ 'port|p=i', 'The database port for config generation.' ],
[ 'url|U=s', 'The installation url' ],
[ 'smtp_host=s', 'Name of the smtp host to use.', ],
[ 'smtp_port=s', 'Number of smpt port to use.', ],
[ 'sasl_username=s', 'Name of the smtp user.' ],
[ 'sasl_password=s', 'Password of the smtp user.' ],
[
'disable-readline-ask',
'Disable the readline questions for undefined fields.'
],
[ 'help|h', 'Print this usage message', { shortcircuit => 1 } ],
);
print( $usage->text ), exit if $opt->help;
my $home = $ENV{HOME};
my $config_path = "$home/.config/peace/peace.conf";
if ( !-e $config_path ) {
my $secret = unpack 'H*', urandom(128);
say $secret;
my $prompt = '> ';
my $dbname = $opt->dbname;
my $host = $opt->host;
my $username = $opt->username;
my $password = $opt->password;
my $port = $opt->port;
my $url = $opt->url;
my $smtp_host = $opt->smtp_host;
my $smtp_port = $opt->smtp_port;
my $sasl_username = $opt->sasl_username;
my $sasl_password = $opt->sasl_password;
if ( !$opt->disable_readline_ask ) {
$dbname = ask_readline_question( $dbname, 'Introduce the dbname:' );
$host = ask_readline_question( $host, 'Introduce the host:' );
$username =
ask_readline_question( $username, 'Introduce the username:' );
$password =
ask_readline_question( $password, 'Introduce the password:' );
$port = ask_readline_question( $port, 'Introduce the port:' );
$url = ask_readline_question( $url, 'Intraduce the final url.' );
$smtp_host =
ask_readline_question( $smtp_host, 'Intraduce the smtp host.' );
$smtp_port =
ask_readline_question( $smtp_port, 'Intraduce the smtp port.' );
$sasl_username =
ask_readline_question( $sasl_username, 'Intraduce the smtp user.' );
$sasl_password = ask_readline_question( $sasl_password,
'Intraduce the smtp password.' );
}
my $config = {
secret => $secret,
url => $url,
smtp => {
smtp_host => $smtp_host,
smtp_port => $smtp_port,
sasl_username => $sasl_username,
sasl_password => $sasl_password,
},
db_config => {
( username => $username ) x ( 0 + !!( defined $username ) ),
( dbname => $dbname ) x ( 0 + !!( defined $dbname ) ),
( host => $host ) x ( 0 + !!( defined $host ) ),
( password => $password ) x ( 0 + !!( defined $password ) ),
( port => $port ) x ( 0 + !!( defined $port ) ),
}
};
path($config_path)->parent->mkpath;
print Data::Dumper::Dumper $config;
my $json = JSON->new->utf8->pretty(1);
path($config_path)->spew_utf8( $json->encode($config));
}
Peace::DB->dbh( config => Peace->new->peace_config );
@ARGV = ('daemon');
# Start command line interface for application
Mojolicious::Commands->start_app('Peace');
sub ask_readline_question {
return if defined shift;
my $prompt = '~> ';
my $term = Term::ReadLine->new('Peace setup');
say shift;
return $term->readline($prompt) || undef;
}