Skip to content

Commit 82650ef

Browse files
committed
Add cat to CLI
1 parent e29078f commit 82650ef

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

python/hdfs_native/cli.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import os
3+
import sys
34
from argparse import ArgumentParser, Namespace
45
from typing import List, Optional, Sequence
56
from urllib.parse import urlparse
@@ -48,6 +49,17 @@ def _glob_path(client: Client, glob: str) -> List[str]:
4849
return [glob]
4950

5051

52+
def cat(args: Namespace):
53+
for src in args.src:
54+
client = _client_for_url(src)
55+
for path in _glob_path(client, src):
56+
with client.read(path) as file:
57+
while chunk := file.read(1024 * 1024):
58+
sys.stdout.buffer.write(chunk)
59+
60+
sys.stdout.buffer.flush()
61+
62+
5163
def mkdir(args: Namespace):
5264
create_parent = args.parent
5365

@@ -95,6 +107,14 @@ def main(in_args: Optional[Sequence[str]] = None):
95107

96108
subparsers = parser.add_subparsers(title="Subcommands", required=True)
97109

110+
cat_parser = subparsers.add_parser(
111+
"cat",
112+
help="Print the contents of a file",
113+
description="Print the contents of a file to stdout",
114+
)
115+
cat_parser.add_argument("src", nargs="+", help="File pattern to print")
116+
cat_parser.set_defaults(func=cat)
117+
98118
mkdir_parser = subparsers.add_parser(
99119
"mkdir",
100120
help="Create a directory",

python/tests/test_cli.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import contextlib
2+
import io
3+
14
import pytest
25

36
from hdfs_native import Client
@@ -7,6 +10,29 @@
710
def test_cli(minidfs: str):
811
client = Client(minidfs)
912

13+
# cat
14+
with client.create("/testfile") as file:
15+
file.write(b"1234")
16+
17+
buf = io.BytesIO()
18+
with contextlib.redirect_stdout(io.TextIOWrapper(buf)):
19+
cli_main(["cat", "/testfile"])
20+
assert buf.getvalue() == b"1234"
21+
22+
with client.create("/testfile2") as file:
23+
file.write(b"5678")
24+
25+
buf = io.BytesIO()
26+
with contextlib.redirect_stdout(io.TextIOWrapper(buf)):
27+
cli_main(["cat", "/testfile", "/testfile2"])
28+
assert buf.getvalue() == b"12345678"
29+
30+
with pytest.raises(FileNotFoundError):
31+
cli_main(["cat", "/nonexistent"])
32+
33+
client.delete("/testfile")
34+
client.delete("/testfile2")
35+
1036
# mkdir
1137
cli_main(["mkdir", "/testdir"])
1238
assert client.get_file_info("/testdir").isdir

0 commit comments

Comments
 (0)