Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add force option to make symlink method (#984)

Mish Ushakov818e8c2647c7
diff --git a/.changeset/gorgeous-queens-dream.md b/.changeset/gorgeous-queens-dream.md
new file mode 100644
index 000000000..004e7e81d
--- /dev/null
+++ b/.changeset/gorgeous-queens-dream.md
@@ -0,0 +1,6 @@
+---
+'@e2b/python-sdk': patch
+'e2b': patch
+---
+
+add force option to make_symlink method
diff --git a/packages/js-sdk/src/template/index.ts b/packages/js-sdk/src/template/index.ts
index 2f9137787..2c872e70a 100644
--- a/packages/js-sdk/src/template/index.ts
+++ b/packages/js-sdk/src/template/index.ts
@@ -359,9 +359,13 @@ export class TemplateBase
makeSymlink(
src: PathLike,
dest: PathLike,
- options?: { user?: string }
+ options?: { user?: string; force?: boolean }
): TemplateBuilder {
- const args = ['ln', '-s', src.toString(), dest.toString()]
+ const args = ['ln', '-s']
+ if (options?.force) {
+ args.push('-f')
+ }
+ args.push(src.toString(), dest.toString())
return this.runInNewStackTraceContext(() =>
this.runCmd(args.join(' '), { user: options?.user })
)
diff --git a/packages/js-sdk/src/template/types.ts b/packages/js-sdk/src/template/types.ts
index f8bd70b01..db848549f 100644
--- a/packages/js-sdk/src/template/types.ts
+++ b/packages/js-sdk/src/template/types.ts
@@ -364,12 +364,13 @@ export interface TemplateBuilder {
* ```ts
* template.makeSymlink('/usr/bin/python3', '/usr/bin/python')
* template.makeSymlink('/usr/bin/python3', '/usr/bin/python', { user: 'root' })
+ * template.makeSymlink('/usr/bin/python3', '/usr/bin/python', { force: true })
* ```
*/
makeSymlink(
src: PathLike,
dest: PathLike,
- options?: { user?: string }
+ options?: { user?: string; force?: boolean }
): TemplateBuilder
/**
diff --git a/packages/js-sdk/tests/template/methods/makeSymlink.test.ts b/packages/js-sdk/tests/template/methods/makeSymlink.test.ts
new file mode 100644
index 000000000..3748ae0d9
--- /dev/null
+++ b/packages/js-sdk/tests/template/methods/makeSymlink.test.ts
@@ -0,0 +1,21 @@
+import { Template } from '../../../src'
+import { buildTemplateTest } from '../../setup'
+
+buildTemplateTest('make symlink', async ({ buildTemplate }) => {
+ const template = Template()
+ .fromImage('ubuntu:22.04')
+ .makeSymlink('.bashrc', '.bashrc.local')
+ .runCmd('test "$(readlink .bashrc.local)" = ".bashrc"')
+
+ await buildTemplate(template)
+})
+
+buildTemplateTest('make symlink (force)', async ({ buildTemplate }) => {
+ const template = Template()
+ .fromImage('ubuntu:22.04')
+ .makeSymlink('.bashrc', '.bashrc.local')
+ .makeSymlink('.bashrc', '.bashrc.local', { force: true }) // Overwrite existing symlink
+ .runCmd('test "$(readlink .bashrc.local)" = ".bashrc"')
+
+ await buildTemplate(template)
+})
diff --git a/packages/python-sdk/e2b/template/main.py b/packages/python-sdk/e2b/template/main.py
index 1a831ba6f..9aa7dd734 100644
--- a/packages/python-sdk/e2b/template/main.py
+++ b/packages/python-sdk/e2b/template/main.py
@@ -220,6 +220,7 @@ class TemplateBuilder:
src: Union[str, Path],
dest: Union[str, Path],
user: Optional[str] = None,
+ force: bool = False,
) -> "TemplateBuilder":
"""
Create a symbolic link in the template.
@@ -227,6 +228,7 @@ class TemplateBuilder:
:param src: Source path (target of the symlink)
:param dest: Destination path (location of the symlink)
:param user: User to run the command as
+ :param force: Force symlink without prompting
:return: `TemplateBuilder` class
@@ -234,9 +236,13 @@ class TemplateBuilder:
```python
template.make_symlink('/usr/bin/python3', '/usr/bin/python')
template.make_symlink('/usr/bin/python3', '/usr/bin/python', user='root')
+ template.make_symlink('/usr/bin/python3', '/usr/bin/python', force=True)
```
"""
- args = ["ln", "-s", str(src), str(dest)]
+ args = ["ln", "-s"]
+ if force:
+ args.append("-f")
+ args.extend([str(src), str(dest)])
return self._template._run_in_new_stack_trace_context(
lambda: self.run_cmd(" ".join(args), user=user)
)
diff --git a/packages/python-sdk/tests/async/template_async/methods/test_make_symlink.py b/packages/python-sdk/tests/async/template_async/methods/test_make_symlink.py
new file mode 100644
index 000000000..4ffc9670f
--- /dev/null
+++ b/packages/python-sdk/tests/async/template_async/methods/test_make_symlink.py
@@ -0,0 +1,30 @@
+import pytest
+
+from e2b import AsyncTemplate
+
+
+async def test_make_symlink(async_build):
+ template = (
+ AsyncTemplate()
+ .from_image("ubuntu:22.04")
+ .make_symlink(".bashrc", ".bashrc.local")
+ .run_cmd('test "$(readlink .bashrc.local)" = ".bashrc"')
+ )
+
+ await async_build(template)
+
+
+async def test_make_symlink_force(async_build):
+ template = (
+ AsyncTemplate()
+ .from_image("ubuntu:22.04")
+ .make_symlink(".bashrc", ".bashrc.local")
+ .make_symlink(
+ ".bashrc", ".bashrc.local", force=True
+ ) # Overwrite existing symlink
+ .run_cmd('test "$(readlink .bashrc.local)" = ".bashrc"')
+ )
+
+ await async_build(template)
diff --git a/packages/python-sdk/tests/sync/template_sync/methods/test_make_symlink.py b/packages/python-sdk/tests/sync/template_sync/methods/test_make_symlink.py
new file mode 100644
index 000000000..a05a33726
--- /dev/null
+++ b/packages/python-sdk/tests/sync/template_sync/methods/test_make_symlink.py
@@ -0,0 +1,30 @@
+import pytest
+
+from e2b import Template
+
+
+def test_make_symlink(build):
+ template = (
+ Template()
+ .from_image("ubuntu:22.04")
+ .make_symlink(".bashrc", ".bashrc.local")
+ .run_cmd('test "$(readlink .bashrc.local)" = ".bashrc"')
+ )
+
+ build(template)
+
+
+def test_make_symlink_force(build):
+ template = (
+ Template()
+ .from_image("ubuntu:22.04")
+ .make_symlink(".bashrc", ".bashrc.local")
+ .make_symlink(
+ ".bashrc", ".bashrc.local", force=True
+ ) # Overwrite existing symlink
+ .run_cmd('test "$(readlink .bashrc.local)" = ".bashrc"')
+ )
+
+ build(template)