indeyets / pake

pake is a PHP automation tool with capabilities similar to make
http://wiki.github.com/indeyets/pake
MIT License
183 stars 16 forks source link

add a pake task for sftp upload #23

Open gggeek opened 11 years ago

gggeek commented 11 years ago

This is, afaik, not currently supported by pake, and enough of a chore to merit some pake_function of its own (either that, or have it working via pake_write_file).

From my limited web serach, it seems that using curl is better than using the ss2_sftp functions (wider installed base).

Sample code:

    $fp = fopen( $randfile, 'rb' );

    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, str_replace( 'http://', 'sftp://@', $srv ) . $remotefilename );
    if ( $user != "" ) curl_setopt( $ch, CURLOPT_USERPWD, $user );
    if ( $public_keyfile != "" ) curl_setopt( $ch, CURLOPT_SSH_PUBLIC_KEYFILE, $public_keyfile );
    if ( $private_keyfile != "" ) curl_setopt( $ch, CURLOPT_SSH_PRIVATE_KEYFILE, $private_keyfile );
    if ( $private_keypasswd != "" ) curl_setopt( $ch, CURLOPT_KEYPASSWD, $private_keypasswd );
    curl_setopt( $ch, CURLOPT_UPLOAD, 1 );
    curl_setopt( $ch, CURLOPT_INFILE, $fp );
    // set size of the file, which isn't _mandatory_ but helps libcurl to do
    // extra error checking on the upload.
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize( $randfile ) );
    $ok = curl_exec( $ch );
    $errinfo = curl_error( $ch );
    curl_close( $ch );

    fclose( $fp );

    if ( !$ok )
    {
        throw new pakeException( "Couldn't write {$fullfilename} file: " . $errinfo );
    }
indeyets commented 11 years ago

well, there's pakeSSH::copy_to_server() which uses scp (CLI). I guess it can be "fixed" to use curl, when possible

gggeek commented 11 years ago

Doh, I looked around in the code for "sftp" but somehow missed that. Scp is available on windows as well (at least it is part of git and cygwin) so I guess it could do...

indeyets commented 11 years ago

well, I still prefer "API-powered" solutions to CLI-solutions, so this is a valid feature request