Dwimmer

 view release on metacpan or  search on metacpan

script/dwimmer_admin.pl  view on Meta::CPAN


    exit;
}

if ($opt{listusers}) {
    $ENV{DWIMMER_ROOT} = $opt{root};
    my $db = _get_db();
    my @users = $db->resultset('User')->all();
    die "No user was found" if not @users;
    foreach my $u (@users) {
        printf("%4s  '%s'\n", $u->id, $u->name);
    }

    exit;
}

if ($opt{showuser}) {
    if (not $opt{username}) {
        die "Need username to ";
    }

    $ENV{DWIMMER_ROOT} = $opt{root};
    my $db = _get_db();
    my $user = $db->resultset('User')->find( { name => $opt{username} } );
    die "User was not found" if not $user;
    foreach my $key (qw(id name email fname lname country state validation_key verified register_ts)) {
        say "$key " . ($user->$key // '');
    }

    exit;
}


if (not $opt{upgrade} and not $opt{setup}) {
    usage();
}




# When we are in the development environment (have .git) set this to the root directory
# When we are in the installation environment (have Makefile.PL) set this to the share/ subdirectory
my $dist_dir;
if (-e File::Spec->catdir(dirname(dirname abs_path($0)) , '.git') ) {
    $dist_dir = dirname(dirname abs_path($0))
} elsif (-e File::Spec->catdir(dirname(dirname abs_path($0)) , 'Makefile.PL') ) {
    $dist_dir = File::Spec->catdir( dirname(dirname abs_path($0)), 'share' );
} else {
    $dist_dir = File::ShareDir::dist_dir('Dwimmer');
}
# die $dist_dir;

my $db_dir = File::Spec->catdir($opt{root}, 'db');
mkpath $db_dir if not -e $db_dir;

if (not $opt{dbonly}) {
    foreach my $dir (qw(views public bin environments)) {
        my $from = File::Spec->catdir( $dist_dir, $dir );
        my $to   = File::Spec->catdir( $opt{root}, $dir );
		print "dircopy $from $to\n";
		chmod 0644, File::Find::Rule->file()->in($to) if -d $to;
        File::Copy::Recursive::dircopy( $from, $to ) or die $!;
    }
	my $from = File::Spec->catdir( $dist_dir, 'config.yml');
    my $to   = File::Spec->catdir( $opt{root} );
	print "fcopy $from $to\n";
	chmod 0644, File::Find::Rule->file()->in($to) if -d $to;
    File::Copy::Recursive::fcopy( $from, $to ) or die $!;
}

# backup the database
if ($opt{upgrade}) {
    my $db_dir = File::Spec->catdir($opt{root}, 'db');
    my $dbfile = File::Spec->catfile( $db_dir, 'dwimmer.db' );
    my $time = time;
    if (-e $dbfile) {
        File::Copy::Recursive::fcopy($dbfile, "$dbfile.$time");
    }
}

my $dbfile = File::Spec->catfile( $db_dir, 'dwimmer.db' );
if (not $opt{upgrade}) {
    setup_db($dbfile);
}

my @upgrade_from;

foreach my $sql ( glob File::Spec->catfile($dist_dir, 'schema', '*.sql' ) ) {
	next if basename($sql) !~ m{^\d+\.sql$};
	push @upgrade_from, sub {
	    my $dbfile = shift;

	    DBIx::RunSQL->create(
	        dsn => "dbi:SQLite:dbname=$dbfile",
	        sql => $sql,
	        verbose => 0,
	    );
	};
}

upgrades($dbfile);

say 'You can now launch the application and visit the web site';

exit;
##################################################################

sub setup_db {
    my $dbfile = shift;

    die "Database file '$dbfile' already exists\n" if -e $dbfile;

    # 0
    my $sql = File::Spec->catfile($dist_dir, 'schema', 'dwimmer.sql');
    DBIx::RunSQL->create(
        dsn => "dbi:SQLite:dbname=$dbfile",
        sql => $sql,
        verbose => 0,
    );

    my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile");
    my $time = time;
    my $validation_key = String::Random->new->randregex('[a-zA-Z0-9]{10}') . $time . String::Random->new->randregex('[a-zA-Z0-9]{10}');
    $dbh->do('INSERT INTO user (name, sha1, email, validation_key, verified, register_ts) VALUES(?, ?, ?, ?, ?, ?)',
        {},
        'admin', sha1_base64($opt{password}), $opt{email}, $validation_key, 1, $time);



( run in 0.592 second using v1.01-cache-2.11-cpan-d8267643d1d )