--- design/design-couch.pl 2009/04/28 12:40:19 33 +++ design/design-couch.pl 2009/04/28 15:06:02 34 @@ -8,6 +8,8 @@ use Data::Dump qw/dump/; use File::Path qw/mkpath/; use File::Slurp qw//; +use File::Find; +use HTTP::Request::Common; # design-couch.pl # @@ -79,17 +81,67 @@ warn "# get $url\n"; my $response = $ua->get( $url ); - die $response->status_line if $response->is_error; my $json = $response->decoded_content; - write_file "../$database-$design.js", $json; + write_file "../$database-$design.pull.js", $json; unroll( from_json $json, '' ); } elsif ( $command eq 'push' ) { + my $json; + + my @attached; + + find({ no_chdir => 1, wanted => sub { + my $path = $File::Find::name; + return unless -f $path; + +warn "## $path\n"; + + $path =~ s{^\./}{}; + + if ( $path =~ m{_attachemnts} ) { + push @attached, $path; + return; + } + + my $data = File::Slurp::read_file( $path ); + $path =~ s[/]['}->{']g; + $path =~ s{\.\w+$}{}; + my $code = "\$json->{'$path'} = \$data;"; + eval $code; + die "ERROR in $code: $@" if $@; +# warn "## json = ",dump( $json ); + }}, '.' ); + + warn "# $database/_design/$design = ",dump( $json ); + write_file "../$database-$design.push.js", to_json $json; + + die "no _id?" unless $json->{_id}; + delete( $json->{_rev} ); + + warn "# put $url\n"; + my $response = $ua->request( HTTP::Request::Common::PUT($url, 'Content-Type' => 'application/json', Content => to_json $json ) ); + + if ( $response->code == 409 ) { + warn "## get old $url\n"; + my $response = $ua->get( $url ); + die $response->status_line if $response->is_error; + + my $data = from_json $response; + $json->{$_} = $data->{$_} foreach ( 'rev', '_id' ); + + $response = $ua->request( HTTP::Request::Common::PUT($url, 'Content-Type' => 'application/json', Content => to_json $json ) ); + die $response->status_line if $response->is_error; + warn "push updated $url\n"; + } else { + die $response->status_line if $response->is_error; + warn "push new $url\n"; + } + warn "FIXME: ignore attachments: ",dump( @attached ); } else { die "$0: unknown command $command";