Skip to content

Commit

Permalink
Using Submodules
Browse files Browse the repository at this point in the history
Feature/subcommands
  • Loading branch information
gcarrarom authored May 19, 2019
2 parents a9989f4 + 5a4fbfd commit cfb3ba7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 43 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ If you know the name of the config entry you're going to remove, you can always

# TO-DO:

1. Add more tests, Code coverage is laughable now ;)
2. Make it easier to add changes and modules to the tool
3. Automated publishing to PyPI, Brew and Chocolatey
4. Add undo flag... Shit happens /shrug
[] 1. Add more tests, Code coverage is laughable now ;)
[x] 2. Make it easier to add changes and modules to the tool - Changed to use command group instead
[] 3. Automated publishing to PyPI, Brew and Chocolatey
[] 3.1. Automated release to PyPI Test
[] 3.2. Automated release to PyPI
[] 3.3. Automated release to Brew
[] 3.4. Automated release to Chocolatey
[] 4. Add undo flag... Shit happens ¯\_(ツ)_
66 changes: 34 additions & 32 deletions kcleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,44 +88,46 @@ def remove_resource(config_file, removing_type):
return config_file


@click.command()
@click.argument(
"resource",
type=click.Choice(
[
'users',
'clusters',
'contexts'
]
),
default='contexts'
)
@click.option(
'--kubeconfig', '-k', default=f'{Path.home()}/.kube/config'
)
@click.option(
'--name', '-n',
help='Name of the entry to remove',
)
def main(resource, name, kubeconfig):
@click.group(invoke_without_command=True)
@click.option('--kubeconfig', '-k', default=f'{Path.home()}/.kube/config', help="Path to the config file to clean")
@click.pass_context
def cli(ctx, kubeconfig):
"""
A little CLI tool to help keeping Config Files clean :)
A little CLI tool to help keeping Config Files clean.
"""
ctx.ensure_object(dict)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
if resource == None:
resource = "clusters"
logging.debug(f'Using resource {resource}')
logging.debug(f'Config file to use: {kubeconfig}')
if name == None:
logging.debug(f'Name is empty, using fzf to search for the resource to remove')
else:
logging.debug(f'Name of the resource requested to remove: {name}')
ctx.obj['KUBE_CONFIG']=kubeconfig
ctx.obj['CONFIG_FILE'] = get_file(ctx.obj['KUBE_CONFIG'])

@cli.command('contexts')
@click.option('--name', '-n', help='Name of the context to remove')
@click.pass_context
def contexts(ctx, name):
config_file = remove_resource(ctx.obj['CONFIG_FILE'], "contexts")

update_file(ctx.obj['KUBE_CONFIG'], config_file)

@cli.command('clusters')
@click.option('--name', '-n', help='Name of the cluster to remove')
@click.pass_context
def clusters(ctx, name):
resource = "clusters"

config_file = remove_resource(ctx.obj['CONFIG_FILE'], resource)

update_file(ctx.obj['KUBE_CONFIG'], config_file)

config_file = get_file(kubeconfig)
config_file = remove_resource(config_file, resource)
@cli.command('users')
@click.option('--name', '-n', help='Name of the user to remove')
@click.pass_context
def users(ctx, name):
resource = "users"

config_file = remove_resource(ctx.obj['CONFIG_FILE'], resource)

update_file(kubeconfig, config_file)
update_file(ctx.obj['KUBE_CONFIG'], config_file)

if __name__ == '__main__':
main()
cli()
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='kcleaner',
version='0.1.0',
version='0.1.1',
author='Gui Martins',
url='https://fancywhale.ca/',
author_email='[email protected]',
Expand All @@ -16,6 +16,6 @@
],
entry_points='''
[console_scripts]
kcleaner=kcleaner:main
kcleaner=kcleaner:cli
''',
)
8 changes: 4 additions & 4 deletions tests/kcleaner_config_file_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import click
from click.testing import CliRunner
from kcleaner import main
from kcleaner import cli

runner = CliRunner()
def test_clean_non_existant_file():

results = runner.invoke(main, ['-k', './non_existent_file'])
results = runner.invoke(cli, ['-k', './non_existent_file'])

assert results.exit_code == 10
assert 'Config File Not found!' in results.output
Expand All @@ -15,7 +15,7 @@ def test_clean_empty_file():
with open('./config', 'w') as f:
f.write('')

result = runner.invoke(main, ['-k', './config'])
result = runner.invoke(cli, ['-k', './config'])
assert result.exit_code == 11
assert "Config File is empty! Can't use it." in result.output

Expand All @@ -24,6 +24,6 @@ def test_non_valid_yaml():
with open('./config', 'w') as f:
f.write('lololol')

result = runner.invoke(main, ['-k', './config'])
result = runner.invoke(cli, ['-k', './config'])
assert result.exit_code == 12
assert "Config File is not a valid yaml file!" in result.output
2 changes: 1 addition & 1 deletion tests/kcleaner_main_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import click
from click.testing import CliRunner
from kcleaner import main
from kcleaner import cli

runner = CliRunner()

0 comments on commit cfb3ba7

Please sign in to comment.