浏览代码

Merge pull request #5709 from mnottale/exec-workdir

Add '--workdir' option to 'exec'.
Joffrey F 7 年之前
父节点
当前提交
38f7fdfe89
共有 2 个文件被更改,包括 21 次插入1 次删除
  1. 11 1
      compose/cli/main.py
  2. 10 0
      tests/acceptance/cli_test.py

+ 11 - 1
compose/cli/main.py

@@ -459,6 +459,7 @@ class TopLevelCommand(object):
                               instances of a service [default: 1]
             -e, --env KEY=VAL Set environment variables (can be used multiple times,
                               not supported in API < 1.25)
+            -w, --workdir DIR Path to workdir directory for this command.
         """
         environment = Environment.from_env_file(self.project_dir)
         use_cli = not environment.get_boolean('COMPOSE_INTERACTIVE_NO_CLI')
@@ -467,7 +468,12 @@ class TopLevelCommand(object):
         detach = options.get('--detach')
 
         if options['--env'] and docker.utils.version_lt(self.project.client.api_version, '1.25'):
-            raise UserError("Setting environment for exec is not supported in API < 1.25'")
+            raise UserError("Setting environment for exec is not supported in API < 1.25 (%s)"
+                            % self.project.client.api_version)
+
+        if options['--workdir'] and docker.utils.version_lt(self.project.client.api_version, '1.35'):
+            raise UserError("Setting workdir for exec is not supported in API < 1.35 (%s)"
+                            % self.project.client.api_version)
 
         try:
             container = service.get_container(number=index)
@@ -487,6 +493,7 @@ class TopLevelCommand(object):
             "user": options["--user"],
             "tty": tty,
             "stdin": True,
+            "workdir": options["--workdir"],
         }
 
         if docker.utils.version_gte(self.project.client.api_version, '1.25'):
@@ -1453,6 +1460,9 @@ def build_exec_command(options, container_id, command):
         for env_variable in options["--env"]:
             args += ["--env", env_variable]
 
+    if options["--workdir"]:
+        args += ["--workdir", options["--workdir"]]
+
     args += [container_id]
     args += command
     return args

+ 10 - 0
tests/acceptance/cli_test.py

@@ -1559,6 +1559,16 @@ class CLITestCase(DockerClientTestCase):
         assert stdout == "operator\n"
         assert stderr == ""
 
+    @v3_only()
+    def test_exec_workdir(self):
+        self.base_dir = 'tests/fixtures/links-composefile'
+        os.environ['COMPOSE_API_VERSION'] = '1.35'
+        self.dispatch(['up', '-d', 'console'])
+        assert len(self.project.containers()) == 1
+
+        stdout, stderr = self.dispatch(['exec', '-T', '--workdir', '/etc', 'console', 'ls'])
+        assert 'passwd' in stdout
+
     @v2_2_only()
     def test_exec_service_with_environment_overridden(self):
         name = 'service'