Skip to content

Commit ae35237

Browse files
committed
Add example for containerCopy()
1 parent 2c63329 commit ae35237

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
"clue/buzz-react": "~0.3.0",
2020
"react/promise": "~1.0|~2.0",
2121
"clue/json-stream": "~0.1.0"
22+
},
23+
"require-dev": {
24+
"clue/tar-react": "~0.1.0"
2225
}
2326
}

examples/copy.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
// this example shows how the containerCopy() call returns a TAR stream,
3+
// how it can be passed to a TAR decoder and how we can then pipe each
4+
// individual file to the console output.
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
use React\EventLoop\Factory as LoopFactory;
9+
use Clue\React\Docker\Factory;
10+
use Clue\React\Tar\Decoder;
11+
use React\Stream\ReadableStreamInterface;
12+
13+
$container = isset($argv[1]) ? $argv[1] : 'asd';
14+
$file = isset($argv[2]) ? $argv[2] : '/etc/passwd';
15+
echo 'Container "' . $container . '" dumping "' . $file . '" (pass as arguments to this example)' . PHP_EOL;
16+
17+
$loop = LoopFactory::create();
18+
19+
$factory = new Factory($loop);
20+
$client = $factory->createClient();
21+
22+
$stream = $client->containerCopyStream($container, array('Resource' => $file));
23+
24+
$tar = new Decoder();
25+
26+
$tar->on('entry', function ($header, ReadableStreamInterface $file) {
27+
// write each entry to the console output
28+
echo '########## ' . $header['filename'] . ' ##########' . PHP_EOL;
29+
$file->on('data', function ($chunk) {
30+
echo $chunk;
31+
});
32+
});
33+
34+
$tar->on('error', function ($e = null) {
35+
// should not be invoked, unless the stream is somehow interrupted
36+
echo 'ERROR processing tar stream' . PHP_EOL . $e;
37+
});
38+
$stream->on('error', function ($e = null) {
39+
// will be called if either parameter is invalid
40+
echo 'ERROR requesting stream' . PHP_EOL . $e;
41+
});
42+
43+
$stream->pipe($tar);
44+
45+
$loop->run();

0 commit comments

Comments
 (0)