-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f97fb15
commit ae967ce
Showing
28 changed files
with
2,203 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 8 additions & 1 deletion
9
docs/.ipynb_checkpoints/Safety-CLI-Quickstart-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from pathlib import Path | ||
|
||
from rich.prompt import Prompt | ||
from ..cli_util import SafetyCLICommand, SafetyCLISubGroup | ||
import typer | ||
import os | ||
|
||
from safety.scan.decorators import initialize_scan | ||
from safety.init.constants import PROJECT_INIT_CMD_NAME, PROJECT_INIT_HELP, PROJECT_INIT_DIRECTORY_HELP | ||
from safety.init.main import create_project | ||
from safety.console import main_console as console | ||
from ..scan.command import scan | ||
from ..scan.models import ScanOutput | ||
from ..tool.main import configure_system, configure_local_directory, has_local_tool_files, configure_alias | ||
|
||
try: | ||
from typing import Annotated | ||
except ImportError: | ||
from typing_extensions import Annotated | ||
|
||
init_app = typer.Typer(rich_markup_mode= "rich", cls=SafetyCLISubGroup) | ||
|
||
@init_app.command( | ||
cls=SafetyCLICommand, | ||
help=PROJECT_INIT_HELP, | ||
name=PROJECT_INIT_CMD_NAME, | ||
options_metavar="[OPTIONS]", | ||
context_settings={ | ||
"allow_extra_args": True, | ||
"ignore_unknown_options": True | ||
}, | ||
) | ||
def init(ctx: typer.Context, | ||
directory: Annotated[ | ||
Path, | ||
typer.Argument( | ||
exists=True, | ||
file_okay=False, | ||
dir_okay=True, | ||
writable=False, | ||
readable=True, | ||
resolve_path=True, | ||
show_default=False, | ||
help=PROJECT_INIT_DIRECTORY_HELP | ||
), | ||
] = Path(".")): | ||
|
||
do_init(ctx, directory, False) | ||
|
||
|
||
def do_init(ctx: typer.Context, directory: Path, prompt_user: bool = True): | ||
project_dir = directory if os.path.isabs(directory) else os.path.join(os.getcwd(), directory) | ||
initialize_scan(ctx, console) | ||
create_project(ctx, console, Path(project_dir)) | ||
|
||
answer = 'y' if not prompt_user else None | ||
if prompt_user: | ||
console.print( | ||
"Safety prevents vulnerable or malicious packages from being installed on your computer. We do this by wrapping your package manager.") | ||
prompt = "Do you want to enable proactive malicious package prevention?" | ||
answer = Prompt.ask(prompt=prompt, choices=["y", "n"], | ||
default="y", show_default=True, console=console).lower() | ||
|
||
if answer == 'y': | ||
configure_system() | ||
|
||
if prompt_user: | ||
prompt = "Do you want to alias pip to Safety?" | ||
answer = Prompt.ask(prompt=prompt, choices=["y", "n"], | ||
default="y", show_default=True, console=console).lower() | ||
|
||
if answer == 'y': | ||
configure_alias() | ||
|
||
if has_local_tool_files(project_dir): | ||
if prompt_user: | ||
prompt = "Do you want to enable proactive malicious package prevention for any project in working directory?" | ||
answer = Prompt.ask(prompt=prompt, choices=["y", "n"], | ||
default="y", show_default=True, console=console).lower() | ||
|
||
if answer == 'y': | ||
configure_local_directory(project_dir) | ||
|
||
if prompt_user: | ||
prompt = "It looks like your current directory contains a requirements.txt file. Would you like Safety to scan it?" | ||
answer = Prompt.ask(prompt=prompt, choices=["y", "n"], | ||
default="y", show_default=True, console=console).lower() | ||
|
||
if answer == 'y': | ||
ctx.command.name = "scan" | ||
ctx.params = { | ||
"target": directory, | ||
"output": ScanOutput.SCREEN, | ||
"policy_file_path": None | ||
} | ||
scan(ctx=ctx, target=directory, output=ScanOutput.SCREEN, policy_file_path=None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Project options | ||
PROJECT_INIT_CMD_NAME = "init" | ||
PROJECT_INIT_HELP = "Creates new Safety CLI project in the current working directory."\ | ||
"\nExample: safety project init" | ||
PROJECT_INIT_DIRECTORY_HELP = "Defines a directory for creating a new project. (default: current directory)\n\n" \ | ||
"[bold]Example: safety project init /path/to/project[/bold]" |
Oops, something went wrong.