Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix the internal usage of the value from --cmd flag #67

Merged
merged 4 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
python_version: ["3.8", "3.9", "3.10"]
python_version: ["3.8.1", "3.9", "3.10"]

concurrency:
group: ci-tests-${{ matrix.python_version }}-${{ github.ref }}
Expand All @@ -24,7 +24,7 @@ jobs:

- name: Prepare conda environment
run: |
sed -i s/python\ 3\.8/python\ ${{ matrix.python_version }}/ conda/dev.yaml
sed -i s/python\ 3\.8\.1/python\ ${{ matrix.python_version }}/ conda/dev.yaml
cat conda/dev.yaml
- name: create a dotenv file
Expand Down
20 changes: 11 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ for line in sys.stdin:
endef
export PRINT_HELP_PYSCRIPT

ARGS:=

.PHONY:help
help:
@python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)
Expand All @@ -37,7 +39,7 @@ lint:

.PHONY:test
test: ## run tests quickly with the default Python
pytest -s -vv tests
pytest -s -vv tests ${ARGS}


.PHONY:docs-build
Expand All @@ -58,8 +60,8 @@ build:
smoke-tests:
set -ex
# group 1
containers-sugar help
containers-sugar version
containers-sugar --help
containers-sugar --version
containers-sugar build --group group1 --all
containers-sugar build --group group1
containers-sugar build --group group1 --services service1-1
Expand All @@ -68,9 +70,9 @@ smoke-tests:
containers-sugar pull --group group1 --services service1-1
containers-sugar start --group group1 --all
containers-sugar restart --group group1 --all
containers-sugar exec --group group1 --services service1-1 --extras="-T" --cmd "env"
containers-sugar exec --group group1 --service service1-1 --extras="-T" --cmd "env"
containers-sugar stop --group group1 --all
containers-sugar run --group group1 --services service1-1 --extras="-T" --cmd "env"
containers-sugar run --group group1 --service service1-1 --extras="-T" --cmd "env"
containers-sugar down --group group1
# group 2
containers-sugar build --group group2 --all
Expand All @@ -81,9 +83,9 @@ smoke-tests:
containers-sugar pull --group group2 --services service2-1
containers-sugar start --group group2 --all
containers-sugar restart --group group2 --all
containers-sugar exec --group group2 --services service2-1 --extras="-T" --cmd "env"
containers-sugar exec --group group2 --service service2-1 --extras="-T" --cmd "env"
containers-sugar stop --group group2 --all
containers-sugar run --group group2 --services service2-1 --extras="-T" --cmd "env"
containers-sugar run --group group2 --service service2-1 --extras="-T" --cmd "env"
containers-sugar down --group group2
# group mix
containers-sugar build --group group-mix --all
Expand All @@ -94,7 +96,7 @@ smoke-tests:
containers-sugar pull --group group-mix --services service1-1,service2-1
containers-sugar start --group group-mix --all
containers-sugar restart --group group-mix --all
containers-sugar exec --group group-mix --services service2-1 --extras="-T" --cmd "env"
containers-sugar exec --group group-mix --service service2-1 --extras="-T" --cmd "env"
containers-sugar stop --group group-mix --all
containers-sugar run --group group-mix --services service2-1 --extras="-T" --cmd "env"
containers-sugar run --group group-mix --service service2-1 --extras="-T" --cmd "env"
containers-sugar down --group group-mix
2 changes: 1 addition & 1 deletion conda/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ channels:
- nodefaults
- conda-forge
dependencies:
- python 3.8
- python 3.8.1
- poetry
- nodejs # used by semantic-release
26 changes: 20 additions & 6 deletions containers_sugar/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@ def _get_args():

parser.add_argument(
'action',
choices=['help'] + Sugar.ACTIONS,
choices=Sugar.ACTIONS,
nargs='?',
default=None,
help='Specify the command to be performed.',
)
parser.add_argument(
'--verbose',
action='store_true',
help='Show the command executed.',
)
parser.add_argument(
'--version',
action='store_true',
help='Show the version of containers-sugar.',
)
parser.add_argument(
'--service-group',
'--group',
Expand All @@ -38,6 +50,11 @@ def _get_args():
"Use comma to separate the services's name"
),
)
parser.add_argument(
'--service',
type=str,
help=('Set the service for the container call.'),
)
parser.add_argument(
'--all',
action='store_true',
Expand Down Expand Up @@ -71,14 +88,11 @@ def app():
args_parser = _get_args()
args = args_parser.parse_args()

if args.action == 'help':
return args_parser.print_help()

sugar = Sugar(args)

if args.action == 'version':
if args.version:
show_version()
sugar.run()
sugar._version()
return

sugar.load_services()
Expand Down
91 changes: 40 additions & 51 deletions containers_sugar/sugar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sugar class for containers"""
import argparse
import os
import shlex
import sys
from pathlib import Path

Expand All @@ -14,7 +15,18 @@
docker_compose = None


class Sugar:
class PrintPlugin:
def _print_error(self, message: str):
print(Fore.RED, message, Fore.RESET)

def _print_info(self, message: str):
print(Fore.BLUE, message, Fore.RESET)

def _print_warning(self, message: str):
print(Fore.YELLOW, message, Fore.RESET)


class Sugar(PrintPlugin):
ACTIONS = [
'build',
'config',
Expand All @@ -28,7 +40,6 @@ class Sugar:
'restart',
'start',
'stop',
'version',
'wait',
]

Expand Down Expand Up @@ -68,27 +79,23 @@ def _call_compose_app(
'_err': sys.stderr,
'_no_err': True,
'_env': os.environ,
'_bg': True,
'_bg_exc': False,
}

sh_extras.update(
{
'_bg': True,
'_bg_exc': False,
}
)

cmd_list = [cmd] if cmd else []

print(
'>>>',
self.compose_app,
*self.compose_args,
*args,
*extras,
*services,
*cmd_list,
)
print('-' * 80)
cmd_list = shlex.split(cmd) if cmd else []

if self.args.verbose:
print(
'>>>',
self.compose_app,
*self.compose_args,
*args,
*extras,
*services,
*cmd_list,
)
print('-' * 80)

positional_parameters = (
self.compose_args + list(args) + extras + services + cmd_list
Expand Down Expand Up @@ -120,7 +127,7 @@ def _verify_args(self):
)
os._exit(1)

if self.args.action not in self.ACTIONS:
if self.args.action and self.args.action not in self.ACTIONS:
self._print_error(
'[EE] The given action is not valid. Use one of them: '
+ ','.join(self.ACTIONS)
Expand Down Expand Up @@ -159,19 +166,19 @@ def _load_compose_args(self):
['--env-file', self.service_group['env-file']]
)

service_group = []
compose_path = []
if isinstance(self.service_group['compose-path'], str):
service_group.append(self.service_group['compose-path'])
compose_path.append(self.service_group['compose-path'])
elif isinstance(self.service_group['compose-path'], list):
service_group.extend(self.service_group['compose-path'])
compose_path.extend(self.service_group['compose-path'])
else:
self.self._print_error(
'[EE] The attribute compose-path` supports the data types'
'string or list.'
)
os._exit(1)

for p in service_group:
for p in compose_path:
self.compose_args.extend(['--file', p])

if self.service_group.get('project-name'):
Expand All @@ -184,12 +191,7 @@ def _filter_service_group(self):

if not self.args.service_group:
if len(groups) > 1:
self._print_error(
'[EE] Unable to infer the service group:'
'The service group for this operation was not defined, '
'and there are more than one service group in the '
'configuration file.'
)
self._print_error('[EE] The service group was not defined.')
os._exit(1)
self.service_group = groups[0]
return
Expand Down Expand Up @@ -221,17 +223,6 @@ def _load_service_names(self):
elif 'default' in services and services['default']:
self.service_names = services['default'].split(',')

# print messages

def _print_error(self, message: str):
print(Fore.RED, message, Fore.RESET)

def _print_info(self, message: str):
print(Fore.BLUE, message, Fore.RESET)

def _print_warning(self, message: str):
print(Fore.YELLOW, message, Fore.RESET)

# container commands
def _config(self):
self._call_compose_app('config')
Expand All @@ -254,16 +245,15 @@ def _down(self):
)

def _exec(self):
if len(self.service_names) > 1:
if not self.args.service:
self._print_error(
'[EE] `exec` sub-command expected just one service as '
'parameter'
'[EE] `exec` sub-command expected --service parameter.'
)
os._exit(1)

self._call_compose_app(
'exec',
services=self.service_names,
services=[self.args.service],
cmd=self.args.cmd,
)

Expand All @@ -285,16 +275,15 @@ def _restart(self):
self._start()

def _run(self):
if len(self.service_names) > 1:
if not self.args.service:
self._print_error(
'[EE] `run` sub-command expected just one service as '
'parameter'
'[EE] `run` sub-command expected --service parameter.'
)
os._exit(1)

self._call_compose_app(
'run',
services=self.service_names,
services=[self.args.service],
cmd=self.args.cmd,
)

Expand Down
Loading