Something went wrong. Try again.
Correctness- and interoperability-oriented Tangled CLI
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399open Cmdliner
let ( let* ) = Result.bind
let camlet_exits = let info = Cmd.Exit.info in [ info 0 ~doc:"on success." ; info 2 ~doc:"for invalid Camlet command input." ; info 3 ~doc:"for invalid or missing configuration." ; info 4 ~doc:"when a requested resource is not found." ; info 5 ~doc:"when authentication fails." ; info 6 ~doc:"when authorization fails." ; info 7 ~doc:"when transport fails." ; info 8 ~doc:"when a remote protocol response is invalid." ; info 9 ~doc:"when a required external tool fails." ; info 10 ~doc:"when authoritative observations disagree." ; info 11 ~doc:"when an operation conflicts with observed state." ; info 70 ~doc:"when Camlet encounters an internal failure." ; info Cmd.Exit.cli_error ~doc:"when command-line parsing fails." ; info Cmd.Exit.internal_error ~doc:"when command evaluation raises unexpectedly." ]
let command_info ?version name ~doc = Cmd.info ?version name ~doc ~exits:camlet_exits
let json = Arg.( value & flag & info [ "j"; "json" ] ~doc:"Emit a versioned JSON document instead of human output.")
let emit_output ~json ~human ~machine value = let output = if json then machine value else human value in print_endline output; 0
let emit_error ~json error = if json then print_endline (Camlet_presentation.Error_output.json error) else prerr_endline (Camlet_presentation.Error_output.human error); Camlet_domain.App_error.exit_code error
let emit_result ~json ~human ~machine = function | Ok value -> emit_output ~json ~human ~machine value | Error error -> emit_error ~json error
let print_version json = emit_output ~json ~human:Camlet_presentation.Version_output.human ~machine:Camlet_presentation.Version_output.json Camlet_domain.Version.current
let version_command = let doc = "show Camlet version information" in let info = command_info "version" ~doc in Cmd.v info Term.(const print_version $ json)
let context_repo = Arg.( value & opt string "." & info [ "C"; "repo" ] ~docv:"PATH" ~doc:"Inspect the repository at $(docv).")
let context_ports = Camlet_application.Context_ports. { inspect_git = Camlet_adapter_git.Git_context.inspect ; inspect_jj = Camlet_adapter_jj.Jj_context.inspect }
let context json repo = Camlet_application.Resolve_context.run ~ports:context_ports ~repo |> emit_result ~json ~human:Camlet_presentation.Context_output.human ~machine:Camlet_presentation.Context_output.json
let context_command = let doc = "inspect local Git, Jujutsu, Tangled, and Radicle context" in let info = command_info "context" ~doc in Cmd.v info Term.(const context $ json $ context_repo)
let repository_option ~doc = Arg.(value & opt (some string) None & info [ "C"; "repo" ] ~docv:"PATH" ~doc)
let timeout_option ~default = Arg.( value & opt string default & info [ "timeout" ] ~docv:"SECONDS" ~doc:"Bound the complete network observation to $(docv).")
let view_repo = repository_option ~doc:"Discover the repository identity from Git remotes under $(docv)."
let view_target = Arg.( value & pos 0 (some string) None & info [] ~docv:"REPOSITORY_DID" ~doc:"Read this did:plc repository identity instead of local discovery.")
let view_timeout = timeout_option ~default:"10"
let list_repo = repository_option ~doc:"Discover the owner from a Tangled repository under $(docv)."
let list_target = Arg.( value & pos 0 (some string) None & info [] ~docv:"OWNER_DID" ~doc:"List repositories published by this did:plc owner identity.")
let list_timeout = timeout_option ~default:"30"
let cli_error ~code ~message = Camlet_domain.App_error.make ~category:Usage ~code ~message
let repository_target = function | None -> Ok None | Some value -> ( match Camlet_domain.Repository_did.of_string value with | Ok did -> Ok (Some did) | Error _ -> let code, message = if String.starts_with ~prefix:"did:" value then ( "identity.unsupported_did_method" , "Camlet supports only did:plc repository identities" ) else ( "identity.unsupported_locator" , "Repository URLs and handles are not supported by this command" ) in Error (cli_error ~code ~message))
let owner_target = function | None -> Ok None | Some value -> ( match Camlet_domain.Did.of_string value with | Ok did -> Ok (Some did) | Error _ -> let code, message = if String.starts_with ~prefix:"did:" value then ( "identity.unsupported_did_method" , "Camlet supports only did:plc owner identities" ) else ( "identity.unsupported_locator" , "Owner handles and URLs are not supported by this command" ) in Error (cli_error ~code ~message))
let timeout_value value = match float_of_string_opt value with | Some timeout when Float.is_finite timeout && timeout > 0. && timeout <= 60. -> Ok timeout | _ -> Error (cli_error ~code:"usage.timeout_invalid" ~message: "The timeout must be greater than zero and at most 60 seconds")
let validate_target_selection ~target_name repo target = match (repo, target) with | Some _, Some _ -> Error (cli_error ~code:"usage.target_conflict" ~message:("Use either " ^ target_name ^ " or --repo, not both")) | _ -> Ok ()
let current_timestamp () = match Camlet_domain.Timestamp.of_float_s (Unix.gettimeofday ()) with | Ok value -> Ok value | Error _ -> Error (Camlet_domain.App_error.make ~category:Internal ~code:"clock.invalid" ~message:"The system clock is outside the supported range")
let repo_view_ports client = Camlet_application.Repo_view_ports. { discover_local = Camlet_adapter_git.Tangled_identity.discover ; resolve_did = Camlet_adapter_plc.Did_resolver.resolve client ; describe_repository = Camlet_adapter_knot.Repository_reader.describe client ; read_record = Camlet_adapter_pds.Repository_reader.read client ; now = current_timestamp }
let run_repo_view repo target timeout_text = let* () = validate_target_selection ~target_name:"REPOSITORY_DID" repo target in let* timeout = timeout_value timeout_text in let* repository_did = repository_target target in let* client = Camlet_adapter_http.Http_client.create ~timeout_s:timeout in Camlet_application.View_repository.run ~ports:(repo_view_ports client) ~repo:(Option.value ~default:"." repo) ~repository_did
let repo_view json repo target timeout_text = run_repo_view repo target timeout_text |> emit_result ~json ~human:Camlet_presentation.Repo_output.human ~machine:Camlet_presentation.Repo_output.json
let repo_view_command = let doc = "read a repository through its authoritative services" in let info = command_info "view" ~doc in Cmd.v info Term.(const repo_view $ json $ view_repo $ view_target $ view_timeout)
let repo_list_ports client = Camlet_application.Repo_list_ports. { discover_local = Camlet_adapter_git.Tangled_identity.discover ; resolve_did = Camlet_adapter_plc.Did_resolver.resolve client ; describe_repository = Camlet_adapter_knot.Repository_reader.describe client ; list_records = Camlet_adapter_pds.Repository_lister.list_page client ; now = current_timestamp }
let run_repo_list repo target timeout_text = let* () = validate_target_selection ~target_name:"OWNER_DID" repo target in let* timeout = timeout_value timeout_text in let* owner_did = owner_target target in let* client = Camlet_adapter_http.Http_client.create ~timeout_s:timeout in Camlet_application.List_repositories.run ~ports:(repo_list_ports client) ~repo:(Option.value ~default:"." repo) ~owner_did
let repo_list json repo target timeout_text = run_repo_list repo target timeout_text |> emit_result ~json ~human:Camlet_presentation.Repo_list_output.human ~machine:Camlet_presentation.Repo_list_output.json
let repo_list_command = let doc = "list an owner's repositories through authoritative services" in let info = command_info "list" ~doc in Cmd.v info Term.(const repo_list $ json $ list_repo $ list_target $ list_timeout)
let repo_command = let doc = "inspect Tangled repository state" in Cmd.group (command_info "repo" ~doc) [ repo_view_command; repo_list_command ]
let issue_repo = repository_option ~doc:"Discover the repository identity from Git remotes under $(docv)."
let issue_target = Arg.( value & pos 0 (some string) None & info [] ~docv:"REPOSITORY_DID" ~doc:"List indexed issues for this did:plc repository identity.")
let issue_bobbin = Arg.( value & opt string "https://api.tangled.org" & info [ "bobbin" ] ~docv:"ORIGIN" ~doc:"Read aggregated issue observations from this HTTPS origin.")
let issue_state = Arg.( value & opt string "all" & info [ "state" ] ~docv:"STATE" ~doc:"Restrict observations to $(b,open) or $(b,closed); default: all.")
let issue_timeout = timeout_option ~default:"30"
let bobbin_origin value = match Camlet_domain.Origin.of_string value with | Ok origin -> Ok origin | Error _ -> Error (cli_error ~code:"issue.bobbin_invalid" ~message: "The Bobbin endpoint must be an HTTPS origin without a path, \ credentials, query, or fragment")
let issue_list_ports client = Camlet_application.Issue_list_ports. { discover_local = Camlet_adapter_git.Tangled_identity.discover ; list_issues = Camlet_adapter_bobbin.Issue_lister.list_page client ; read_coverage = Camlet_adapter_bobbin.Issue_lister.coverage client ; now = current_timestamp }
let issue_state_filter = function | "all" -> Ok Camlet_domain.Issue_list.All | "open" -> Ok Camlet_domain.Issue_list.Open_only | "closed" -> Ok Camlet_domain.Issue_list.Closed_only | _ -> Error (cli_error ~code:"issue.state_invalid" ~message:"The issue state must be all, open, or closed")
let run_issue_list repo target bobbin_text state_text timeout_text = let* () = validate_target_selection ~target_name:"REPOSITORY_DID" repo target in let* state_filter = issue_state_filter state_text in let* timeout = timeout_value timeout_text in let* repository_did = repository_target target in let* bobbin = bobbin_origin bobbin_text in let* client = Camlet_adapter_http.Http_client.create ~timeout_s:timeout in Camlet_application.List_issues.run ~ports:(issue_list_ports client) ~repo:(Option.value ~default:"." repo) ~repository_did ~bobbin ~state_filter
let issue_list json repo target bobbin state timeout_text = run_issue_list repo target bobbin state timeout_text |> emit_result ~json ~human:Camlet_presentation.Issue_list_output.human ~machine:Camlet_presentation.Issue_list_output.json
let issue_list_command = let doc = "list repository issues observed by Bobbin" in let info = command_info "list" ~doc in Cmd.v info Term.( const issue_list $ json $ issue_repo $ issue_target $ issue_bobbin $ issue_state $ issue_timeout)
let issue_view_target = Arg.( required & pos 0 (some string) None & info [] ~docv:"ISSUE_AT_URI" ~doc:"Read this canonical did:plc sh.tangled.repo.issue AT URI.")
let issue_uri value = let invalid () = Error (cli_error ~code:"issue.view_uri_invalid" ~message: "The issue locator must be a did:plc sh.tangled.repo.issue AT URI") in match Camlet_domain.At_uri.of_string value with | Error _ -> invalid () | Ok uri -> ( match ( Camlet_domain.At_uri.collection uri , Camlet_domain.At_uri.record_key uri , Camlet_domain.Did.of_string (Camlet_domain.At_uri.authority uri) ) with | Some "sh.tangled.repo.issue", Some _, Ok _ -> Ok uri | _ -> invalid ())
let issue_view_ports client = Camlet_application.Issue_view_ports. { resolve_did = Camlet_adapter_plc.Did_resolver.resolve client ; read_issue = Camlet_adapter_pds.Issue_reader.read_issue client ; view_repository = (fun repository_did -> Camlet_application.View_repository.run ~ports:(repo_view_ports client) ~repo:"." ~repository_did:(Some repository_did)) ; list_states = Camlet_adapter_bobbin.Issue_view_reader.list_states client ; list_comments = Camlet_adapter_bobbin.Issue_view_reader.list_comments client ; read_state = Camlet_adapter_pds.Issue_reader.read_state client ; read_comment = Camlet_adapter_pds.Issue_reader.read_comment client ; read_coverage = Camlet_adapter_bobbin.Issue_lister.coverage client ; now = current_timestamp }
let run_issue_view target bobbin_text timeout_text = let* issue_uri = issue_uri target in let* timeout = timeout_value timeout_text in let* bobbin = bobbin_origin bobbin_text in let* client = Camlet_adapter_http.Http_client.create ~timeout_s:timeout in Camlet_application.View_issue.run ~ports:(issue_view_ports client) ~issue_uri ~bobbin
let issue_view json target bobbin timeout_text = run_issue_view target bobbin timeout_text |> emit_result ~json ~human:Camlet_presentation.Issue_view_output.human ~machine:Camlet_presentation.Issue_view_output.json
let issue_view_command = let doc = "read and verify one issue and its indexed relations" in let info = command_info "view" ~doc in Cmd.v info Term.( const issue_view $ json $ issue_view_target $ issue_bobbin $ issue_timeout)
let issue_command = let doc = "inspect Tangled issue observations" in Cmd.group (command_info "issue" ~doc) [ issue_list_command; issue_view_command ]
let command = let doc = "correctness- and interoperability-oriented Tangled client" in let info = command_info "camlet" ~version:Camlet_domain.Version.current.version ~doc in Cmd.group info [ version_command; context_command; repo_command; issue_command ]
let () = exit (Cmd.eval' command)