Merge pull request #1232 from aleksandr-vin/add-parent-directories-search-for-default-compose-files
Add parent directories search for default compose-files
This commit is contained in:
commit
89789c54ad
5 changed files with 61 additions and 35 deletions
|
|
@ -10,7 +10,7 @@ from .. import config
|
|||
from ..project import Project
|
||||
from ..service import ConfigError
|
||||
from .docopt_command import DocoptCommand
|
||||
from .utils import call_silently, is_mac, is_ubuntu
|
||||
from .utils import call_silently, is_mac, is_ubuntu, find_candidates_in_parent_dirs
|
||||
from .docker_client import docker_client
|
||||
from . import verbose_proxy
|
||||
from . import errors
|
||||
|
|
@ -18,6 +18,13 @@ from .. import __version__
|
|||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
SUPPORTED_FILENAMES = [
|
||||
'docker-compose.yml',
|
||||
'docker-compose.yaml',
|
||||
'fig.yml',
|
||||
'fig.yaml',
|
||||
]
|
||||
|
||||
|
||||
class Command(DocoptCommand):
|
||||
base_dir = '.'
|
||||
|
|
@ -100,20 +107,10 @@ class Command(DocoptCommand):
|
|||
if file_path:
|
||||
return os.path.join(self.base_dir, file_path)
|
||||
|
||||
supported_filenames = [
|
||||
'docker-compose.yml',
|
||||
'docker-compose.yaml',
|
||||
'fig.yml',
|
||||
'fig.yaml',
|
||||
]
|
||||
|
||||
def expand(filename):
|
||||
return os.path.join(self.base_dir, filename)
|
||||
|
||||
candidates = [filename for filename in supported_filenames if os.path.exists(expand(filename))]
|
||||
(candidates, path) = find_candidates_in_parent_dirs(SUPPORTED_FILENAMES, self.base_dir)
|
||||
|
||||
if len(candidates) == 0:
|
||||
raise errors.ComposeFileNotFound(supported_filenames)
|
||||
raise errors.ComposeFileNotFound(SUPPORTED_FILENAMES)
|
||||
|
||||
winner = candidates[0]
|
||||
|
||||
|
|
@ -130,4 +127,4 @@ class Command(DocoptCommand):
|
|||
log.warning("%s is deprecated and will not be supported in future. "
|
||||
"Please rename your config file to docker-compose.yml\n" % winner)
|
||||
|
||||
return expand(winner)
|
||||
return os.path.join(path, winner)
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ class ConnectionErrorGeneric(UserError):
|
|||
class ComposeFileNotFound(UserError):
|
||||
def __init__(self, supported_filenames):
|
||||
super(ComposeFileNotFound, self).__init__("""
|
||||
Can't find a suitable configuration file. Are you in the right directory?
|
||||
Can't find a suitable configuration file in this directory or any parent. Are you in the right directory?
|
||||
|
||||
Supported filenames: %s
|
||||
""" % ", ".join(supported_filenames))
|
||||
|
|
|
|||
|
|
@ -62,6 +62,25 @@ def mkdir(path, permissions=0o700):
|
|||
return path
|
||||
|
||||
|
||||
def find_candidates_in_parent_dirs(filenames, path):
|
||||
"""
|
||||
Given a directory path to start, looks for filenames in the
|
||||
directory, and then each parent directory successively,
|
||||
until found.
|
||||
|
||||
Returns tuple (candidates, path).
|
||||
"""
|
||||
candidates = [filename for filename in filenames
|
||||
if os.path.exists(os.path.join(path, filename))]
|
||||
|
||||
if len(candidates) == 0:
|
||||
parent_dir = os.path.join(path, '..')
|
||||
if os.path.abspath(parent_dir) != os.path.abspath(path):
|
||||
return find_candidates_in_parent_dirs(filenames, parent_dir)
|
||||
|
||||
return (candidates, path)
|
||||
|
||||
|
||||
def split_buffer(reader, separator):
|
||||
"""
|
||||
Given a generator which yields strings and a separator string,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue