Skip to content

Commit

Permalink
Merge pull request #27 from craigcomstock/CFE-3764
Browse files Browse the repository at this point in the history
CFE-3764 Added show/info command
  • Loading branch information
nickanderson authored Oct 14, 2021
2 parents 43cab6c + 4bdf89a commit 535fe5b
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 2 deletions.
72 changes: 71 additions & 1 deletion cfbs/commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/bin/env python3
"""
Functions ending in "_command" are dynamically included in the list of commands
in main.py for -h/--help/help.
"""
import os

from cfbs.utils import (
Expand Down Expand Up @@ -31,6 +34,8 @@ def get_definition() -> dict:
global definition
if not definition:
definition = read_json(cfbs_filename())
if not definition:
user_error("Unable to read {}".format(cfbs_filename()))
return definition


Expand Down Expand Up @@ -461,3 +466,68 @@ def install_command(destination=None) -> int:
rm(destination, missing_ok=True)
cp("out/masterfiles", destination)
return 0


def help_command():
pass # no-op here, all *_command functions are presented in help contents


def print_module_info(data):
ordered_keys = [
"module",
"version",
"status",
"by",
"tags",
"repo",
"commit",
"dependencies",
"added_by",
"description",
]
for key in ordered_keys:
if key in data:
if key in ["tags", "dependencies"]:
value = ", ".join(data[key])
else:
value = data[key]
print("{}: {}".format(key.title().replace("_", " "), value))


def info_command(modules, index=None):
if not index:
index = get_index_from_config()
index = Index(index)
build = get_definition()["build"]
alias = None

for module in modules:
print() # whitespace for readability
if not index.exists(module):
print("Module '{}' does not exist".format(module))
continue
if module in index:
data = index[module]
if "alias" in data:
alias = module
module = data["alias"]
data = index[module]
found = any(m for m in build if m["name"] == module)
data["status"] = "Added" if found else "Not Added"
else:
if not module.startswith("./"):
module = "./" + module
data = next((m for m in build if m["name"] == module), None)
if data is None:
print("Path {} exists but is not yet added as a module.".format(module))
continue
data["status"] = "Added"
data["module"] = (module + "({})".format(alias)) if alias else module
print_module_info(data)
print() # extra line for ease of reading
return 0


# show_command here to auto-populate into help in main.py
def show_command(module):
return info_command(module)
11 changes: 10 additions & 1 deletion cfbs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@


def get_args():
command_list = [
cmd.split("_")[0] for cmd in dir(commands) if cmd.endswith("_command")
]
parser = argparse.ArgumentParser(description="CFEngine Build System.")
parser.add_argument(
"command",
metavar="cmd",
type=str,
nargs="?",
help="The command to perform (init, status, search, add, download, build, install, pretty)",
help="The command to perform ({})".format(", ".join(command_list)),
)
parser.add_argument("args", nargs="*", help="Command arguments")
parser.add_argument(
Expand All @@ -41,6 +44,8 @@ def get_args():
)

args = parser.parse_args()
if args.command == "help":
parser.print_help()
return args


Expand Down Expand Up @@ -72,6 +77,8 @@ def main() -> int:
user_error("Usage: cfbs COMMAND")

# Commands you can run outside a cfbs repo:
if args.command == "help":
return 0
if args.command == "init":
return commands.init_command(index=args.index)
if args.command == "search":
Expand All @@ -80,6 +87,8 @@ def main() -> int:
return commands.pretty_command(args.args, args.check)
if args.command == "validate":
return commands.validate_command(index=args.index)
if args.command in ("info", "show"):
return commands.info_command(args.args, index=args.index)

if not is_cfbs_repo():
user_error("This is not a cfbs repo, to get started, type: cfbs init")
Expand Down
5 changes: 5 additions & 0 deletions test/sample/bar/baz/main.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bundle agent bar_baz
{
reports:
"$(this.promise_dirname) $(this.promise_filename) $(this.bundle)";
}
6 changes: 6 additions & 0 deletions test/sample/bar/my.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"vars":
{
"bar-my": "is-a-value"
}
}
41 changes: 41 additions & 0 deletions test/sample/cfbs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "Example",
"description": "Example description",
"build": [
{
"name": "autorun",
"description": "Enable autorun functionality",
"tags": ["wip", "untested"],
"repo": "https://github.com/cfengine/modules",
"by": "https://github.com/cfengine",
"version": "1.0.0",
"commit": "2bd83b1c0d1e6799a8d7221d7e98360b1bdc99bc",
"subdirectory": "management/autorun",
"steps": ["json def.json def.json"],
"added_by": "./foo/main.cf"
},
{
"name": "./foo/main.cf",
"description": "Local policy file added using cfbs command line",
"tags": ["local"],
"dependencies": ["autorun"],
"steps": ["copy ./foo/main.cf services/autorun/main.cf"],
"added_by": "cfbs add"
},
{
"name": "./bar/my.json",
"description": "Local augments file added using cfbs command line",
"tags": ["local"],
"steps": ["json ./bar/my.json def.json"],
"added_by": "cfbs add"
},
{
"name": "./bar/baz/main.cf",
"description": "Local policy file added using cfbs command line",
"tags": ["local"],
"dependencies": ["autorun"],
"steps": ["copy ./bar/baz/main.cf services/autorun/main.cf"],
"added_by": "cfbs add"
}
]
}
5 changes: 5 additions & 0 deletions test/sample/foo/main.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bundle agent foo
{
reports:
"$(this.promise_dirname) $(this.promise_filename) $(this.bundle)";
}
62 changes: 62 additions & 0 deletions test/test_showinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import os
import re
from cfbs.commands import info_command

os.chdir(os.path.join(os.path.dirname(__file__),"sample"))

def test_noargs(capfd):
info_command([])
out, err = capfd.readouterr()
assert out == "\n"

def test_showinfo(capfd):
info_command("autorun masterfiles foo/main.cf bar/my.json bar/baz/main.cf nosuchfile".split(" "))
out, err = capfd.readouterr()

assert re.search(r"""Module: autorun
Version: \d+\.\d+\.\d+
Status: Added
By: https:\/\/github.com\/cfengine
Tags: wip, untested
Repo: https:\/\/github.com\/cfengine\/modules
Commit: [a-zA-Z0-9]+
Description: Enable autorun functionality
""", out, re.M)


assert re.search(r"""Module: masterfiles
Version: \d+\.\d+\.\d+
Status: Not Added
By: https:\/\/github.com\/cfengine
Tags: official, base
Repo: https:\/\/github.com\/cfengine\/masterfiles
Commit: [a-zA-Z0-9]+
Description: Official CFEngine Masterfiles Policy Framework \(MPF\)
""", out, re.M)

assert """Module: ./foo/main.cf
Status: Added
Tags: local
Dependencies: autorun
Added By: cfbs add
Description: Local policy file added using cfbs command line
Module: ./bar/my.json
Status: Added
Tags: local
Added By: cfbs add
Description: Local augments file added using cfbs command line
Module: ./bar/baz/main.cf
Status: Added
Tags: local
Dependencies: autorun
Added By: cfbs add
Description: Local policy file added using cfbs command line
Module 'nosuchfile' does not exist
""" in out

def __main__():
test_showinfo()

0 comments on commit 535fe5b

Please sign in to comment.