App-KGB

 view release on metacpan or  search on metacpan

script/kgb-add-project  view on Meta::CPAN

my $file = '/etc/kgb-bot/kgb.conf';
my $dir  = '/etc/kgb-bot/kgb.conf.d';
my $net  = 'oftc';
my $save = 0;


GetOptions(
    'file=s'        => \$file,
    'dir=s'         => \$dir,
    'net|network=s' => \$net,
    'save|write!'   => \$save,
) or pod2usage( { -verbose => 1 } );

@ARGV == 3 or pod2usage();

my $yaml = LoadFile($file);
my $networks = $yaml->{networks};

if ( $dir and -d $dir ) {
    $yaml = {};
}

my ( $prj, $chan, $pwd ) = @ARGV;

$yaml->{repositories}{$prj}{password} = $pwd;

unless ( $chan =~ /^[#&+]/ ) {
    warn "W: Prepending channel name with a hash sign.\n";
    $chan = "#$chan";
}

unless ( exists $networks->{$net} ) {
    warn "W: Network '$net' missing on configuration file.\n";
}

push @{ $yaml->{channels} }, { name => $chan, network => $net, repos => [ $prj ] };

my $i;
my %key_order = map( ( $_ => $i++ ),
    qw(soap queue_limit log_file pid_dir min_protocol_ver
        debug admins
        repositories networks channels
        smart_answers smart_answers_polygen colors) );

Bless($yaml)->keys(
    [   sort { ( $key_order{$a} // 999 ) <=> ( $key_order{$b} // 999 ) }
            keys %$yaml
    ]
);

foreach ( @{ $yaml->{channels} } ) {
    Bless($_)->keys(
        [ sort { $a ne 'name' } keys %$_ ]
    );
}

my $outfh = \*STDOUT;
my ( $uid, $gid, $mode );

if ($save) {
    $uid  = ( stat($file) )[4];
    $gid  = ( stat($file) )[5];
    $mode = ( stat($file) )[2] & 07777;
    if ( $dir and -d $dir ) {
        $file = File::Spec->catdir($dir, "$prj.conf");
    }
    umask(~$mode & 0777);
    open( $outfh, '>', $file );
}

print $outfh YAML::Dump($yaml);

if ($save) {
    chown $uid, $gid, $file;
}

__END__

=head1 NAME

kgb-add-project - helper script for adding new projects to kgb-bot configuration file

=head1 SYNOPSIS

B<kgb-add-project> [--file configfile] [--dir configdir] [--net|--networks ircnetwork] [--save|--write] project-id ircchannel password

=head1 DESCRIPTION

B<kgb-add-project> allows one to add new projects to B<kgb-bot>'s configuration
file from the command line. It writes the new config to stdout unless I<--save|--write> is given.

=head1 ARGUMENTS

=over

=item B<--file> configfile

Optional. Defaults to F</etc/kgb-bot/kgb.conf>.

=item B<--dir> configdir

Optional. Defaults to F</etc/kgb-bot/kgb.conf.d/>.
Used for writing config snippets for projects with B<--save|--write>.

=item B<--net|network> ircnetwork

Optional. Defaults to B<oftc>. Needs to be in the I<networks> section of the configuration file.

=item B<--save|--write>

Optional. Write new config back to file instead of stdout.

=item B<project-id>

B<project-id> to be added.

=item B<irchannel>

IRC channel where the new project sends its messages. If the channel doesn't
start with a character denoting IRC channel (C<#&+>), then a hash sign is
prepended.

=item B<password>



( run in 1.592 second using v1.01-cache-2.11-cpan-99c4e6809bf )