81 lines
2.5 KiB
Plaintext
81 lines
2.5 KiB
Plaintext
|
#!/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;
|
||
|
|
||
|
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.' ],
|
||
|
[
|
||
|
'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;
|
||
|
|
||
|
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:' );
|
||
|
}
|
||
|
my $config = {
|
||
|
secret => $secret,
|
||
|
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;
|
||
|
path ($config_path)->spew_utf8(encode_json $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;
|
||
|
}
|