App-MtAws
view release on metacpan or search on metacpan
lib/App/MtAws/ConfigDefinition.pm view on Meta::CPAN
mandatory('dir'), check_base_dir, http_download_options();
}
sub check_wait
{
if (present('wait')) {
mandatory('wait'), download_options
} else {
return;
}
}
sub existing_journal
{
my ($journal) = @_;
if (defined($journal) && present($journal) && !exists $App::MtAws::ConfigEngine::context->{override_validations}->{journal}) { # TODO: this is hack!
error('Journal file not found') unless -f binaryfilename value($journal);
}
$journal;
}
sub writable_journal
{
my ($journal) = @_;
if (defined($journal) && present($journal)) {
my $value = binaryfilename value($journal);
error('Journal file not writable') if (-e $value && (! -w $value or -d $value));
}
$journal;
}
sub empty_journal
{
my ($journal) = @_;
if (defined($journal) && present($journal)) {
error('Journal file not empty - please provide empty file no write new journal') unless ! -s binaryfilename value($journal);
}
$journal;
}
sub check_https
{
if (present('protocol') and value('protocol') eq 'https') {
if (LWP::UserAgent->is_protocol_supported("https")) {
# to get LWP version, use LWP->VERSION instead of LWP::UserAgent->VERSION
error('LWP::UserAgent 6.x required to use HTTPS') unless LWP->VERSION() ge '6';
require LWP::Protocol::https;
error('LWP::Protocol::https 6.x required to use HTTPS') unless LWP::Protocol::https->VERSION && LWP::Protocol::https->VERSION ge '6';
} else {
error('IO::Socket::SSL or LWP::Protocol::https is not installed');
}
}
return;
}
sub check_max_size
{
if (present('check-max-file-size')) {
if (value('check-max-file-size') > 10_000 * value('partsize')) {
seen('check-max-file-size'), error(message('partsize_vs_maxsize',
"With current partsize %d partsizevalue%MiB and maximum allowed file size %d maxsizevalue%MiB, upload might exceed 10 000 parts. ".
"Increase %option partsize% or decrease %option maxsize%"),
partsize => 'partsize', maxsize => 'check-max-file-size', partsizevalue => value('partsize'), maxsizevalue => value('check-max-file-size'));
} else {
seen('check-max-file-size')
}
} else {
return;
}
}
sub detect_opts
{
seen('detect'), do { # TODO: movify configengine to somehow simplify this
explicit('detect') && (!present('replace-modified')) ?
error("option_for_command_can_be_used_only_with", a => 'detect', b => 'replace-modified', c => 'sync') :
();
};
}
sub sync_opts
{
my @sync_opts = qw/new replace-modified delete-removed/;
optional(@sync_opts);
if (present('new') || present('replace-modified') || present('delete-removed')) {
@sync_opts
} else {
impose('new', 1); # TODO: can cause problems in the future
}
}
sub get_config
{
my (%args) = @_;
my $c = App::MtAws::ConfigEngine->new(ConfigOption => 'config', CmdEncoding => 'terminal-encoding', ConfigEncoding => 'config-encoding', %args);
$c->{preinitialize} = sub {
set_filename_encoding $c->{options}{'filenames-encoding'}{value};
};
$c->define(sub {
message 'no_command', 'Please specify command', allow_redefine=>1;
message 'already_specified_in_alias', '%option b% specified, while %option a% already defined', allow_redefine => 1;
message 'unexpected_argument', "Extra argument in command line: %a%", allow_redefine => 1;
message 'mandatory', "Please specify %option a%", allow_redefine => 1;
message 'cannot_read_config', 'Cannot read config file "%config%"';
message 'deprecated_option', '%option% deprecated, use %main% instead';
message 'option_for_command_can_be_used_only_with', "Option %option a% for %command c% command can be used only together with %option b%";
for (option 'dir', deprecated => ['to-dir', 'from-dir']) {
validation $_, message('%option a% should be less than 512 characters'), stop => 1, sub { length($_) < 512 }; # TODO: check that dir is dir
validation $_, message('%option a% not a directory'), stop => 1, sub { -d binaryfilename };
}
option 'base-dir';
validation option('leaf-optimization', default => 1), message('%option a% should be either "1" or "0"'), sub { /^[01]$/ };
option 'follow', type=>'';
lib/App/MtAws/ConfigDefinition.pm view on Meta::CPAN
my @config_opts = (
validation(option('key'), $invalid_format, sub { /^[A-Za-z0-9]{20}$/ }),
validation(option('secret'), $invalid_format, sub { /^[\x21-\x7e]{40}$/ }),
validation(option('region'), $invalid_format, sub { /^[A-Za-z0-9\-]{3,20}$/ }),
optional(validation(option('token'), $invalid_format, sub { /^[\x21-\x7e]{20,1024}$/ })),
validation(option('timeout', default => 180), $invalid_format, sub { /^[0-9]{1,5}$/ }),
validation(option('protocol', default => 'http'), message('protocol must be "https" or "http"'), sub { /^https?$/ }),
);
for (option('concurrency', type => 'i', default => 4)) {
validation $_, $must_be_an_integer, stop => 1, sub { $_ =~ /^\d+$/ };
validation $_, message('Max concurrency is 30, Min is 1'), sub { $_ >= 1 && $_ <= 30 };
}
for (option('check-max-file-size', type => 'i')) {
validation $_, $must_be_an_integer, stop => 1, sub { $_ =~ /^\d+$/ };
validation $_, message('check-max-file-size should be greater than 0'), stop => 1, sub { $_ > 0 }; # TODO: %option .. %
validation $_, message('maxsize_too_big', '%option a% should be less than or equal to 40960000 (and you have %value%)'),
stop => 1, sub { $_ <= 10_000 * 4096 };
}
for (option('partsize', type => 'i', default => 16)) {
validation $_, $must_be_an_integer, stop => 1, sub { $_ =~ /^\d+$/ };
validation $_, message('Part size must be power of two'), sub { ($_ != 0) && (($_ & ($_ - 1)) == 0) };
validation $_, message('%option a% must be less or equal to 4096'), sub { $_ <= 4096 };
}
for (option('segment-size', type => 'i')) {
validation $_, $must_be_an_integer, stop => 1, sub { $_ =~ /^\d+$/ };
validation $_, message('%option a% must be zero or power of two'), sub { (($_ & ($_ - 1)) == 0) }; # TODO: proper format
}
validation(option('request-inventory-format', default => 'json'),
message('request-inventory-format must be "json" or "csv"'), sub { /^(json|csv)$/ });
validation positional('vault-name'), message('Vault name should be 255 characters or less and consisting of a-z, A-Z, 0-9, ".", "-", and "_"'), sub {
/^[A-Za-z0-9\.\-_]{1,255}$/
};
{
my $fh = 'for-humans';
my $mt = 'mtmsg';
validation(option('format', default => $fh),
message(qq{%option a% must be "$fh" or "$mt"}), sub { /^(\Q$fh\E|\Q$mt\E)$/ });
}
command 'create-vault' => sub { validate(optional('config'), mandatory(@encodings), mandatory('vault-name'), mandatory(@config_opts), check_https)};
command 'delete-vault' => sub { validate(optional('config'), mandatory(@encodings), mandatory('vault-name'), mandatory(@config_opts), check_https)};
command 'list-vaults' => sub {
validate(optional('config'), mandatory(@encodings), optional('dry-run'), mandatory(@config_opts), check_https, mandatory('format'))
};
command 'sync' => sub {
validate(mandatory(
optional('config'), mandatory(@encodings), @config_opts, sync_opts, detect_opts, check_https,
qw/dir vault concurrency partsize/, writable_journal('journal'),
optional(qw/max-number-of-files leaf-optimization follow/),
filter_options, optional('dry-run')
))
};
command 'upload-file' => sub {
validate(mandatory( optional('config'), mandatory(@encodings), @config_opts, check_https, qw/vault concurrency/, writable_journal('journal'),
check_dir_or_relname, check_base_dir, mandatory('partsize'), check_max_size ))
};
command 'purge-vault' => sub {
validate(mandatory(
optional('config'), mandatory(@encodings), @config_opts, check_https, qw/vault concurrency/,
writable_journal(existing_journal('journal')),
deprecated('dir'), filter_options, optional('dry-run')
))
};
command 'restore' => sub {
validate(mandatory(
optional('config'), mandatory(@encodings), @config_opts, check_https, qw/dir vault max-number-of-files concurrency/,
writable_journal(existing_journal('journal')),
filter_options, optional('dry-run')
))
};
command 'restore-completed' => sub {
validate(mandatory(
optional('config'), mandatory(@encodings), @config_opts, check_https, qw/dir vault concurrency/, existing_journal('journal'),
filter_options, optional('dry-run'), http_download_options
))
};
command 'check-local-hash' => sub {
validate(mandatory(
optional('config'), mandatory(@encodings), @config_opts, check_https, qw/dir/, existing_journal('journal'), deprecated('vault'),
filter_options, optional('dry-run')
))
};
command 'retrieve-inventory' => sub {
validate(mandatory(optional('config'), mandatory(@encodings), 'request-inventory-format', @config_opts, check_https, qw/vault/))
};
command 'download-inventory' => sub {
validate(mandatory(optional('config'), mandatory(@encodings), @config_opts, check_https, 'vault', empty_journal('new-journal')))
};
});
return $c;
}
1;
__END__
( run in 0.797 second using v1.01-cache-2.11-cpan-b16cb0d3907 )