Skip to content

Commit

Permalink
feat: Add file extension (read/write)
Browse files Browse the repository at this point in the history
Signed-off-by: FreehuntX <freehuntx@web.de>
  • Loading branch information
freehuntx committed Jul 26, 2024
1 parent 3fe5b94 commit 0fa9fff
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 38 additions & 0 deletions file/README.md
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions file/Tiltfile
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0fa9fff

Please sign in to comment.