8000 feat: add codebase init command by yeisonvargasf · Pull Request #756 · pyupio/safety · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add codebase init command #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions safety/auth/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from safety.auth.models import Auth
from safety.auth.utils import initialize, is_email_verified
from safety.console import main_console as console
from safety.constants import MSG_FINISH_REGISTRATION_TPL, MSG_VERIFICATION_HINT
from safety.constants import MSG_FINISH_REGISTRATION_TPL, MSG_VERIFICATION_HINT, DEFAULT_EPILOG
from safety.meta import get_version
from safety.decorators import notify

Expand Down Expand Up @@ -37,7 +37,6 @@
CLI_AUTH_LOGIN_HELP,
CLI_AUTH_LOGOUT_HELP,
CLI_AUTH_STATUS_HELP,
DEFAULT_EPILOG,
)

from ..cli_util import SafetyCLISubGroup, get_command_for, pass_safety_cli_obj
Expand Down
6 changes: 4 additions & 2 deletions safety/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from safety.auth.cli import auth_app
from safety.auth.models import Organization
from safety.decorators import notify
from safety.codebase.command import codebase_app
from safety.console import main_console as console
from safety.constants import (
BAR_LINE,
Expand All @@ -42,6 +43,8 @@
EXIT_CODE_FAILURE,
EXIT_CODE_OK,
EXIT_CODE_VULNERABILITIES_FOUND,
CLI_MAIN_INTRODUCTION,
DEFAULT_EPILOG,
)
from safety.error_handlers import handle_cmd_exception, output_exception
from safety.errors import InvalidCredentialError, SafetyError, SafetyException
Expand Down Expand Up @@ -70,8 +73,6 @@
CLI_GENERATE_MINIMUM_CVSS_SEVERITY,
CLI_GENERATE_PATH,
CLI_LICENSES_COMMAND_HELP,
CLI_MAIN_INTRODUCTION,
DEFAULT_EPILOG,
DEFAULT_SPINNER,
)
from safety.scan.finder import FileFinder
Expand Down Expand Up @@ -1410,6 +1411,7 @@ def check_updates(
cli.add_command(typer.main.get_command(init_app), name="init")
cli.add_command(typer.main.get_command(scan_project_app), name="scan")
cli.add_command(typer.main.get_command(scan_system_app), name="system-scan")
cli.add_command(typer.main.get_command(codebase_app), name="codebase")

tool_commands.auto_register_tools(group=cli)

Expand Down
57 changes: 57 additions & 0 deletions safety/codebase/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import logging
from os import name
import typer
from safety.cli_util import SafetyCLISubGroup, SafetyCLICommand
from .constants import CMD_CODEBASE_INIT_NAME, CMD_HELP_CODEBASE_INIT, CMD_HELP_CODEBASE, CMD_CODEBASE_GROUP_NAME

from ..cli_util import get_command_for
from ..error_handlers import handle_cmd_exception
from ..decorators import notify
from ..constants import DEFAULT_EPILOG
from safety.console import main_console as console

logger = logging.getLogger(__name__)

cli_apps_opts = {"rich_markup_mode": "rich", "cls": SafetyCLISubGroup, "name": CMD_CODEBASE_GROUP_NAME}
codebase_app = typer.Typer(**cli_apps_opts)

DEFAULT_CMD = CMD_CODEBASE_INIT_NAME


@codebase_app.callback(
invoke_without_command=True,
cls=SafetyCLISubGroup,
help=CMD_HELP_CODEBASE,
epilog=DEFAULT_EPILOG,
context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
)
def codebase(
ctx: typer.Context,
):
"""
Group command for Safety Codebase (project) operations. Running this command will forward to the default command.
"""
logger.info("codebase started")

# If no subcommand is invoked, forward to the default command
if not ctx.invoked_subcommand:
default_command = get_command_for(name=DEFAULT_CMD, typer_instance=codebase_app)
return ctx.forward(default_command)



@codebase_app.command(
cls=SafetyCLICommand,
help=CMD_HELP_CODEBASE_INIT,
name=CMD_CODEBASE_INIT_NAME,
epilog=DEFAULT_EPILOG,
options_metavar="[OPTIONS]",
context_settings={"allow_extra_args": True, "ignore_unknown_options": True},
)
@handle_cmd_exception
@notify
def init(ctx: typer.Context):
"""
Initialize a Safety Codebase (project). The codebase may be entirely new to Safety Platform, or may already exist in Safety Platform and the user is wanting to initialize it locally.
"""
console.print("Initializing Safety Codebase...")
7 changes: 7 additions & 0 deletions safety/codebase/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


CMD_HELP_CODEBASE_INIT = "Initialize a Safety Codebase (project). The codebase may be entirely new to Safety Platform, or may already exist in Safety Platform and the user is wanting to initialize it locally."
CMD_HELP_CODEBASE = "Run operations for Safety Codebase (project)."

CMD_CODEBASE_GROUP_NAME = "codebase"
CMD_CODEBASE_INIT_NAME = "init"
19 changes: 19 additions & 0 deletions safety/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pathlib import Path
from typing import Optional

from safety.meta import get_version

JSON_SCHEMA_VERSION = "2.0.0"

# TODO fix this
Expand Down Expand Up @@ -232,3 +234,20 @@ def get_config_setting(name: str, default=None) -> Optional[str]:

CONTEXT_COMMAND_TYPE = "command_type"
CONTEXT_FEATURE_TYPE = "feature_type"

CLI_VERSION = get_version()
CLI_WEBSITE_URL="https://safetycli.com"
CLI_DOCUMENTATION_URL="https://docs.safetycli.com"
CLI_SUPPORT_EMAIL="support@safetycli.com"

# Main Safety --help data:
CLI_MAIN_INTRODUCTION = f"Safety CLI 3 - Vulnerability Scanning for Secure Python Development\n\n" \
"Leverage the most comprehensive vulnerability data available to secure your projects against vulnerable and malicious packages. Safety CLI is a Python dependency vulnerability scanner that enhances software supply chain security at every stage of development.\n\n" \
f"Documentation: {CLI_DOCUMENTATION_URL}\n"\
f"Contact: {CLI_SUPPORT_EMAIL}\n"

DEFAULT_EPILOG = f"\nSafety CLI version: {CLI_VERSION}\n" \
f"\nDocumentation: {CLI_DOCUMENTATION_URL}\n\n\n\n" \
"Made with love by Safety Cybersecurity\n\n" \
f"{CLI_WEBSITE_URL}\n\n"\
f"{CLI_SUPPORT_EMAIL}\n"
3 changes: 1 addition & 2 deletions safety/firewall/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

# TODO: refactor this import and the related code
# For now, let's keep it as is
from safety.scan.constants import DEFAULT_EPILOG
from safety.error_handlers import handle_cmd_exception

from ..cli_util import (
Expand All @@ -21,7 +20,7 @@
SafetyCLISubGroup,
pass_safety_cli_obj,
)
from ..constants import CONTEXT_COMMAND_TYPE, CONTEXT_FEATURE_TYPE, EXIT_CODE_OK
from ..constants import CONTEXT_COMMAND_TYPE, CONTEXT_FEATURE_TYPE, EXIT_CODE_OK, DEFAULT_EPILOG
from ..tool.interceptors import create_interceptor
from ..tool.main import reset_system
from .constants import (
Expand Down
3 changes: 1 addition & 2 deletions safety/scan/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
from typing import Any, Dict, List, Optional, Set, Tuple, Callable

from safety.constants import EXIT_CODE_VULNERABILITIES_FOUND
from safety.constants import EXIT_CODE_VULNERABILITIES_FOUND, DEFAULT_EPILOG
from safety.safety import process_fixes_scan
from safety.scan.finder.handlers import ECOSYSTEM_HANDLER_MAPPING, FileHandler
from safety.scan.validators import output_callback, save_as_callback
Expand All @@ -29,7 +29,6 @@
CMD_SYSTEM_NAME,
DEFAULT_SPINNER,
SCAN_OUTPUT_HELP,
DEFAULT_EPILOG,
SCAN_POLICY_FILE_HELP,
SCAN_SAVE_AS_HELP,
SCAN_TARGET_HELP,
Expand Down
19 changes: 0 additions & 19 deletions safety/scan/constants.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
from safety.meta import get_version

# Console Help Theme
CONSOLE_HELP_THEME = {
"nhc": "grey82"
}

CLI_VERSION = get_version()
CLI_WEBSITE_URL="https://safetycli.com"
CLI_DOCUMENTATION_URL="https://docs.safetycli.com"
CLI_SUPPORT_EMAIL="support@safetycli.com"

# Main Safety --help data:
CLI_MAIN_INTRODUCTION = f"Safety CLI 3 - Vulnerability Scanning for Secure Python Development\n\n" \
"Leverage the most comprehensive vulnerability data available to secure your projects against vulnerable and malicious packages. Safety CLI is a Python dependency vulnerability scanner that enhances software supply chain security at every stage of development.\n\n" \
f"Documentation: {CLI_DOCUMENTATION_URL}\n"\
f"Contact: {CLI_SUPPORT_EMAIL}\n"

CLI_AUTH_COMMAND_HELP = (
"Authenticate Safety CLI to perform scans. Your default browser will automatically open to "
"https://platform.safetycli.com.\n\n"
Expand Down Expand Up @@ -118,12 +105,6 @@
CLI_AUTH_STATUS_HELP = "Show the current authentication status.\n\n" \
"[bold]Example: safety auth status[/bold]"

DEFAULT_EPILOG = f"\nSafety CLI version: {CLI_VERSION}\n" \
f"\nDocumentation: {CLI_DOCUMENTATION_URL}\n\n\n\n" \
"Made with love by Safety Cybersecurity\n\n" \
f"{CLI_WEBSITE_URL}\n\n"\
f"{CLI_SUPPORT_EMAIL}\n"

# Configure options
CLI_CONFIGURE_PROXY_HOST_HELP = "Specify a proxy host for network communications to be saved into Safety's configuration. \n\n"
CLI_CONFIGURE_PROXY_PORT_HELP = "Set the proxy port to be saved into Safety's configuration file (default: 80).\n\n"
Expand Down
Loading
0