Skip to content

Commit bc1f6c9

Browse files
committed
Merge pull request #898 from aanand/support-fig-yml
Support fig.yml
2 parents fd30920 + 7c087f1 commit bc1f6c9

4 files changed

Lines changed: 85 additions & 20 deletions

File tree

compose/cli/command.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import unicode_literals
22
from __future__ import absolute_import
33
from requests.exceptions import ConnectionError, SSLError
4-
import errno
54
import logging
65
import os
76
import re
@@ -75,8 +74,6 @@ def get_config(self, config_path):
7574
with open(config_path, 'r') as fh:
7675
return yaml.safe_load(fh)
7776
except IOError as e:
78-
if e.errno == errno.ENOENT:
79-
raise errors.ComposeFileNotFound(os.path.basename(e.filename))
8077
raise errors.UserError(six.text_type(e))
8178

8279
def get_project(self, config_path, project_name=None, verbose=False):
@@ -110,13 +107,34 @@ def get_config_path(self, file_path=None):
110107
if file_path:
111108
return os.path.join(self.base_dir, file_path)
112109

113-
if os.path.exists(os.path.join(self.base_dir, 'docker-compose.yaml')):
114-
log.warning("Compose just read the file 'docker-compose.yaml' on startup, rather "
115-
"than 'docker-compose.yml'")
110+
supported_filenames = [
111+
'docker-compose.yml',
112+
'docker-compose.yaml',
113+
'fig.yml',
114+
'fig.yaml',
115+
]
116+
117+
def expand(filename):
118+
return os.path.join(self.base_dir, filename)
119+
120+
candidates = [filename for filename in supported_filenames if os.path.exists(expand(filename))]
121+
122+
if len(candidates) == 0:
123+
raise errors.ComposeFileNotFound(supported_filenames)
124+
125+
winner = candidates[0]
126+
127+
if len(candidates) > 1:
128+
log.warning("Found multiple config files with supported names: %s", ", ".join(candidates))
129+
log.warning("Using %s\n", winner)
130+
131+
if winner == 'docker-compose.yaml':
116132
log.warning("Please be aware that .yml is the expected extension "
117133
"in most cases, and using .yaml can cause compatibility "
118-
"issues in future")
134+
"issues in future.\n")
119135

120-
return os.path.join(self.base_dir, 'docker-compose.yaml')
136+
if winner.startswith("fig."):
137+
log.warning("%s is deprecated and will not be supported in future. "
138+
"Please rename your config file to docker-compose.yml\n" % winner)
121139

122-
return os.path.join(self.base_dir, 'docker-compose.yml')
140+
return expand(winner)

compose/cli/errors.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def __init__(self, url):
5656

5757

5858
class ComposeFileNotFound(UserError):
59-
def __init__(self, filename):
59+
def __init__(self, supported_filenames):
6060
super(ComposeFileNotFound, self).__init__("""
61-
Can't find %s. Are you in the right directory?
62-
""" % filename)
61+
Can't find a suitable configuration file. Are you in the right directory?
62+
63+
Supported filenames: %s
64+
""" % ", ".join(supported_filenames))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
simple:
2+
image: busybox:latest
3+
command: /bin/sleep 300
4+
another:
5+
image: busybox:latest
6+
command: /bin/sleep 300

tests/unit/cli_test.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
from __future__ import absolute_import
33
import logging
44
import os
5+
import tempfile
6+
import shutil
57
from .. import unittest
68

79
import mock
810

911
from compose.cli import main
1012
from compose.cli.main import TopLevelCommand
13+
from compose.cli.errors import ComposeFileNotFound
1114
from six import StringIO
1215

1316

@@ -31,9 +34,9 @@ def test_project_name_with_explicit_base_dir(self):
3134

3235
def test_project_name_with_explicit_uppercase_base_dir(self):
3336
command = TopLevelCommand()
34-
command.base_dir = 'tests/fixtures/Simple-figfile'
37+
command.base_dir = 'tests/fixtures/UpperCaseDir'
3538
project_name = command.get_project_name(command.get_config_path())
36-
self.assertEquals('simplefigfile', project_name)
39+
self.assertEquals('uppercasedir', project_name)
3740

3841
def test_project_name_with_explicit_project_name(self):
3942
command = TopLevelCommand()
@@ -57,12 +60,30 @@ def test_project_name_from_environment_new_var(self):
5760
project_name = command.get_project_name(None)
5861
self.assertEquals(project_name, name)
5962

60-
def test_yaml_filename_check(self):
61-
command = TopLevelCommand()
62-
command.base_dir = 'tests/fixtures/longer-filename-composefile'
63-
with mock.patch('compose.cli.command.log', autospec=True) as mock_log:
64-
self.assertTrue(command.get_config_path())
65-
self.assertEqual(mock_log.warning.call_count, 2)
63+
def test_filename_check(self):
64+
self.assertEqual('docker-compose.yml', get_config_filename_for_files([
65+
'docker-compose.yml',
66+
'docker-compose.yaml',
67+
'fig.yml',
68+
'fig.yaml',
69+
]))
70+
71+
self.assertEqual('docker-compose.yaml', get_config_filename_for_files([
72+
'docker-compose.yaml',
73+
'fig.yml',
74+
'fig.yaml',
75+
]))
76+
77+
self.assertEqual('fig.yml', get_config_filename_for_files([
78+
'fig.yml',
79+
'fig.yaml',
80+
]))
81+
82+
self.assertEqual('fig.yaml', get_config_filename_for_files([
83+
'fig.yaml',
84+
]))
85+
86+
self.assertRaises(ComposeFileNotFound, lambda: get_config_filename_for_files([]))
6687

6788
def test_get_project(self):
6889
command = TopLevelCommand()
@@ -81,3 +102,21 @@ def test_setup_logging(self):
81102
main.setup_logging()
82103
self.assertEqual(logging.getLogger().level, logging.DEBUG)
83104
self.assertEqual(logging.getLogger('requests').propagate, False)
105+
106+
107+
def get_config_filename_for_files(filenames):
108+
project_dir = tempfile.mkdtemp()
109+
try:
110+
make_files(project_dir, filenames)
111+
command = TopLevelCommand()
112+
command.base_dir = project_dir
113+
return os.path.basename(command.get_config_path())
114+
finally:
115+
shutil.rmtree(project_dir)
116+
117+
118+
def make_files(dirname, filenames):
119+
for fname in filenames:
120+
with open(os.path.join(dirname, fname), 'w') as f:
121+
f.write('')
122+

0 commit comments

Comments
 (0)