The lyf so short, the craft so long to lerne. - Geoffrey Chaucer

the brown-dragon blog

Curl up with a good blog...

2009-01-04

I found this small but incredibly powerful tool when looking for a way to automate uploading my website. cURL is like a swiss-army knife for transferring files. From the documentation on the site:


    cURL supports FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos...), file transfer resume, proxy tunneling and a busload of other useful tricks

It's a nice tool so I'm now using it to automatically upload all my changes to the blog. I track changes using Git so I wrote a small sed script that generates the curl operations needed to synchronize my site.

Here is the batch file that drives the script:

curl.bat

@echo off
REM Create bd-curlfile with the files to upload/delete
REM from the git-changed status

REM Start from scratch
if EXIST bd-curlfile del bd-curlfile

REM Get the difference between current uploaded version
REM and the new uploaded version
git diff --name-status HEAD^^ HEAD > delthis
if errorlevel 1 goto ERRFOUND

REM Use sed to create the curlfile
sed -f %~dp0curl.sed delthis > bd-curlfile
if errorlevel 1 goto ERRFOUND

REM Run curl and save the output
curl -K bd-curlfile > delthis
if errorlevel 1 goto ERRFOUND

REM Check that everything went fine
sed -e "/<H1>/!d" -e "/Operation successful/!s/.*/Uh oh! - &/" delthis
if errorlevel 1 goto ERRFOUND

REM All done!
goto DONE

REM We reach here on error
:ERRFOUND
echo ERROR!
exit /B 1

:DONE

And here is the sed script that generates the curl commands:

curl.sed

# Upload files to server using curl

# Write header
1i\
--ftp-create-dirs\
--ftp-method nocwd\
-x <proxy>:<port>\
\

# Check for tmp files
/.swp$/{s/.*/Error - tmp file found! (&)/;q;}
/\#/{s/.*/Error - tmp file found! (&)/;q;}
/delthis/{s/.*/Error - tmp file found! (&)/;q;}

# Check that we have the correct format
/^[AMD]\t/!{
s/.*/Damn...did not understand "&"/
q
}

# Copy only the www files
/^[AMD]\twww/!d

# Upload an added or modified file
s_^[AM]\t\(.*\)_-T "\1"\n--url "ftp://<user>:<password>@<server>/\1"_
# Remove a deleted file
s_^D\t\(.*\)_-Q del "ftp://<user>:<password>@<server>/\1"_

It seems to be working really well. I simply make my changes, check-in to Git and run the batch file. Voila! The exact changes needed to keep my blog in synch are magically done!

Ah...the joys of automation... :-)

Other Posts

(ordered by Tags then Date)