Skip to content

Commit

Permalink
CM-29960 - Migrate all CLI commands to new project structure (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshalX authored Dec 6, 2023
1 parent 91d3ea5 commit 16bc696
Show file tree
Hide file tree
Showing 43 changed files with 711 additions and 628 deletions.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import click

from cycode.cli.auth.auth_manager import AuthManager
from cycode.cli.commands.auth.auth_manager import AuthManager
from cycode.cli.exceptions.custom_exceptions import AuthProcessError, HttpUnauthorizedError, NetworkError
from cycode.cli.models import CliError, CliErrors, CliResult
from cycode.cli.printers import ConsolePrinter
Expand All @@ -15,7 +15,7 @@
invoke_without_command=True, short_help='Authenticate your machine to associate the CLI with your Cycode account.'
)
@click.pass_context
def authenticate(context: click.Context) -> None:
def auth_command(context: click.Context) -> None:
"""Authenticates your machine."""
if context.invoked_subcommand is not None:
# if it is a subcommand, do nothing
Expand All @@ -33,7 +33,7 @@ def authenticate(context: click.Context) -> None:
_handle_exception(context, e)


@authenticate.command(
@auth_command.command(
name='check', short_help='Checks that your machine is associating the CLI with your Cycode account.'
)
@click.pass_context
Expand Down
File renamed without changes.
79 changes: 79 additions & 0 deletions cycode/cli/commands/main_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import logging
from typing import Optional

import click

from cycode.cli.commands.auth.auth_command import auth_command
from cycode.cli.commands.configure.configure_command import configure_command
from cycode.cli.commands.ignore.ignore_command import ignore_command
from cycode.cli.commands.report.report_command import report_command
from cycode.cli.commands.scan.scan_command import scan_command
from cycode.cli.commands.version.version_command import version_command
from cycode.cli.consts import (
CLI_CONTEXT_SETTINGS,
)
from cycode.cli.user_settings.configuration_manager import ConfigurationManager
from cycode.cli.utils.progress_bar import SCAN_PROGRESS_BAR_SECTIONS, get_progress_bar
from cycode.cyclient.config import set_logging_level
from cycode.cyclient.cycode_client_base import CycodeClientBase
from cycode.cyclient.models import UserAgentOptionScheme


@click.group(
commands={
'scan': scan_command,
'report': report_command,
'configure': configure_command,
'ignore': ignore_command,
'auth': auth_command,
'version': version_command,
},
context_settings=CLI_CONTEXT_SETTINGS,
)
@click.option(
'--verbose',
'-v',
is_flag=True,
default=False,
help='Show detailed logs.',
)
@click.option(
'--no-progress-meter',
is_flag=True,
default=False,
help='Do not show the progress meter.',
)
@click.option(
'--output',
'-o',
default='text',
help='Specify the output type (the default is text).',
type=click.Choice(['text', 'json', 'table']),
)
@click.option(
'--user-agent',
default=None,
help='Characteristic JSON object that lets servers identify the application.',
type=str,
)
@click.pass_context
def main_cli(
context: click.Context, verbose: bool, no_progress_meter: bool, output: str, user_agent: Optional[str]
) -> None:
context.ensure_object(dict)
configuration_manager = ConfigurationManager()

verbose = verbose or configuration_manager.get_verbose_flag()
context.obj['verbose'] = verbose
if verbose:
set_logging_level(logging.DEBUG)

context.obj['output'] = output
if output == 'json':
no_progress_meter = True

context.obj['progress_bar'] = get_progress_bar(hidden=no_progress_meter, sections=SCAN_PROGRESS_BAR_SECTIONS)

if user_agent:
user_agent_option = UserAgentOptionScheme().loads(user_agent)
CycodeClientBase.enrich_user_agent(user_agent_option.user_agent_suffix)
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from cycode.cli import consts
from cycode.cli.commands.report.sbom.common import create_sbom_report, send_report_feedback
from cycode.cli.commands.report.sbom.handle_errors import handle_report_exception
from cycode.cli.exceptions.handle_report_sbom_errors import handle_report_exception
from cycode.cli.files_collector.path_documents import get_relevant_document
from cycode.cli.files_collector.sca.sca_code_scanner import perform_pre_scan_documents_actions
from cycode.cli.files_collector.zip_documents import zip_documents
Expand All @@ -15,7 +15,7 @@
@click.command(short_help='Generate SBOM report for provided path in the command.')
@click.argument('path', nargs=1, type=click.Path(exists=True, resolve_path=True), required=True)
@click.pass_context
def sbom_path_command(context: click.Context, path: str) -> None:
def path_command(context: click.Context, path: str) -> None:
client = get_report_cycode_client()
report_parameters = context.obj['report_parameters']
output_format = report_parameters.output_format
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import click

from cycode.cli.commands.report.sbom.common import create_sbom_report, send_report_feedback
from cycode.cli.commands.report.sbom.handle_errors import handle_report_exception
from cycode.cli.exceptions.handle_report_sbom_errors import handle_report_exception
from cycode.cli.utils.get_api_client import get_report_cycode_client
from cycode.cli.utils.progress_bar import SbomReportProgressBarSection


@click.command(short_help='Generate SBOM report for provided repository URI in the command.')
@click.argument('uri', nargs=1, type=str, required=True)
@click.pass_context
def sbom_repository_url_command(context: click.Context, uri: str) -> None:
def repository_url_command(context: click.Context, uri: str) -> None:
progress_bar = context.obj['progress_bar']
progress_bar.start()
progress_bar.set_section_length(SbomReportProgressBarSection.PREPARE_LOCAL_FILES)
Expand Down
8 changes: 4 additions & 4 deletions cycode/cli/commands/report/sbom/sbom_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

import click

from cycode.cli.commands.report.sbom.sbom_path_command import sbom_path_command
from cycode.cli.commands.report.sbom.sbom_repository_url_command import sbom_repository_url_command
from cycode.cli.commands.report.sbom.path.path_command import path_command
from cycode.cli.commands.report.sbom.repository_url.repository_url_command import repository_url_command
from cycode.cli.config import config
from cycode.cyclient.report_client import ReportParameters


@click.group(
commands={
'path': sbom_path_command,
'repository_url': sbom_repository_url_command,
'path': path_command,
'repository_url': repository_url_command,
},
short_help='Generate SBOM report for remote repository by url or local directory by path.',
)
Expand Down
Empty file.
Loading

0 comments on commit 16bc696

Please sign in to comment.