Skip to content

Commit

Permalink
feat:add headless arg to help
Browse files Browse the repository at this point in the history
  • Loading branch information
SafetyQuincyF committed Oct 31, 2024
1 parent 9ee0b67 commit 82a2222
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Safety --help",
"type": "debugpy",
"request": "launch",
"module": "safety",
"args": [
"--help"
],
"console": "integratedTerminal"
},
{
"name": "Safety Auth Login",
"type": "debugpy",
Expand Down
16 changes: 5 additions & 11 deletions safety/auth/cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from safety.auth.models import Organization, Auth
from safety.auth.utils import S3PresignedAdapter, SafetyAuthSession, get_keys, is_email_verified
from safety.constants import REQUEST_TIMEOUT
from safety.scan.constants import CLI_KEY_HELP, CLI_PROXY_HOST_HELP, CLI_PROXY_PORT_HELP, CLI_PROXY_PROTOCOL_HELP, CLI_STAGE_HELP
from safety.scan.constants import CLI_HEADLESS_AUTH_HELP, CLI_KEY_HELP, CLI_PROXY_HOST_HELP, CLI_PROXY_PORT_HELP, CLI_PROXY_PROTOCOL_HELP, CLI_STAGE_HELP
from safety.scan.util import Stage
from safety.util import DependentOption, SafetyContext, get_proxy_dict

Expand Down Expand Up @@ -136,27 +136,21 @@ def proxy_options(func: Callable) -> Callable:

return func

def auth_options(stage: bool = True) -> Callable:
def auth_options() -> Callable:
"""
Decorator that defines authentication options for Click commands.
Args:
stage (bool): Whether to include the stage option.
Returns:
Callable: The decorator function.
"""
def decorator(func: Callable) -> Callable:

func = click.option("--key", default=None, envvar="SAFETY_API_KEY",
help=CLI_KEY_HELP)(func)

if stage:
func = click.option("--stage", default=None, envvar="SAFETY_STAGE",
help=CLI_STAGE_HELP)(func)

func = click.option("--headless",
help=CLI_HEADLESS_AUTH_HELP)(func)
return func

return decorator


Expand Down
10 changes: 3 additions & 7 deletions safety/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def print_deprecation_message(

@cli.command(cls=SafetyCLILegacyCommand, utility_command=True, help=CLI_CHECK_COMMAND_HELP)
@proxy_options
@auth_options(stage=False)
@auth_options()
@click.option("--db", default="",
help="Path to a local or remote vulnerability database. Default: empty")
@click.option("--full-report/--short-report", default=False, cls=MutuallyExclusiveOption,
Expand Down Expand Up @@ -467,7 +467,7 @@ def inner(ctx, *args, **kwargs):

@cli.command(cls=SafetyCLILegacyCommand, utility_command=True, help=CLI_LICENSES_COMMAND_HELP)
@proxy_options
@auth_options(stage=False)
@auth_options()
@click.option("--db", default="",
help="Path to a local license database. Default: empty")
@click.option('--output', "-o", type=click.Choice(['screen', 'text', 'json', 'bare'], case_sensitive=False),
Expand Down Expand Up @@ -644,9 +644,6 @@ def fail_validation(e):
cls=DependentOption,
required_options=['organization_id'],
help=CLI_CONFIGURE_ORGANIZATION_NAME)
@click.option("--stage", "-stg", multiple=False, default=Stage.development.value,
type=click.Choice([stage.value for stage in Stage]),
help="The project development stage to be tied to the current device.")
@click.option("--save-to-system/--save-to-user", default=False, is_flag=True,
help=CLI_CONFIGURE_SAVE_TO_SYSTEM)
@click.pass_context
Expand Down Expand Up @@ -706,8 +703,7 @@ def configure(ctx, proxy_protocol, proxy_host, proxy_port, proxy_timeout,
for key, value in proxy_config.items():
config.set(PROXY_SECTION_NAME, key, value)

if stage:
config['host'] = {'stage': "development" if stage == "dev" else stage}


try:
with open(CONFIG_FILE, 'w') as configfile:
Expand Down
11 changes: 8 additions & 3 deletions safety/scan/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
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."\
"\nExample: safety auth login"
CLI_AUTH_COMMAND_HELP = (
"Authenticate with Safety CLI to perform scans.\n"
"If already authenticated, your default browser will automatically open to https://platform.safetycli.com."
"\nExample: safety auth login\n"
"For headless authentication, you will receive a URL to paste into an external browser."
"\nExample: safety auth login --headless"
)
CLI_SCAN_COMMAND_HELP = "Scans a Python project directory."\
"\nExample: safety scan to scan the current directory"
CLI_SYSTEM_SCAN_COMMAND_HELP = "\\[beta] Run a comprehensive scan for packages and vulnerabilities across your entire machine/environment."\
Expand Down Expand Up @@ -61,7 +66,7 @@
"[nhc]For development stage scans unset the API key and authenticate using [bold]safety auth[/bold].[/nhc]\n\n" \
"[nhc]Tip: the API key can also be set using the environment variable: SAFETY_API_KEY[/nhc]\n\n"\
"[bold]Example: safety --key API_KEY --stage cicd scan[/bold]"

CLI_HEADLESS_AUTH_HELP = "For headless authentication, you will receive a URL to paste into an external browser, \n\n [bold]Example: safety auth login --headless[/bold]\n\n"
CLI_STAGE_HELP = "Assign a development lifecycle stage to your scan (default: development).\n\n" \
"[nhc]This labels the scan and its findings in Safety Platform with this stage.[/nhc]\n\n" \
"[bold]Example: safety --stage production scan[/bold]"
Expand Down

0 comments on commit 82a2222

Please sign in to comment.