/
app.py
118 lines (92 loc) · 3.79 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright 2019-Present Sonatype Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
from datetime import datetime
from typing import Dict
import pkg_resources
from pyfiglet import figlet_format
from termcolor import cprint
from command import BaseCommand
from command.iq import IqCommand
from command.oss import OssCommand
from command.sbom import SbomCommand
class JakeCmd:
# Whether debug output is enabled
_DEBUG_ENABLED: bool = False
# Argument Parser
_arg_parser: argparse.ArgumentParser
# Parsed Arguments
_arguments: argparse.Namespace
# Sub Commands
_subcommands: Dict[str, BaseCommand] = []
def __init__(self):
# Build and parse command arguments
self._load_subcommands()
self._build_arg_parser()
self._parse_arguments()
if self._arguments.debug_enabled:
self._DEBUG_ENABLED = True
self._debug_message('!!! DEBUG MODE ENABLED !!!')
self._debug_message('Parsed Arguments: {}'.format(self._arguments))
def execute(self):
# Show the Jake header
JakeCmd._print_jake_header()
# Determine primary command and then hand off to that Command handler
command = self._subcommands[self._arguments.cmd]
command.execute(arguments=self._arguments)
def _load_subcommands(self):
self._subcommands = {
# 'config': ConfigCommand(),
'iq': IqCommand(),
'oss': OssCommand(),
'sbom': SbomCommand()
}
def _build_arg_parser(self):
self._arg_parser = argparse.ArgumentParser(description='Put your Python dependencies in a chokehold')
# Add global options
self._arg_parser.add_argument('-v', '--version', help='show which version of jake you are running',
action='version',
version='%{prog}s 1.0-dev')
self._arg_parser.add_argument('-X', action='store_true', help='enable debug output', dest='debug_enabled')
subparsers = self._arg_parser.add_subparsers(title='Jake sub-commands', dest='cmd', metavar='')
for subcommand in self._subcommands.keys():
self._subcommands[subcommand].setup_argument_parser(subparsers=subparsers)
def _debug_message(self, message: str):
if self._DEBUG_ENABLED:
print('[DEBUG] - {} - {}'.format(datetime.now(), message))
@staticmethod
def _get_jake_version():
return pkg_resources.get_distribution('jake').version
@staticmethod
def _print_jake_header():
""" Prints the banner, most of the user facing commands start with this """
cprint(figlet_format('Jake', font='isometric4'), 'green', attrs=[])
cprint(figlet_format('..the snake..', font='invita'), 'blue', attrs=['dark'])
print("Jake Version: {}".format(JakeCmd._get_jake_version()))
print('Put your Python dependencies in a chokehold')
print('')
@staticmethod
def _error_and_exit(message: str, exit_code: int = 1):
print('[ERROR] - {} - {}'.format(datetime.now(), message))
exit(exit_code)
def _parse_arguments(self):
self._arguments = self._arg_parser.parse_args()
def main():
JakeCmd().execute()
if __name__ == "__main__":
main()