Commits

Anonymous committed 64f2b7d

restructure code ; first pass at clone()

* properly split Client::open() args from hg init and hg clone command
line arguments.

* add clone code

  • Participants
  • Parent commits 6a5e89b

Comments (0)

Files changed (1)

File lib/Hg/Lib.pm

 
 our $VERSION = '0.01_05';
 
-my @common_args = (
-    args      => Optional [StrList],
-    configs   => Optional [StrList],
-    dest      => Optional [Str],
-    encoding  => Optional [Str],
-    env       => Optional [HashRef],
-    hg        => Optional [StrList],
-    insecure  => Optional [Str],
-    path      => Optional [Str],
-    remotecmd => Optional [Str],
-    ssh       => Optional [Str],
-    timeout   => Optional [Num],
+my @open_opts = (
+
+    args     => Optional [StrList],
+    configs  => Optional [StrList],
+    connect  => Optional [Bool],
+    encoding => Optional [Str],
+    env      => Optional [HashRef],
+    hg       => Optional [StrList],
+    timeout  => Optional [Num],
+
 );
 
 sub open {
 
     state $check = compile(
         slurpy Dict [
-            connect => Optional [Bool],
-            @common_args
+            path => Optional [Str],
+            @open_opts,
         ] );
 
     return Hg::Lib::Client->new( $check->( @_ ) );
 }
 
+sub _xopts {
+
+    my $opts = shift;
+
+    return (
+        args     => delete $opts->{args},
+        configs  => delete $opts->{configs},
+        encoding => delete $opts->{encoding} // {},
+        env      => delete $opts->{env} // {},
+        hg       => delete $opts->{hg} // 'hg',
+        path     => delete $opts->{dest},
+        timeout  => delete $opts->{timeout},
+        connect  => delete $opts->{connect},
+    );
+
+}
+
 sub init {
 
     my $class = shift;
 
-    state $check = compile( slurpy Dict \@common_args );
+    state $check = compile(
+        slurpy Dict [
+            insecure  => Optional [Str],
+            remotecmd => Optional [Str],
+            ssh       => Optional [Str],
+            dest      => Optional [Str],
+            @open_opts,
+        ] );
 
     my ( $opts ) = $check->( @_ );
 
-    my $hg = delete $opts->{hg} // 'hg';
+    my %xopts = _xopts( $opts );
 
     my $cmd = prep_cmd(
-        init => [  delete $opts->{dest} // () ],
+        init => [ $xopts{path} // () ],
         $opts
     );
 
-    my ( $stdout, $stderr, $exit ) = capture { system( $hg, @$cmd ) };
+    my ( $stdout, $stderr, $exit ) = capture {
+        local %ENV = ( %ENV, %{ $xopts{env} } );
+        system( $xopts{hg}, @$cmd );
+    };
 
     ECommand->throw(
         cmd    => $cmd,
         error  => $stderr
     ) if $exit;
 
-    return Hg::Lib::Client->new( %$opts, hg => $hg );
+    return Hg::Lib::Client->new( %xopts );
 }
 
 
 
     state $check = compile(
         slurpy Dict [
+            source       => Str,
+            dest         => Optional [Str],
             noupdate     => Optional [Bool],
-            updatrev     => Optional [Str],
+            updaterev    => Optional [Str],
             rev          => Optional [Str],
             branch       => Optional [Str],
             pull         => Optional [Bool],
             uncompressed => Optional [Bool],
-            source       => Str,
-            @common_args,
-          ]
+            ssh          => Optional [Str],
+            remotecmd    => Optional [Str],
+            insecure     => Optional [Str],
+            @open_opts,
+        ] );
 
-    );
 
     my ( $opts ) = $check->( @_ );
 
-    return Hg::Lib::Client->new(
-        @{$opts}{ keys %{ Hg::Lib::Client->shadowed_attrs } } );
+    my %xopts = _xopts( $opts );
+
+    my $cmd = prep_cmd(
+        clone => [ $xopts{path} // () ],
+        $opts
+    );
+
+
+    my ( $stdout, $stderr, $exit ) = capture {
+        local %ENV = ( %ENV, %{ $xopts{env} } );
+        system( $xopts{hg}, @$cmd );
+    };
+
+
+    ECommand->throw(
+        cmd    => $cmd,
+        ret    => $exit,
+        output => $stdout,
+        error  => $stderr
+    ) if $exit;
+
+    return Hg::Lib::Client->new( %xopts );
 }