Skip to content

Commit bc69958

Browse files
committed
fix: command options learn-ocaml-client
1 parent 671ccf5 commit bc69958

File tree

1 file changed

+105
-12
lines changed

1 file changed

+105
-12
lines changed

src/main/learnocaml_client.ml

Lines changed: 105 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ open Learnocaml_data
1010
open Lwt.Infix
1111
module Api = Learnocaml_api
1212

13+
module ES = Exercise.Status
14+
1315
open Cmdliner
1416
open Arg
1517

@@ -37,6 +39,50 @@ let url_conv =
3739
(fun fmt t -> Format.pp_print_string fmt (Token.to_string t))
3840
)
3941

42+
module Args_server = struct
43+
type t = {
44+
server_url: Uri.t option;
45+
local: bool;
46+
}
47+
48+
let server_url =
49+
value & opt (some url_conv) None &
50+
info ["s";"server"] ~docv:"URL" ~doc:
51+
"The URL of the learn-ocaml server."
52+
~env:(Term.env_info "LEARNOCAML_SERVER" ~doc:
53+
"Sets the learn-ocaml server URL. Overridden by $(b,--server).")
54+
let local =
55+
value & flag & info ["local"] ~doc:
56+
"Use a configuration file local to the current directory, rather \
57+
than user-wide."
58+
59+
let apply server_url local =
60+
{server_url; local}
61+
62+
let term =
63+
Term.(const apply $server_url $local)
64+
65+
let term_server =
66+
Term.(const (fun x -> x) $ server_url)
67+
end
68+
69+
module Args_logout = struct
70+
type t = {
71+
local: bool;
72+
}
73+
74+
let local =
75+
value & flag & info ["local"] ~doc:
76+
"Use a configuration file local to the current directory, rather \
77+
than user-wide."
78+
79+
let apply local =
80+
{local}
81+
82+
let term =
83+
Term.(const apply $local)
84+
end
85+
4086
module Args_global = struct
4187
type t = {
4288
server_url: Uri.t option;
@@ -93,6 +139,8 @@ module Args_create_token = struct
93139
let term = Term.(const apply $ nickname $ secret)
94140
end
95141

142+
143+
96144
module Args_create_user = struct
97145
type t = {
98146
email : string;
@@ -675,6 +723,37 @@ let get_config_o ?save_back ?(allow_static=false) o =
675723
let open Args_global in
676724
get_config ~local:o.local ?save_back ~allow_static o.server_url o.token
677725

726+
let get_config_option_server ?local ?(save_back=false) ?(allow_static=false) server_opt =
727+
match ConfigFile.path ?local () with
728+
| Some f ->
729+
ConfigFile.read f >>= fun c ->
730+
let c = match server_opt with
731+
| Some server -> { c with ConfigFile.server }
732+
| None -> c
733+
in
734+
check_server_version ~allow_static c.ConfigFile.server
735+
>>= fun _ ->
736+
(
737+
if save_back
738+
then
739+
ConfigFile.write f c >|= fun () ->
740+
Printf.eprintf "Configuration written to %s\n%!" f
741+
else
742+
Lwt.return_unit
743+
)
744+
>|= fun () -> Some c
745+
| None -> Lwt.return_none
746+
747+
let get_config_server ?local ?(save_back=false) ?(allow_static=false) server_opt =
748+
get_config_option_server ?local ~save_back ~allow_static server_opt
749+
>>= function
750+
| Some c -> Lwt.return c
751+
| None -> Lwt.fail_with "No config file found. Please do `learn-ocaml-client init`"
752+
753+
let get_config_o_server ?save_back ?(allow_static=false) o =
754+
let open Args_server in
755+
get_config_server ~local:o.local ?save_back ~allow_static o.server_url
756+
678757
module Init = struct
679758
open Args_global
680759
open Args_create_token
@@ -719,12 +798,12 @@ module Init = struct
719798
end
720799

721800
module Init_server = struct
722-
open Args_global
801+
open Args_server
723802

724-
let init_server global_args =
725-
let path = if global_args.local then ConfigFile.local_path else ConfigFile.user_path in
803+
let init_server server_args =
804+
let path = if server_args.local then ConfigFile.local_path else ConfigFile.user_path in
726805
let get_server () =
727-
match global_args.server_url with
806+
match server_args.server_url with
728807
| None -> Lwt.fail_with "You must provide a server."
729808
| Some s -> Lwt.return s
730809
in
@@ -739,17 +818,17 @@ module Init_server = struct
739818
let cmd =
740819
Term.(
741820
const (fun go -> Pervasives.exit (Lwt_main.run (init_server go)))
742-
$ Args_global.term),
821+
$ Args_server.term),
743822
Term.info ~man
744823
~doc:"Initialize the configuration file."
745824
"init-server"
746825
end
747826

748827
module Logout = struct
749-
open Args_global
828+
open Args_logout
750829

751-
let logout global_args =
752-
let path = if global_args.local then ConfigFile.local_path else ConfigFile.user_path in
830+
let logout logout_args =
831+
let path = if logout_args.local then ConfigFile.local_path else ConfigFile.user_path in
753832
let get_server () = Lwt.return Uri.empty
754833
in
755834
get_server () >>= fun server ->
@@ -763,7 +842,7 @@ module Logout = struct
763842
let cmd =
764843
Term.(
765844
const (fun go -> Pervasives.exit (Lwt_main.run (logout go)))
766-
$ Args_global.term),
845+
$ Args_logout.term),
767846
Term.info ~man
768847
~doc:"delete current configuration file."
769848
"logout"
@@ -1141,9 +1220,11 @@ module Exercise_list = struct
11411220
end
11421221

11431222
module Server_config = struct
1223+
open Args_server
1224+
11441225
let doc = "Get a structured json containing an information about the use_password compatibility"
11451226

1146-
let server_config o = get_config_o ~allow_static:true o
1227+
let server_config o = get_config_o_server ~allow_static:true o
11471228
>>= fun {ConfigFile.server;_} ->
11481229
fetch server (Learnocaml_api.Server_config ())
11491230
>>= (fun isPassword->
@@ -1162,13 +1243,25 @@ module Server_config = struct
11621243
let man = man doc
11631244

11641245
let cmd =
1165-
use_global server_config,
1166-
Term.info ~man ~doc:doc "server-config"
1246+
Term.(
1247+
const (fun go -> Pervasives.exit (Lwt_main.run (server_config go)))
1248+
$ Args_server.term),
1249+
Term.info ~man
1250+
~doc:doc
1251+
"server-config"
11671252
end
11681253

11691254
module Exercise_score = struct
11701255
let doc = "Get informations about scores of exercises"
11711256

1257+
let status_map = ref SMap.empty
1258+
1259+
let open_exercises =
1260+
SMap.fold (fun ex st acc ->
1261+
if ES.(st.assignments.default = Open) then ex::acc else acc)
1262+
!status_map []
1263+
|> List.rev
1264+
11721265
let exercise_score _ = Lwt.return 0
11731266

11741267
let man = man doc

0 commit comments

Comments
 (0)