Skip to content

Commit

Permalink
feat: Add a new key in the root of the config file for services (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
joel5vega authored Jan 25, 2024
1 parent 40dace1 commit 7e6eabc
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 188 deletions.
9 changes: 8 additions & 1 deletion .makim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ groups:
sugar stop --verbose --group group2 --all
sugar run --verbose --group group2 --service service2-1 --options -T --cmd env
sugar down --verbose --group group2
smoke-services:
help:
dependencies:
- target: docker.killall
run: |
sugar build --verbose --config-file tests/containers/.services.sugar.yaml
smoke-mix:
help: Run smoke tests for group mix
dependencies:
Expand Down Expand Up @@ -174,6 +180,7 @@ groups:
- target: tests.smoke-main
- target: tests.smoke-defaults
- target: tests.smoke-final
- target: tests.smoke-services
run: |
sugar --help
sugar --version
Expand Down
186 changes: 0 additions & 186 deletions makim.yaml

This file was deleted.

44 changes: 43 additions & 1 deletion src/sugar/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def __init__(
self._load_config()
self._load_env()
self._load_defaults()
self._load_root_services()
self._verify_args()
self._load_compose_app()
self._load_compose_args()
Expand Down Expand Up @@ -118,6 +119,34 @@ def _call_compose_app(
def _check_config_file(self):
return Path(self.config_file).exists()

# Check if services item is given
def _check_services_item(self):
return hasattr(self.config, 'services')

# set default group main
def _load_root_services(self) -> None:
"""Load services attribute in the root of the configuration."""
# must set the default group
services = self.config.get('services', {})

if not services:
return

self.config['groups'] = {
'main': {
'project-name': services.get('project-name'),
'compose-path': services.get('compose-path'),
'env-file': services.get('env-file'),
'services': {
'default': services.get('default'),
'available': services.get('available'),
},
}
}
self.defaults['group'] = 'main'
self.service_group = deepcopy(self.config['groups']['main'])
del self.config['services']

def _filter_service_group(self):
groups = self.config['groups']

Expand Down Expand Up @@ -169,6 +198,19 @@ def _load_config(self):
f_content = io.StringIO(content)
self.config = yaml.safe_load(f_content)

# check if either services or groups are present
if not (self.config.get('services') or self.config.get('groups')):
KxgrLogs.raise_error(
'Either `services` OR `groups` flag must be given',
KxgrErrorType.KXGR_INVALID_CONFIGURATION,
)
# check if both services and groups are present
if self.config.get('services') and self.config.get('groups'):
KxgrLogs.raise_error(
'`services` and `groups` flags given, only 1 is allowed.',
KxgrErrorType.KXGR_INVALID_CONFIGURATION,
)

def _load_compose_app(self):
compose_cmd = self.config.get('compose-app', '')
if compose_cmd.replace(' ', '-') != 'docker-compose':
Expand All @@ -186,7 +228,7 @@ def _load_compose_app(self):
def _load_compose_args(self):
self._filter_service_group()

if 'env-file' in self.service_group:
if self.service_group.get('env-file'):
self.compose_args.extend(
['--env-file', self.service_group['env-file']]
)
Expand Down
4 changes: 4 additions & 0 deletions test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- name: group2
project-name: null #optional
compose-path: containers/tests2/group2/compose.yaml
env-file: .env
12 changes: 12 additions & 0 deletions tests/containers/.services.sugar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 1.0
compose-app: docker compose
defaults:
project-name: sugar-{{ env.KXGR_PROJECT_NAME }}
services:
project-name: project1 # optional
compose-path: tests/containers/group1/compose.yaml
default: service1-1,service1-3
available:
- name: service1-1
- name: service1-2
- name: service1-3

0 comments on commit 7e6eabc

Please sign in to comment.