Skip to content

Commit

Permalink
wip: migrating Jake to use cyclonedx-python-lib and ossindex-lib
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Horton <phorton@sonatype.com>
  • Loading branch information
madpah committed Sep 15, 2021
1 parent fc2ecb1 commit 23f6412
Show file tree
Hide file tree
Showing 32 changed files with 1,203 additions and 2,041 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,6 @@ dmypy.json
/.circleci/local-config.yml

.DS_Store

# .sonatype-config for VS Code plugin
.sonatype-config
4 changes: 0 additions & 4 deletions .pylintrc

This file was deleted.

1 change: 0 additions & 1 deletion MANIFEST.in

This file was deleted.

44 changes: 0 additions & 44 deletions Pipfile

This file was deleted.

484 changes: 0 additions & 484 deletions Pipfile.lock

This file was deleted.

121 changes: 121 additions & 0 deletions jake/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/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
import os
import sys
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.config import ConfigCommand
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='CycloneDX SBOM Generator')

# 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: v{}".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()
19 changes: 19 additions & 0 deletions jake/command/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import argparse
from abc import ABC, abstractmethod


class BaseCommand(ABC):
# Parsed Arguments
_arguments: argparse.Namespace

@abstractmethod
def handle_args(self):
pass

def execute(self, arguments: argparse.Namespace):
self._arguments = arguments
self.handle_args()

@abstractmethod
def setup_argument_parser(self, subparsers: argparse._SubParsersAction):
pass
20 changes: 20 additions & 0 deletions jake/command/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import argparse
from . import BaseCommand


class ConfigCommand(BaseCommand):

def setup_argument_parser(self, subparsers: argparse._SubParsersAction):
parser_config: argparse.ArgumentParser = subparsers.add_parser(
'config',
help='configure jake for OSS Index or Nexus Lifecycle access'
)

parser_config.add_argument('oss', help='configure Nexus IQ Server or OSSIndex', nargs='?',
choices=('iq', 'oss'))

# iq_group = parser_config.add_argument_group('iq')
# iq_group.



24 changes: 24 additions & 0 deletions jake/command/iq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import argparse
from . import BaseCommand


class IqCommand(BaseCommand):

def handle_args(self):
pass

def setup_argument_parser(self, subparsers: argparse._SubParsersAction):
parser: argparse.ArgumentParser = subparsers.add_parser('iq', help='perform a scan backed by Nexus Lifecycle')

parser.add_argument('-s', '--server-url', help='Full http(s):// URL to your Nexus Lifecycle server',
metavar='https://localhost:8070', required=True, dest='iq_server_url')

parser.add_argument('-i', '--application-id', help='Public Application ID in Nexus Lifecycle',
metavar='APP_ID', required=True, dest='iq_application_id')

parser.add_argument('-u', '--username', help='Username for authentication to Nexus Lifecycle',
metavar='USER_ID', required=True, dest='iq_username')

parser.add_argument('-p', '--password', help='Password for authentication to Nexus Lifecycle',
metavar='PASSWORD', required=True, dest='iq_password')

Loading

0 comments on commit 23f6412

Please sign in to comment.