From 0fa9fff6e4cec0789c2250cbfc447e5d73c11ce8 Mon Sep 17 00:00:00 2001 From: freehuntx Date: Wed, 24 Jul 2024 14:58:17 +0200 Subject: [PATCH] feat: Add file extension (read/write) Signed-off-by: FreehuntX --- README.md | 1 + file/README.md | 38 ++++++++++++++++++++++++++++++++++++++ file/Tiltfile | 21 +++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 file/README.md create mode 100644 file/Tiltfile diff --git a/README.md b/README.md index e036bb2be..bab87e1b6 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ All extensions have been vetted and approved by the Tilt team. - [`dotenv`](/dotenv): Load environment variables from `.env` or another file. - [`earthly`](/earthly): Build container images using [earthly](https://earthly.dev) - [`execute_in_pod`](/execute_in_pod): Execute a command on a pod container. +- [`file`](/file): Read/Write files as text. - [`file_sync_only`](/file_sync_only): No-build, no-push, file sync-only development. Useful when you want to live-reload a single config file into an existing public image, like nginx. - [`get_obj`](/get_obj): Get object yaml and the container's registry and image from an existing k8s resource such as deployment or statefulset - [`git_resource`](/git_resource): Deploy a dockerfile from a remote repository -- or specify the path to a local checkout for local development. diff --git a/file/README.md b/file/README.md new file mode 100644 index 000000000..20daadc6d --- /dev/null +++ b/file/README.md @@ -0,0 +1,38 @@ +# File extension + +Author: [FreehuntX](https://github.com/freehuntx) + +Helper functions to read/write files. + +## Usage + +### Read a file as text + +```python +load('ext://file', 'read_file') + +text = read_file('/tmp/some_file') +print(text) # Prints the content of /tmp/some_file +``` + +### Write text to a file + +```python +load('ext://file', 'write_file') + +write_file('/tmp/some_file', 'This is some text') # Writes to /tmp/some_file +print(read_file('/tmp/some_file')) # Prints the content +``` + +## Functions +### read_file(path) +#### Parameters +* `path` (str) – The path of the file to read +#### Result +* (str) - The content of the file +### write_file(path, content) +#### Parameters +* `path` (str) – The path of the file to read +* `content` (str) – The text to write to the file +#### Result +* (void) diff --git a/file/Tiltfile b/file/Tiltfile new file mode 100644 index 000000000..d04c66c7a --- /dev/null +++ b/file/Tiltfile @@ -0,0 +1,21 @@ +def read_file(path): + if os.name == "posix": # Linux/Mac implementation + return str(local("cat {}".format(path), echo_off=True, quiet=True)) + elif os.name == "nt": # Windows implementation + path = path.replace("/", "\\") + return str(local("type {}".format(path), echo_off=True, quiet=True)) + else: + fail("Unknown system architecture: " + os.name) + +def write_file(path, content): + content = str(content) + + if os.name == "posix": # Linux/Mac implementation + local("echo '{}' > {}".format(content, path), echo_off=True, quiet=True) + elif os.name == "nt": # Windows implementation + path = path.replace("/", "\\") + local('type nul > {}'.format(path), echo_off=True, quiet=True) + for line in content.splitlines(): + local('echo {} >> {}'.format(str(line).replace('&', '^&').replace('<', '^<').replace('>', '^>'), path), echo_off=True, quiet=True) + else: + fail("Unknown system architecture: " + os.name)