Skip to content

Commit

Permalink
Merge pull request #47 from larsewi/clean
Browse files Browse the repository at this point in the history
Added a command to clean up unused dependencies -  `cfbs clean`
  • Loading branch information
olehermanse authored Oct 29, 2021
2 parents 5c04ab2 + 8ebe80c commit 52a6bd0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ cfbs build
cfbs install /var/cfengine/masterfiles
```

### Remove unused dependencies

```
cfbs clean
```

### Deploy your policy set to a remote hub

```
Expand Down
35 changes: 35 additions & 0 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,41 @@ def add_command(to_add: list, added_by="cfbs add", index_path=None, checksum=Non
put_definition(definition)


def clean_command():
definition = get_definition()
modules = definition["build"]

def _someone_needs_me(this) -> bool:
if this["added_by"] == "cfbs add":
return True
for other in modules:
if not "dependencies" in other:
continue
if this["name"] in other["dependencies"]:
return _someone_needs_me(other)
return False

to_remove = list()
for module in modules:
if not _someone_needs_me(module):
to_remove.append(module)

if not to_remove:
return 0

print("The following modules were added as dependencies but are no longer needed:")
for module in to_remove:
print("%s - %s – added by: %s" % (module["name"], module["description"], module["added_by"]))

answer = input("Do you wish to remove these modules? [y/N] ")
if answer.lower() in ("yes", "y"):
for module in to_remove:
modules.remove(module)
put_definition(definition)

return 0


def update_command():
cfg_index = get_index_from_config()
index = Index(cfg_index)
Expand Down
2 changes: 2 additions & 0 deletions cfbs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def main() -> int:
return commands.status_command()
if args.command == "add":
return commands.add_command(args.args, index_path=args.index, checksum=args.checksum)
if args.command == "clean":
return commands.clean_command()
if args.command == "download":
return commands.download_command(args.force)
if args.command == "build":
Expand Down

0 comments on commit 52a6bd0

Please sign in to comment.