App-MtAws

 view release on metacpan or  search on metacpan

lib/App/MtAws/ConfigDefinition.pm  view on Meta::CPAN

					$F->parse_include($_->{value});
				} elsif ($_->{name} eq 'exclude') {
					$F->parse_exclude($_->{value});
				} else {
					confess;
				}
			}
			@l, custom('parsed', $F);
		} else {
			@l;
		}
	}
}

sub check_base_dir
{
	if ( present('base-dir') && !present('dir') ) {
		error('base-dir can be used only with dir');
	} elsif ( present('dir') ) {
		optional('base-dir');
	} else {
		return;
	}
}

#sub abs_dir()
#{
#	custom 'abs-dir', File::Spec->rel2abs(value('dir'));
#}

sub mandatory_maxsize
{
	unless (present(optional('check-max-file-size'))) {
		error('mandatory_with', a => 'check-max-file-size', b => seen('stdin'));
	}
	'check-max-file-size'
}

sub check_dir_or_relname
{

	message 'mutual', "%option a% and %option b% are mutual exclusive";
	message 'mandatory_with', "Need to use %option b% together with %option a%";
	if (present('filename')) {
		custom('data-type', 'filename'), mandatory('filename'), do {
			if (present('set-rel-filename')) {
				if (present('dir')) {
					error('mutual', a => seen('set-rel-filename'), b => seen('dir'));
				} else {
					custom('name-type', 'rel-filename'), mandatory('set-rel-filename'), custom('relfilename', value('set-rel-filename'));
				}
			} elsif (present('dir')) {
				custom('relfilename', do {
					validate 'dir', 'filename';
					if (valid('dir') && valid('filename')) {

						my $b_dir = binary_abs_path(binaryfilename value('dir'));
						my $b_file = binary_abs_path(binaryfilename value('filename'));

						if (!defined $b_dir) {
							error(message('cannot_resolve_dir',
								'Directory specified with "%option a%" cannot be resolved to full path'),
								a => 'dir'), undef
						} elsif (!defined $b_file) {
							error(message('cannot_resolve_file',
								'File specified with "%option a%" cannot be resolved to full path'),
								a => 'filename'), undef;
						} else {
							my $relfilename = characterfilename abs2rel($b_file, $b_dir, allow_rel_base => 0, use_filename_encoding => 0);

							my $dir = value('dir');
							$dir =~ s!/$!!; # just in case

							confess "something wrong with relative-absolute paths"
								unless file_inodev(value('filename')) eq file_inodev($dir."/".$relfilename);

							if (!is_relative_filename($relfilename)) {
								error(message('filename_inside_dir',
									'File specified with %option a% should be inside directory specified in %option b%'),
									a => 'filename', b => 'dir'),
								undef;
							} else {
								$relfilename
							}
						}
					} else {
						undef;
					}
				}), custom('name-type', 'dir'), mandatory('dir');
			} else {
				error(message('either', 'Please specify %option a% or %option b%'), a => 'set-rel-filename', b => 'dir');
			}
		}
	} elsif (present('stdin')) {
		if (present('set-rel-filename')) {
			if (present('dir')) {
				seen('stdin'), mandatory_maxsize, error('mutual', a => seen('set-rel-filename'), b => seen('dir'));
			} else {
				custom('name-type', 'rel-filename'), custom('data-type', 'stdin'), mandatory('set-rel-filename'), mandatory('stdin'),
				custom('relfilename', value('set-rel-filename')), mandatory_maxsize;
			}
		} else {
			error('mandatory_with', a => 'set-rel-filename', b => seen('stdin'))
		}
	} else {
		error(message 'Please specify filename or stdin')
	}
}

sub http_download_options
{
	scope('file_downloads', optional('segment-size'))
}

sub download_options
{
	mandatory('dir'), check_base_dir, http_download_options();
}

sub check_wait
{
	if (present('wait')) {
		mandatory('wait'), download_options
	} else {
		return;

lib/App/MtAws/ConfigDefinition.pm  view on Meta::CPAN

		} 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=>'';

		for (option 'filename') {
			validation $_, message('%option a% not a file'), stop => 1, sub { -f binaryfilename };
			validation $_, message('%option a% file not readable'), stop => 1, sub { -r binaryfilename };
			validation $_, message('%option a% file size is zero'), stop => 1, sub { -s binaryfilename };
		}


		for (option 'set-rel-filename') {
			validation $_, message('require_relative_filename', '%option a% should be canonical relative filename'),
				stop => 1,
				sub { is_relative_filename($_) };
		}
		option 'stdin', type=>'';

		option 'vault', deprecated => 'to-vault';
		option 'config', binary => 1;
		options 'journal', 'job-id', 'max-number-of-files', 'new-journal';

		my @encodings =
			map { option($_, binary =>1, default => 'UTF-8') }
			qw/terminal-encoding config-encoding filenames-encoding journal-encoding/;

		for (@encodings) {
			validation $_, 'unknown_encoding', sub { find_encoding($_) };
		}


		my @filters = map { option($_, type => 's', list => 1) } qw/include exclude filter/;

		option 'dry-run', type=>'';

		my $invalid_format = message('invalid_format', 'Invalid format of "%a%"');
		my $must_be_an_integer = message('must_be_an_integer', '%option a% must be positive integer number');

		option('new', type=>'');
		option('replace-modified', type=>'');
		option('delete-removed', type=>'');


		# treehash, mtime, mtime-and-treehash, mtime-or-treehash
		# mtime-and-treehash := treat_as_modified if differs(mtime) && differs(treehash)
		# mtime-or-treehash := treat_as_modified if differs(mtime) or differs(treehash)
		validation
			option('detect', default => 'mtime-and-treehash'),
			$invalid_format,
			sub { my $v = $_; first { $_ eq $v } qw/treehash mtime mtime-and-treehash mtime-or-treehash always-positive size-only/ };



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