Skip to content

Commit

Permalink
Add support for interactive installation for use (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
Schniz authored Mar 17, 2019
1 parent 413b748 commit 2ce8517
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
32 changes: 28 additions & 4 deletions executable/Use.re
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,41 @@ let main = (~version as providedVersion, ~quiet) => {
switchVersion(~version, ~quiet);
};

let run = (~version, ~quiet) =>
let rec askIfInstall = (~version, ~quiet, retry) => {
let%lwt _ =
Lwt_io.write(
Lwt_io.stderr,
<Pastel color=Pastel.Red> "Do you want to install it? [y/n] " </Pastel>,
);
switch%lwt (Lwt_io.read_line(Lwt_io.stdin)) {
| "Y"
| "y" =>
let%lwt _ = Install.run(~version);
retry(~version, ~quiet);
| "N"
| "n" => Lwt_io.write_line(Lwt_io.stderr, "not installing!")
| _ =>
let%lwt _ =
Lwt_io.write_line(Lwt_io.stderr, "Invalid response. Please try again:");
askIfInstall(~version, ~quiet, retry);
};
};

let rec run = (~version, ~quiet) =>
try%lwt (main(~version, ~quiet)) {
| Version_Not_Installed(version) =>
| Version_Not_Installed(versionString) =>
log(
~quiet,
<Pastel color=Pastel.Red>
"The following version is not installed: "
version
versionString
</Pastel>,
);
exit(1);
if (Fnm.Config.FNM_INTERACTIVE_CLI.get()) {
askIfInstall(~version, ~quiet, run);
} else {
exit(1);
};
| Dotfiles.Conflicting_Dotfiles_Found(v1, v2) =>
log(
~quiet,
Expand Down
39 changes: 37 additions & 2 deletions library/Config.re
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module EnvVar =
},
) => {
include M;
let getOpt = () => Sys.getenv_opt(name) |> Opt.map(parse);
let optValue = Sys.getenv_opt(name) |> Opt.map(parse);
let getOpt = () => optValue;
let get = () => Opt.(getOpt() or default);
let docInfo = {name, doc, default: unparse(default)};
};
Expand Down Expand Up @@ -62,4 +63,38 @@ module FNM_MULTISHELL_PATH =
let default = "";
});

let getDocs = () => [FNM_DIR.docInfo, FNM_NODE_DIST_MIRROR.docInfo];
let parseBooleanOrDie = (~name, str) =>
switch (bool_of_string_opt(str)) {
| Some(boolean) => boolean
| None =>
let errorString =
<Pastel color=Pastel.Red>
<Pastel color=Pastel.Cyan> str </Pastel>
" isn't a valid option for "
<Pastel color=Pastel.Magenta> name </Pastel>
" which assumes a boolean value. Consider passing "
<Pastel color=Pastel.Cyan> "true" </Pastel>
" or "
<Pastel color=Pastel.Cyan> "false" </Pastel>
"."
</Pastel>;
Printf.eprintf("%s\n", errorString);
exit(1);
};

module FNM_INTERACTIVE_CLI =
EnvVar({
type t = bool;
let default = Unix.(isatty(stdin) && isatty(stdout) && isatty(stderr));
let unparse = string_of_bool;
let name = "FNM_INTERACTIVE_CLI";
let doc = "Should the CLI be interactive? true/false";
let parse = parseBooleanOrDie(~name);
});

let getDocs = () => [
FNM_DIR.docInfo,
FNM_NODE_DIST_MIRROR.docInfo,
FNM_MULTISHELL_PATH.docInfo,
FNM_INTERACTIVE_CLI.docInfo,
];
2 changes: 1 addition & 1 deletion library/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
(name Fnm)
; Other libraries list this name in their package.json 'require' field to use this library.
(public_name fnm.lib)
(libraries str core lwt ssl lwt_ssl lambdasoup semver cohttp cohttp-lwt cohttp-lwt-unix )
(libraries pastel.lib str core lwt ssl lwt_ssl lambdasoup semver cohttp cohttp-lwt cohttp-lwt-unix )
(preprocess ( pps lwt_ppx ppx_let )) ; From package.json preprocess field
)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"ppx_let"
],
"require": [
"pastel.lib",
"str",
"core",
"lwt",
Expand Down

0 comments on commit 2ce8517

Please sign in to comment.