WP_CLI

Runner::generate_ssh_command()privateWP-CLI 1.0

Generate a shell command from the parsed connection string.

Метод класса: Runner{}

Хуков нет.

Возвращает

Строку.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->generate_ssh_command( $bits, $wp_command );
$bits(массив) (обязательный)
Parsed connection string.
$wp_command(строка) (обязательный)
WP-CLI command to run.

Код Runner::generate_ssh_command() WP-CLI 2.8.0-alpha

private function generate_ssh_command( $bits, $wp_command ) {
	$escaped_command = '';

	// Set default values.
	foreach ( [ 'scheme', 'user', 'host', 'port', 'path', 'key' ] as $bit ) {
		if ( ! isset( $bits[ $bit ] ) ) {
			$bits[ $bit ] = null;
		}

		WP_CLI::debug( 'SSH ' . $bit . ': ' . $bits[ $bit ], 'bootstrap' );
	}

	$is_tty = function_exists( 'posix_isatty' ) && posix_isatty( STDOUT );

	if ( 'docker' === $bits['scheme'] ) {
		$command = 'docker exec %s%s%s sh -c %s';

		$escaped_command = sprintf(
			$command,
			$bits['user'] ? '--user ' . escapeshellarg( $bits['user'] ) . ' ' : '',
			$is_tty ? '-t ' : '',
			escapeshellarg( $bits['host'] ),
			escapeshellarg( $wp_command )
		);
	}

	if ( 'docker-compose' === $bits['scheme'] ) {
		$command = 'docker-compose exec %s%s%s sh -c %s';

		$escaped_command = sprintf(
			$command,
			$bits['user'] ? '--user ' . escapeshellarg( $bits['user'] ) . ' ' : '',
			$is_tty ? '' : '-T ',
			escapeshellarg( $bits['host'] ),
			escapeshellarg( $wp_command )
		);
	}

	if ( 'docker-compose-run' === $bits['scheme'] ) {
		$command = 'docker-compose run %s%s%s %s';

		$escaped_command = sprintf(
			$command,
			$bits['user'] ? '--user ' . escapeshellarg( $bits['user'] ) . ' ' : '',
			$is_tty ? '' : '-T ',
			escapeshellarg( $bits['host'] ),
			$wp_command
		);
	}

	// Vagrant ssh-config.
	if ( 'vagrant' === $bits['scheme'] ) {
		$cache     = WP_CLI::get_cache();
		$cache_key = 'vagrant:' . $this->project_config_path;
		if ( $cache->has( $cache_key ) ) {
			$cached = $cache->read( $cache_key );
			$values = json_decode( $cached, true );
		} else {
			$ssh_config = shell_exec( 'vagrant ssh-config 2>/dev/null' );
			if ( preg_match_all( '#\s*(?<NAME>[a-zA-Z]+)\s(?<VALUE>.+)\s*#', $ssh_config, $matches ) ) {
				$values = array_combine( $matches['NAME'], $matches['VALUE'] );
				$cache->write( $cache_key, json_encode( $values ) );
			}
		}

		if ( empty( $bits['host'] ) || ( isset( $values['Host'] ) && $bits['host'] === $values['Host'] ) ) {
			$bits['scheme'] = 'ssh';
			$bits['host']   = $values['HostName'];
			$bits['port']   = $values['Port'];
			$bits['user']   = $values['User'];
			$bits['key']    = $values['IdentityFile'];
		}

		// If we could not resolve the bits still, fallback to just `vagrant ssh`
		if ( 'vagrant' === $bits['scheme'] ) {
			$command = 'vagrant ssh -c %s %s';

			$escaped_command = sprintf(
				$command,
				escapeshellarg( $wp_command ),
				escapeshellarg( $bits['host'] )
			);
		}
	}

	// Default scheme is SSH.
	if ( 'ssh' === $bits['scheme'] || null === $bits['scheme'] ) {
		$command = 'ssh -q %s %s %s';

		if ( $bits['user'] ) {
			$bits['host'] = $bits['user'] . '@' . $bits['host'];
		}

		$command_args = [
			$bits['port'] ? '-p ' . (int) $bits['port'] . ' ' : '',
			$bits['key'] ? sprintf( '-i %s', $bits['key'] ) : '',
			$is_tty ? '-t' : '-T',
		];

		$escaped_command = sprintf(
			$command,
			implode( ' ', array_filter( $command_args ) ),
			escapeshellarg( $bits['host'] ),
			escapeshellarg( $wp_command )
		);
	}

	WP_CLI::debug( 'Running SSH command: ' . $escaped_command, 'bootstrap' );

	return $escaped_command;
}