From 0da7e302d6fa3e74d3744fc9e9616e27a7467498 Mon Sep 17 00:00:00 2001 From: Mikita Pilinka Date: Thu, 13 Jun 2024 12:07:47 +0200 Subject: [PATCH] added -M option to show generated man page Ticket: ENT-11824 Changelog: None Signed-off-by: Mikita Pilinka --- cfbs/args.py | 28 ++++++++++--- cfbs/cfbs.1 | 97 +++++++++++++++++++++++++++++++++++++++++++ cfbs/commands.py | 1 + cfbs/main.py | 6 ++- cfbs/man_generator.py | 43 +++++++++++++++++++ setup.py | 4 +- 6 files changed, 171 insertions(+), 8 deletions(-) create mode 100644 cfbs/cfbs.1 create mode 100644 cfbs/man_generator.py diff --git a/cfbs/args.py b/cfbs/args.py index 5a0c319e..bfd48960 100644 --- a/cfbs/args.py +++ b/cfbs/args.py @@ -1,24 +1,41 @@ import argparse +import os from cfbs import commands -from cfbs.utils import cache +from cfbs.utils import cache, user_error def get_args(): - parser = _get_arg_parser() + parser = get_arg_parser() args = parser.parse_args() return args def print_help(): - parser = _get_arg_parser() + parser = get_arg_parser() parser.print_help() +def get_manual(): + file_path = os.path.join(os.path.dirname(__file__), "cfbs.1") + if os.path.exists(file_path): + try: + with open(file_path, "r", encoding="utf-8") as man_file: + man = man_file.read() + if not man: + user_error("Manual file is empty") + else: + return man + except OSError: + user_error("Error reading manual file " + file_path) + else: + user_error("Manual file does not exist") + + @cache -def _get_arg_parser(): +def get_arg_parser(): command_list = commands.get_command_names() - parser = argparse.ArgumentParser(description="CFEngine Build System.") + parser = argparse.ArgumentParser(prog="cfbs", description="CFEngine Build System.") parser.add_argument( "command", metavar="cmd", @@ -34,6 +51,7 @@ def _get_arg_parser(): type=str, default="warning", ) + parser.add_argument("-M", "--manual", help="Print manual page", action="store_true") parser.add_argument( "--version", "-V", help="Print version number", action="store_true" ) diff --git a/cfbs/cfbs.1 b/cfbs/cfbs.1 new file mode 100644 index 00000000..049ef06e --- /dev/null +++ b/cfbs/cfbs.1 @@ -0,0 +1,97 @@ +.TH CFBS "1" "2024\-06\-07" "cfbs" "CFEngine Build System manual" +.SH NAME +cfbs \- combines multiple modules into 1 policy set to deploy on your infrastructure. Modules can be custom promise types, JSON files which enable certain functionality, or reusable CFEngine policy. The modules you use can be written by the CFEngine team, others in the community, your colleagues, or yourself. +.SH SYNOPSIS +.B cfbs +[-h] [--loglevel LOGLEVEL] [-M] [--version] [--force] [--non-interactive] [--index INDEX] [--check] [--checksum CHECKSUM] [--keep-order] [--git {yes,no}] [--git-user-name GIT_USER_NAME] [--git-user-email GIT_USER_EMAIL] [--git-commit-message GIT_COMMIT_MESSAGE] [--ignore-versions-json] [--masterfiles MASTERFILES] [cmd] [args ...] +.SH DESCRIPTION +CFEngine Build System. + +.TP +\fBcmd\fR +The command to perform (pretty, init, status, search, add, remove, clean, update, validate, download, build, install, help, info, show, input, set\-input, get\-input) + +.TP +\fBargs\fR +Command arguments + +.SH OPTIONS +.TP +\fB\-\-loglevel\fR \fI\,LOGLEVEL\/\fR, \fB\-l\fR \fI\,LOGLEVEL\/\fR +Set log level for more/less detailed output + +.TP +\fB\-M\fR, \fB\-\-manual\fR +Print manual page + +.TP +\fB\-\-version\fR, \fB\-V\fR +Print version number + +.TP +\fB\-\-force\fR +Force rebuild / redownload + +.TP +\fB\-\-non\-interactive\fR +Don't prompt, use defaults (only for testing) + +.TP +\fB\-\-index\fR \fI\,INDEX\/\fR +Specify alternate index + +.TP +\fB\-\-check\fR +Check if file(s) would be reformatted + +.TP +\fB\-\-checksum\fR \fI\,CHECKSUM\/\fR +Expected checksum of the downloaded file + +.TP +\fB\-\-keep\-order\fR +Keep order of items in the JSON in 'cfbs pretty' + +.TP +\fB\-\-git\fR \fI\,{yes,no}\/\fR +Override git option in cfbs.json + +.TP +\fB\-\-git\-user\-name\fR \fI\,GIT_USER_NAME\/\fR +Specify git user name + +.TP +\fB\-\-git\-user\-email\fR \fI\,GIT_USER_EMAIL\/\fR +Specify git user email + +.TP +\fB\-\-git\-commit\-message\fR \fI\,GIT_COMMIT_MESSAGE\/\fR +Specify git commit message + +.TP +\fB\-\-ignore\-versions\-json\fR +Ignore versions.json. Necessary in case of a custom index or testing changes to the default index. + +.TP +\fB\-\-masterfiles\fR \fI\,MASTERFILES\/\fR +Add masterfiles on cfbs init choose between + +.br +Binary packages may be downloaded from https://cfengine.com/download/. +.br +The source code is available at https://github.com/cfengine/ +.SH BUGS +Please see the public bug-tracker at https://northerntech.atlassian.net/projects/CFE/. +.br +GitHub pull-requests may be submitted to https://github.com/cfengine/cfbs. +.SH "SEE ALSO" +.BR cf-promises (8), +.BR cf-agent (8), +.BR cf-serverd (8), +.BR cf-execd (8), +.BR cf-monitord (8), +.BR cf-runagent (8), +.BR cf-key (8) +.SH AUTHOR + Northern.tech AS + \ No newline at end of file diff --git a/cfbs/commands.py b/cfbs/commands.py index c05ab093..53653136 100644 --- a/cfbs/commands.py +++ b/cfbs/commands.py @@ -2,6 +2,7 @@ Functions ending in "_command" are dynamically included in the list of commands in main.py for -h/--help/help. """ + import os import re import copy diff --git a/cfbs/main.py b/cfbs/main.py index a892b709..ca5d1e3d 100644 --- a/cfbs/main.py +++ b/cfbs/main.py @@ -10,7 +10,7 @@ from cfbs.utils import user_error, is_cfbs_repo, ProgrammerError from cfbs.cfbs_config import CFBSConfig from cfbs import commands -from cfbs.args import get_args, print_help +from cfbs.args import get_args, print_help, get_manual def init_logging(level): @@ -36,7 +36,9 @@ def init_logging(level): def main() -> int: args = get_args() init_logging(args.loglevel) - + if args.manual: + print(get_manual()) + return 0 if args.version: print("cfbs %s" % version()) return 0 diff --git a/cfbs/man_generator.py b/cfbs/man_generator.py new file mode 100644 index 00000000..46a6d52f --- /dev/null +++ b/cfbs/man_generator.py @@ -0,0 +1,43 @@ +import os +from utils import user_error +from args import get_arg_parser + +try: + from build_manpages.manpage import Manpage +except ImportError: + user_error("'Manpage' dependency not satisfied") + + +def generate_man_page(): + manpage = Manpage(get_arg_parser()) + manpage.manual = "CFEngine Build System manual" + manpage.description = "combines multiple modules into 1 policy set to deploy on your infrastructure. Modules can be custom promise types, JSON files which enable certain functionality, or reusable CFEngine policy. The modules you use can be written by the CFEngine team, others in the community, your colleagues, or yourself." + body = ( + str(manpage) + + """ +.br +Binary packages may be downloaded from https://cfengine.com/download/. +.br +The source code is available at https://github.com/cfengine/ +.SH BUGS +Please see the public bug-tracker at https://northerntech.atlassian.net/projects/CFE/. +.br +GitHub pull-requests may be submitted to https://github.com/cfengine/cfbs. +.SH "SEE ALSO" +.BR cf-promises (8), +.BR cf-agent (8), +.BR cf-serverd (8), +.BR cf-execd (8), +.BR cf-monitord (8), +.BR cf-runagent (8), +.BR cf-key (8) +.SH AUTHOR + Northern.tech AS + """ + ) + with open(os.path.dirname(__file__) + "/cfbs.1", "w", encoding="utf-8") as man_file: + man_file.write(body) + return body + + +generate_man_page() diff --git a/setup.py b/setup.py index 08e0a4bf..a291eb57 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,8 @@ with open("cfbs/VERSION", "w", encoding="utf-8") as fh: fh.write("%s\n" % cfbs_version) +assert os.path.isfile("cfbs/cfbs.1") + with open("README.md", "r", encoding="utf-8") as fh: long_description = fh.read() @@ -35,7 +37,7 @@ long_description_content_type="text/markdown", url="https://github.com/cfengine/cfbs", packages=setuptools.find_packages(exclude=["tests*"]), - package_data={"cfbs": ["VERSION"]}, + package_data={"cfbs": ["VERSION", "cfbs.1"]}, include_package_data=True, classifiers=[ "Programming Language :: Python :: 3",