From 73d2797bf156f3fa75d56f3b36bbf9f5030f9cb4 Mon Sep 17 00:00:00 2001 From: Guilherme Carraro Martins Date: Sun, 19 May 2019 18:37:23 -0400 Subject: [PATCH 1/5] Adding subcommands to the mix --- kcleaner.py | 66 +++++++++++++++++++++++++++-------------------------- setup.py | 4 ++-- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/kcleaner.py b/kcleaner.py index dfd5e6a..40fc5c3 100755 --- a/kcleaner.py +++ b/kcleaner.py @@ -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() +@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 + +@cli.command('contexts') +@click.option('--name', '-n', help='Name of the context to remove') +@click.pass_context +def contexts(ctx, name): + config_file = get_file(ctx.obj['KUBE_CONFIG']) + config_file = remove_resource(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 = get_file(ctx.obj['KUBE_CONFIG']) + config_file = remove_resource(config_file, resource) + + update_file(ctx.obj['KUBE_CONFIG'], config_file) - config_file = get_file(kubeconfig) +@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 = get_file(ctx.obj['KUBE_CONFIG']) config_file = remove_resource(config_file, resource) - - update_file(kubeconfig, config_file) + update_file(ctx.obj['KUBE_CONFIG'], config_file) if __name__ == '__main__': - main() + cli(obj={}) \ No newline at end of file diff --git a/setup.py b/setup.py index 3432c35..235a41b 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='kcleaner', - version='0.1.0', + version='0.1.1', author='Gui Martins', url='https://fancywhale.ca/', author_email='gui.martins.94@outlook.com', @@ -16,6 +16,6 @@ ], entry_points=''' [console_scripts] - kcleaner=kcleaner:main + kcleaner=kcleaner:cli ''', ) From d58d43ba8bc3f08ba05aabe8b586471495cfd750 Mon Sep 17 00:00:00 2001 From: Guilherme Carraro Martins Date: Sun, 19 May 2019 18:44:31 -0400 Subject: [PATCH 2/5] Testing the file before running --- kcleaner.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/kcleaner.py b/kcleaner.py index 40fc5c3..0269c1c 100755 --- a/kcleaner.py +++ b/kcleaner.py @@ -88,7 +88,7 @@ def remove_resource(config_file, removing_type): return config_file -@click.group() +@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): @@ -99,13 +99,13 @@ def cli(ctx, kubeconfig): logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logging.debug(f'Config file to use: {kubeconfig}') 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 = get_file(ctx.obj['KUBE_CONFIG']) - config_file = remove_resource(config_file, "contexts") + config_file = remove_resource(ctx.obj['CONFIG_FILE'], "contexts") update_file(ctx.obj['KUBE_CONFIG'], config_file) @@ -114,8 +114,8 @@ def contexts(ctx, name): @click.pass_context def clusters(ctx, name): resource = "clusters" - config_file = get_file(ctx.obj['KUBE_CONFIG']) - config_file = remove_resource(config_file, resource) + + config_file = remove_resource(ctx.obj['CONFIG_FILE'], resource) update_file(ctx.obj['KUBE_CONFIG'], config_file) @@ -124,10 +124,10 @@ def clusters(ctx, name): @click.pass_context def users(ctx, name): resource = "users" - config_file = get_file(ctx.obj['KUBE_CONFIG']) - config_file = remove_resource(config_file, resource) + + config_file = remove_resource(ctx.obj['CONFIG_FILE'], resource) update_file(ctx.obj['KUBE_CONFIG'], config_file) if __name__ == '__main__': - cli(obj={}) \ No newline at end of file + cli() \ No newline at end of file From b103ec6c151292049142e04bf6e3f2d50e471b67 Mon Sep 17 00:00:00 2001 From: Guilherme Carraro Martins Date: Sun, 19 May 2019 18:44:41 -0400 Subject: [PATCH 3/5] Now with the right name --- tests/kcleaner_config_file_test.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/kcleaner_config_file_test.py b/tests/kcleaner_config_file_test.py index c41dea7..17b57da 100644 --- a/tests/kcleaner_config_file_test.py +++ b/tests/kcleaner_config_file_test.py @@ -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 @@ -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 @@ -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 \ No newline at end of file From fed9ca431261917215c785fd7c5ce33b1d6a505a Mon Sep 17 00:00:00 2001 From: Guilherme Carraro Martins Date: Sun, 19 May 2019 18:44:49 -0400 Subject: [PATCH 4/5] Importing the right name --- tests/kcleaner_main_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/kcleaner_main_test.py b/tests/kcleaner_main_test.py index 03a0bf2..7ab3356 100644 --- a/tests/kcleaner_main_test.py +++ b/tests/kcleaner_main_test.py @@ -1,5 +1,5 @@ import click from click.testing import CliRunner -from kcleaner import main +from kcleaner import cli runner = CliRunner() From b83a42b70fe12ae929214884328d2042b8be43c4 Mon Sep 17 00:00:00 2001 From: Guilherme Carraro Martins Date: Sun, 19 May 2019 18:54:15 -0400 Subject: [PATCH 5/5] Updated Readme --- README.md | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index ead788b..a454a69 100644 --- a/README.md +++ b/README.md @@ -13,20 +13,21 @@ I want to clean my kube config file without having to open my config file ever a To use this CLI simply type: `kcleaner` This will prompt you to remove the context by using Fuzzy Search. -If you want to clean another kube config file, you should use the flag `-k` or `--kube-config` with the path for your config file - -# Versions and Branches -The naming of the branches are important as the versioning is automatic. -``` -v$(majorVersion).$(devVersion).$(betaVersion) -``` -## Master -Any new Pull Requests to Master are going to increase the master version by 1. -If on version `v0.2.1` and pushed to master, the new release is going to be `v1.0.0`. -## Dev -The dev branches should be created following the pattern of the version of the master branch you are working on. -If you are working out of the version `v1.0.0`, the dev branch should be named `dev/1`, the pipelines will then understand that you are developing in the version v1 and every commit/PR to this branch will then release a new version. -If working on version `v1.0.1` and pushed to `dev/1`, the new release is going to be `v1.1.0` -## Beta -Following the same pattern as Dev, beta branches are going to follow the same naming scheme: `beta/1.0`. -If working on version `v1.2.3` and pushed to `beta/1.2`, the new release is going to be `v1.2.4` +If you want to clean another kube config file, you should use the option `-k` or `--kube-config` with the path for your config file. +If you want to remove clusters, you can too! Just call `kcleaner clusters` and voilá! +What about users? Sure can! `kcleaner users` is here to help! +To select more than one entry, just press tab. All the selected entries will be removed! + +If you know the name of the config entry you're going to remove, you can always use the `-n` or `--name` option to remove it. + + +# TO-DO: + +[] 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 ¯\\_(ツ)_/¯