actions-toolkit/toolkit/php/env.php
2020-10-22 07:05:28 +05:30

52 lines
No EOL
1.4 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
function gh_env( $key, $default = null ) {
if ( isset( $_ENV[ $key ] ) ) {
return $_ENV[ $key ];
}
return $default;
}
function gh_validate_env( $key, $custom_message ) {
if ( null === gh_env( $key ) ) {
$custom_message = ( false === $custom_message ) ? '%s Not Found. Please Set It As ENV Variable' : $custom_message;
$custom_message = sprintf( $custom_message, $key );
$custom_message = '🚩 ' . $custom_message;
gh_log_red( $custom_message );
shell_exec( 'exit;' );
exit;
}
return gh_env( $key );
}
function gh_set_env_multiline( $key, $value, $silent = false ) {
shell_exec( 'echo "' . $key . '<<EOF" >> $GITHUB_ENV' );
shell_exec( 'echo "' . addslashes( $value ) . '" >> $GITHUB_ENV' );
shell_exec( 'echo "EOF" >> $GITHUB_ENV' );
if ( ! $silent ) {
gh_log( "✔️ ENV : ${key} => ${value}" );
}
}
function gh_set_env( $key, $value, $silent = false ) {
shell_exec( 'echo "' . $key . '=' . $value . '" >> $GITHUB_ENV' );
if ( ! $silent ) {
gh_log( "✔️ ENV : ${key} => ${value}" );
}
}
function gh_set_env_silent( $key, $value ) {
gh_set_env( $key, $value, true );
}
function gh_set_env_not_exists( $key, $value, $silent = false ) {
if ( ! isset( $_ENV[ $key ] ) ) {
gh_set_env( $key, $value, $silent );
return true;
}
if ( ! $silent ) {
gh_log( " ENV ${key} ALREADY EXISTS WITH VALUE - {$_ENV[$key]}" );
}
return false;
}