Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add file extension (read/write) #592

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)