···11+(** snapshots command: list snapshots for a profile *)
22+33+open Cmdliner
44+55+let run profile_name profile_dir =
66+ match Common.load_profile ~profile_dir ~name:profile_name with
77+ | Error (`Msg e) -> Printf.eprintf "Error: %s\n%!" e; 1
88+ | Ok (_profile, paths) ->
99+ let snaps = Common.snapshot_dirs_by_recency paths in
1010+ if snaps = [] then begin
1111+ Printf.printf "No snapshots for profile '%s'\n%!" profile_name;
1212+ 0
1313+ end else begin
1414+ Printf.printf "Snapshots for profile '%s' (%d):\n\n" profile_name
1515+ (List.length snaps);
1616+ List.iter (fun snap_dir ->
1717+ let key = Fpath.basename snap_dir in
1818+ let created = match Day11_batch.Snapshot.load snap_dir with
1919+ | Ok s -> s.created
2020+ | Error _ -> "?"
2121+ in
2222+ let n_solutions =
2323+ let sol_dir = Day11_batch.Snapshot.solutions_dir snap_dir in
2424+ match Bos.OS.Dir.contents sol_dir with
2525+ | Ok files ->
2626+ List.length (List.filter (fun p ->
2727+ Fpath.has_ext ".json" p && Fpath.basename p <> "repos.json"
2828+ ) files)
2929+ | Error _ -> 0
3030+ in
3131+ let n_runs =
3232+ let runs_dir = Day11_batch.Snapshot.runs_dir snap_dir in
3333+ match Bos.OS.Dir.contents runs_dir with
3434+ | Ok dirs -> List.length dirs
3535+ | Error _ -> 0
3636+ in
3737+ let repos = match Day11_batch.Snapshot.load snap_dir with
3838+ | Ok s ->
3939+ String.concat ", " (List.map (fun (_, sha) ->
4040+ String.sub sha 0 (min 12 (String.length sha))
4141+ ) s.repos)
4242+ | Error _ -> "?"
4343+ in
4444+ Printf.printf " %s %s %d solutions, %d runs [%s]\n"
4545+ key created n_solutions n_runs repos
4646+ ) snaps;
4747+ 0
4848+ end
4949+5050+let cmd =
5151+ let doc = "List snapshots for a profile" in
5252+ let info = Cmd.info "snapshots" ~doc in
5353+ Cmd.v info
5454+ Term.(const run $ Common.profile_term $ Common.profile_dir_term)