Skip to content

Commit

Permalink
osemgrep: skeleton for the login/logout/ci subcommands (semgrep#6389)
Browse files Browse the repository at this point in the history
test plan:
make test

make osemgrep-e2e => 65 tests are passing.
  • Loading branch information
aryx authored Oct 25, 2022
1 parent e51c332 commit 5a543eb
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 9 deletions.
6 changes: 0 additions & 6 deletions semgrep-core/src/osemgrep/TOPORT/commands/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ def make_login_url() -> Tuple[uuid.UUID, str]:
@handle_command_errors
def login() -> NoReturn:
"""
Obtain and save credentials for semgrep.dev
Looks for an semgrep.dev API token in the environment variable SEMGREP_API_TOKEN_SETTINGS_KEY.
If not defined and running in a TTY, prompts interactively.
Once token is found, saves it to global settings file
"""
state = get_state()
saved_login_token = auth._read_token_from_settings_file()
Expand Down Expand Up @@ -100,7 +95,6 @@ def save_token(login_token: Optional[str], echo_token: bool) -> bool:
@handle_command_errors
def logout() -> None:
"""
Remove locally stored credentials to semgrep.dev
"""
auth.delete_token()
click.echo("Logged out (log back in with `semgrep login`)")
6 changes: 3 additions & 3 deletions semgrep-core/src/osemgrep/cli/CLI.ml
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ let dispatch_subcommand argv =
in
(* coupling: with known_subcommands above *)
match subcmd with
| "ci" -> missing_subcommand ()
| "login" -> missing_subcommand ()
| "logout" -> missing_subcommand ()
| "ci" -> Ci_subcommand.main subcmd_argv
| "login" -> Login_subcommand.main subcmd_argv
| "logout" -> Logout_subcommand.main subcmd_argv
| "lsp" -> missing_subcommand ()
| "publish" -> missing_subcommand ()
| "scan" -> Scan_subcommand.main subcmd_argv
Expand Down
2 changes: 2 additions & 0 deletions semgrep-core/src/osemgrep/cli/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

; Internal libraries
osemgrep_cli_scan
osemgrep_cli_login
osemgrep_cli_ci
; reusing code from Core_cli
semgrep_core_cli
)
Expand Down
21 changes: 21 additions & 0 deletions semgrep-core/src/osemgrep/cli_ci/Ci_CLI.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(*
'semgrep ci' subcommand
Translated from ci.py
*)

(*****************************************************************************)
(* Types and constants *)
(*****************************************************************************)

type conf = Scan_CLI.conf

(*****************************************************************************)
(* Entry point *)
(*****************************************************************************)

let parse_argv (argv : string array) : (conf, Exit_code.t) result =
Scan_CLI.parse_argv argv
19 changes: 19 additions & 0 deletions semgrep-core/src/osemgrep/cli_ci/Ci_CLI.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(*
'semgrep ci' command-line parsing.
*)

(*
The result of parsing a 'semgrep ci' command.
*)

type conf = Scan_CLI.conf

(*
Usage: parse_argv [| "semgrep-ci"; <args> |]
This function returns an exit code to be passed to the 'exit' function
if there was an error parsing argv (Exit_code.fatal) or when
using semgrep ci --help (Exit_code.ok), and the conf otherwise if everything
went fine.
*)
val parse_argv : string array -> (conf, Exit_code.t) result
42 changes: 42 additions & 0 deletions semgrep-core/src/osemgrep/cli_ci/Ci_subcommand.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
let _logger = Logging.get_logger [ __MODULE__ ]

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(*
Parse a semgrep-ci command, execute it and exit.
Translated from ci.py
*)

(*****************************************************************************)
(* Types *)
(*****************************************************************************)

(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)

(*****************************************************************************)
(* Main logic *)
(*****************************************************************************)

(* All the business logic after command-line parsing. Return the desired
exit code. *)
let run (_conf : Ci_CLI.conf) : Exit_code.t =
(* TODO:
Setup_logging.setup config;
logger#info "Executed as: %s" (Sys.argv |> Array.to_list |> String.concat " ");
logger#info "Version: %s" config.version;
*)
Exit_code.ok

(*****************************************************************************)
(* Entry point *)
(*****************************************************************************)

let main (argv : string array) : Exit_code.t =
let res = Ci_CLI.parse_argv argv in
match res with
| Ok conf -> CLI_common.safe_run run conf
| Error exit_code -> exit_code
12 changes: 12 additions & 0 deletions semgrep-core/src/osemgrep/cli_ci/Ci_subcommand.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(*
Parse a semgrep-ci command, execute it and exit.
Usage: main [| "semgrep-ci"; ... |]
This function returns an exit code to be passed to the 'exit' function.
Exceptions are caught and turned into an appropriate exit code.
*)
val main : string array -> Exit_code.t

(* internal *)
val run : Ci_CLI.conf -> Exit_code.t
22 changes: 22 additions & 0 deletions semgrep-core/src/osemgrep/cli_ci/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; OCaml implementation of the 'semgrep ci' subcommand.
;
(library
(name osemgrep_cli_ci)
(wrapped false)
(libraries
cmdliner

osemgrep_core
osemgrep_utils
osemgrep_networking
osemgrep_cli_scan ; reusing the same flags and most of the code
)
(preprocess
(pps
ppx_profiling
ppx_deriving.show
ppx_deriving.eq
ppx_hash
)
)
)
78 changes: 78 additions & 0 deletions semgrep-core/src/osemgrep/cli_login/Login_subcommand.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
open Cmdliner

let _logger = Logging.get_logger [ __MODULE__ ]

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(*
Parse a semgrep-login command, execute it and exit.
Translated from login.py
*)

(*****************************************************************************)
(* Types *)
(*****************************************************************************)
(* no CLI parameters for now *)
type login_cli_conf = unit
type conf = login_cli_conf

(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)

(* this could be moved in a Login_CLI.ml file at some point *)
let cmdline_term : conf Term.t =
let combine = () in
Term.(const combine)

let doc = "Obtain and save credentials for semgrep.dev"

let man : Manpage.block list =
[
`S Manpage.s_description;
`P
"Obtain and save credentials for semgrep.dev\n\n\
\ Looks for an semgrep.dev API token in the environment variable \
SEMGREP_API_TOKEN_SETTINGS_KEY.\n\
\ If not defined and running in a TTY, prompts interactively.\n\
\ Once token is found, saves it to global settings file";
]
@ CLI_common.help_page_bottom

let parse_argv (argv : string array) : (conf, Exit_code.t) result =
let info : Cmd.info = Cmd.info "semgrep login" ~doc ~man in
let cmd : conf Cmd.t = Cmd.v info cmdline_term in
match Cmd.eval_value ~argv cmd with
| Error _n -> Error Exit_code.fatal
| Ok ok -> (
match ok with
| `Ok config -> Ok config
| `Version
| `Help ->
Error Exit_code.ok)

(*****************************************************************************)
(* Main logic *)
(*****************************************************************************)

(* All the business logic after command-line parsing. Return the desired
exit code. *)
let run (_conf : login_cli_conf) : Exit_code.t =
(* TODO:
Setup_logging.setup config;
logger#info "Executed as: %s" (Sys.argv |> Array.to_list |> String.concat " ");
logger#info "Version: %s" config.version;
*)
Exit_code.ok

(*****************************************************************************)
(* Entry point *)
(*****************************************************************************)

let main (argv : string array) : Exit_code.t =
let res = parse_argv argv in
match res with
| Ok conf -> CLI_common.safe_run run conf
| Error exit_code -> exit_code
15 changes: 15 additions & 0 deletions semgrep-core/src/osemgrep/cli_login/Login_subcommand.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(* no parameters for now *)
type login_cli_conf = unit

(*
Parse a semgrep-login command, execute it and exit.
Usage: main [| "semgrep-login"; ... |]
This function returns an exit code to be passed to the 'exit' function.
Exceptions are caught and turned into an appropriate exit code.
*)
val main : string array -> Exit_code.t

(* internal *)
val run : login_cli_conf -> Exit_code.t
73 changes: 73 additions & 0 deletions semgrep-core/src/osemgrep/cli_login/Logout_subcommand.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
open Cmdliner

let _logger = Logging.get_logger [ __MODULE__ ]

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(*
Parse a semgrep-logout command, execute it and exit.
Translated from login.py
*)

(*****************************************************************************)
(* Types *)
(*****************************************************************************)
(* no CLI parameters for now *)
type logout_cli_conf = unit
type conf = logout_cli_conf

(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)

(* this could be moved in a Login_CLI.ml file at some point *)
let cmdline_term : conf Term.t =
let combine = () in
Term.(const combine)

let doc = "Remove locally stored credentials to semgrep.dev"

let man : Manpage.block list =
[
`S Manpage.s_description;
`P "Remove locally stored credentials to semgrep.dev";
]
@ CLI_common.help_page_bottom

let parse_argv (argv : string array) : (conf, Exit_code.t) result =
let info : Cmd.info = Cmd.info "semgrep logout" ~doc ~man in
let cmd : conf Cmd.t = Cmd.v info cmdline_term in
match Cmd.eval_value ~argv cmd with
| Error _n -> Error Exit_code.fatal
| Ok ok -> (
match ok with
| `Ok config -> Ok config
| `Version
| `Help ->
Error Exit_code.ok)

(*****************************************************************************)
(* Main logic *)
(*****************************************************************************)

(* All the business logic after command-line parsing. Return the desired
exit code. *)
let run (_conf : logout_cli_conf) : Exit_code.t =
(* TODO:
Setup_logging.setup config;
logger#info "Executed as: %s" (Sys.argv |> Array.to_list |> String.concat " ");
logger#info "Version: %s" config.version;
*)
Exit_code.ok

(*****************************************************************************)
(* Entry point *)
(*****************************************************************************)

let main (argv : string array) : Exit_code.t =
let res = parse_argv argv in
match res with
| Ok conf -> CLI_common.safe_run run conf
| Error exit_code -> exit_code
15 changes: 15 additions & 0 deletions semgrep-core/src/osemgrep/cli_login/Logout_subcommand.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(* no parameters for now *)
type logout_cli_conf = unit

(*
Parse a semgrep-logout command, execute it and exit.
Usage: main [| "semgrep-logut"; ... |]
This function returns an exit code to be passed to the 'exit' function.
Exceptions are caught and turned into an appropriate exit code.
*)
val main : string array -> Exit_code.t

(* internal *)
val run : logout_cli_conf -> Exit_code.t
21 changes: 21 additions & 0 deletions semgrep-core/src/osemgrep/cli_login/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; OCaml implementation of the 'semgrep login' and 'semgrep logout' subcommands.
;
(library
(name osemgrep_cli_login)
(wrapped false)
(libraries
cmdliner

osemgrep_core
osemgrep_utils
osemgrep_networking
)
(preprocess
(pps
ppx_profiling
ppx_deriving.show
ppx_deriving.eq
ppx_hash
)
)
)

0 comments on commit 5a543eb

Please sign in to comment.