App-Env

 view release on metacpan or  search on metacpan

script/appexec  view on Meta::CPAN

    if ( @envs > 1 ) {

        for my $k ( keys %{ $opt{appopts} } ) {
            my ( $env, $key ) = $k =~ /^([^:]*):(.*)$/;

            die( "appopts ($key) not specific to one of the specified environments" )
              unless exists $appopts{$env};

            $appopts{$env}{$key} = $opt{appopts}->{$k};
        }
    }

    else {
        $appopts{ $envs[0] } = $opt{appopts};
    }

    my $env = eval {
        App::Env->new(
            ( map { [ $_ => { AppOpts => $appopts{$_} } ] } @envs ),
            { ( defined $opt{site} ? ( Site => $opt{site} ) : () ), } );
    } // die( "error setting up environment `$opt{env}': $@\n" );

    $env->setenv( $_ ) for @{ $opt{delete} };
    $env->setenv( $_, $opt{define}{$_} ) for keys %{ $opt{define} };

    dumpenv( $env, $opt{dumpenv}, $opt{dumpvar} ) if $opt{dumpenv};

    if ( @ARGV ) {
        say join( q{ }, @ARGV )    ## no critic(InputOutput::RequireCheckedSyscalls)
          if $opt{verbose};

        %ENV = %$env;              ## no critic( Variables::RequireLocalizedPunctuationVars )

        die( "$ARGV[0] does not exist, is not executable, or is not in PATH\n" )
          unless ( file_name_is_absolute( $ARGV[0] ) && -e $ARGV[0] )
          || defined which( $ARGV[0] );

        exec { $ARGV[0] } @ARGV
          or die( "can't exec $ARGV[0]: not in path?\n" );
    }

    return !!1;
}

sub _is_valid_env_name {
    my $name = shift;
    return $name !~ /\P{IsWord}/ && substr( $name, 0, 1 ) =~ /\P{IsDigit}/;
}

sub dumpenv {
    my ( $env, $fmt, $vars ) = @_;

    $vars = [ keys %$env ] unless @$vars;

    ## no critic (InputOutput::RequireCheckedSyscalls)
    ## no critic (ControlStructures::ProhibitCascadingIfElse)
    if ( $fmt eq 'raw' ) {
        say "$_=",
          (
            length $env->{$_}
            ? App::Env::_Util::shell_escape( $env->{$_} )
            : q{}
          ) for @$vars;
    }

    elsif ( $fmt eq 'unquoted' ) {
        say "$_=$env->{$_}" for @$vars;
    }

    elsif ( $fmt eq 'values' ) {
        say $env->{$_} for @$vars;
    }

    elsif ( $fmt eq 'json' ) {
        require JSON::PP;
        say JSON::PP::encode_json( { map { $_ => $env->{$_} } @$vars } );
    }

    elsif ( $fmt eq 'delta-json' ) {
        my ( $delete, $add ) = delta( $env );
        require JSON::PP;
        say JSON::PP::encode_json( {
            delete => $delete,
            add    => { map { $_ => $env->{$_} } @$add },
        } );
    }

    elsif ( $fmt eq 'delta-args' ) {
        my ( $delete, $add ) = delta( $env );
        say join q{ }, ( map { "-X $_" } @$delete ),
          ( map { "-D $_=" . App::Env::_Util::shell_escape( $env->{$_} ) } @$add );
    }

    else {
        require Shell::Guess;

        if ( $fmt eq 'auto' ) {
            $fmt = Shell::Guess->running_shell;
        }
        else {
            die( "unknown dump format: $fmt\n" )
              unless my $mth = Shell::Guess->can( ( $ShellMap{$fmt} // $fmt ) . '_shell' );
            $fmt = Shell::Guess->$mth;
        }
        require Shell::Config::Generate;
        my $config    = Shell::Config::Generate->new;
        my $extracted = $env->env( $vars, { AllowIllegalVariableNames => !!0 } );
        $config->set( $_, $extracted->{$_} ) for keys %$extracted;
        print $config->generate( $fmt );
    }

    return;
}

sub delta {
    my ( $env ) = @_;
    my @delete = grep { !exists $env->{$_} } keys %ENV;

    my @add
      = grep { !exists $ENV{$_} || exists $ENV{$_} && exists $env->{$_} && $ENV{$_} ne $env->{$_} }
      keys %$env;
    return ( \@delete, \@add );
}

sub parse_args {

    %opt = (
        appopts => {},
        clear   => 0,
        define  => {},
        delete  => [],
        dumpvar => [],
        verbose => 0,
        version => 0,
        usage   => 0,
        help    => 0,
    );

    eval {
        local $SIG{__WARN__} = sub { die $_[0] };

        Getopt::Long::Configure( 'no_ignore_case' );

        GetOptions(
            \%opt,
            qw/
              env=s
              appopts|o=s%
              define|D=s%
              delete|X=s@
              usage

script/appexec  view on Meta::CPAN

=item C<--env>=I<name>

A comma separated list of environments (or I<applications> in
B<App::Env> terminology) in which to run the program.  The Perl modules
defining the environments (e.g. B<App::Env::<environment name>> ) must exist.

If this option is not specified, the first non-option argument should
contain the environment name(s).

=item C<--appopts> I<key>=I<value> | C<-o> I<key>=I<value>

Specify a option key and value to be passed via B<AppOpts> to the
B<App::Env> application module.  This option may be specified multiple times.

If multiple environments will be loaded, then each key must be prefixed with
an environment name followed by a colon, e.g.:

  appexec --env env1,env2 -o env1:opt1=val1 -o env2:opt2=val2

=item C<--site>=I<site>

Specify a site. (See B<App::Env> for more information.)

=item C<--define>|C<-D> I<variable>=I<value>

Specify an environment variable to be added to the
application environment.  This option may be repeated.

=item C<--delete>|C<-X> I<variable>

Specify an environment variable to be removed from the
application environment.  This option may be repeated.

=item C<--dumpenv>=I<format> | B<-d> I<format>

Output the environmental variables to the standard output stream with
the specified format.  To specify a subset of the variables to output,
use L</--dumpvar>.

The possible formats are:

=over

=item C<auto>

Guess the current shell using L<Shell::Guess> and output commands to set
the environment

=item C<bash>

=item C<csh>

=item C<ksh>

=item C<tcsh>

Output commands to set the environment appropriate to the specified shell.

=item C<raw>

Output C<key=value> where C<value> has shell metacharacters and spaces escaped

=item C<unquoted>

Output C<key=value>.

=item C<values>

Output only the values one per line.  Only eally useful with L<--dumpvar>.

=item C<json>

Output the values as C<JSON>.

=back

=item C<--dumpvar>|C<-V> I<variable>

Specify an environment variable to dump if L</--dumpenv> is specified.
May be repeated.  If not specified, all environment variables are
dumped.

=item C<--verbose>

Print the command to be run to the standard output stream before running it.

=item C<--help>

Print a short help summary and exit.

=item C<--usage>

Print full documentation and exit.

=back

=head1 EXAMPLES

Real-life examples:

=over

=item 1

Run the B<fhelp> tool from the HEADAS FTOOLS application suite:

  appexec HEADAS fhelp fparkey

=item 2

Run the B<dmlist> tool from the CIAO version 3.4 application suite:

  appexec -o version=3.4 CIAO dmlist

=back

=head1 SUPPORT

=head2 Bugs

Please report any bugs or feature requests to bug-app-env@rt.cpan.org  or through the web interface at: L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-Env>



( run in 1.487 second using v1.01-cache-2.11-cpan-5a3173703d6 )