App-SimplenoteSync

 view release on metacpan or  search on metacpan

bin/simplenotesync  view on Meta::CPAN

        'password=s',        'quiet|q!',
        'edit|e=s',          'editor=s'
    );

    if ($getopt == 0 or defined $opt->{help}) {
        pod2usage;
    }

    if (defined $opt->{version}) {
        printf "%s version %s [using WebService::Simplenote %s]\n", $PROGNAME,
          $App::SimplenoteSync::VERSION, $WebService::Simplenote::VERSION;
        exit;
    }

    if (defined $opt->{man}) {
        pod2usage(-verbose => 2);
    }

    if (defined $opt->{edit}) {
        $opt->{action} = 'edit';
    }

    # is there a path on the command line?
    if (defined $ARGV[0]) {
        $opt->{notes_dir} = $ARGV[0];
    }

    return 1;
}

sub get_logger {

    my $log_level = 'info';
    if ($opt->{debug}) {
        $log_level = 'debug';
    }

    # TODO interactive/background
    $logger = Log::Dispatch->new(
        outputs => [

            [
                'Syslog',
                min_level => $log_level,
                ident     => $PROGNAME,
            ],
        ],
    );

    if (!$opt->{quiet}) {
        require Log::Dispatch::Screen::Color;
        $logger->add(
            Log::Dispatch::Screen::Color->new(
                min_level => 'info',
                newline   => 1,
            ));
    }

    if ($opt->{debug}) {
        require Log::Dispatch::File;
        require DateTime;
        my $sub = sub {
            my %p   = @_;
            my $dt  = DateTime->now;
            my $str = sprintf '%s::%s::%s', $dt->iso8601, uc $p{level},
              $p{message};
            return $str;
        };

        $logger->add(
            Log::Dispatch::File->new(
                name      => 'debug_file',
                min_level => 'debug',
                filename  => $PROGNAME . '_debug.log',
                newline   => 1,
                callbacks => $sub,
            ));
    }

    Log::Any::Adapter->set('Dispatch', dispatcher => $logger);
}

sub get_config {
    my $conf_dir  = Path::Class::Dir->new(File::BaseDir->config_home);
    my $conf_file = $conf_dir->file("$PROGNAME.ini");
    $logger->debug("Trying to read config from $conf_file");

    my $config;
    try {
        $config = Config::INI::Reader->read_file($conf_file);
    }
    catch {
        if (!exists $opt->{email} || !exists $opt->{password}) {
            die
              "--email and/or --password missing from command line, and failed to read config file: $_";
        }
    };

    # merge config opts - cli opts take precedence
    foreach my $key (keys %{$config->{_}}) {
        if (!exists $opt->{$key}) {
            $opt->{$key} = $config->{_}->{$key};
        }
    }

    if (!$opt->{debug}) {
        return;
    }

    $logger->debug('======= Options ========');
    foreach my $key (keys %$opt) {
        my $value = $opt->{$key};
        if ($key eq 'password') {
            $value = '******';
        }
        $logger->debug("$key: [$value]");
    }
    $logger->debug('===== End Options ======');
    return;
}

get_opts;
get_logger;
get_config;



( run in 0.997 second using v1.01-cache-2.11-cpan-39bf76dae61 )