Browse Source

Add support for log level (closes #33) (#93)

remotes/origin/add-simple-redirecting-site
Tomer Ohana 6 years ago committed by Gal Schlezinger
parent
commit
6b82b9ce23
  1. 2
      README.md
  2. 4
      executable/Alias.re
  3. 12
      executable/Env.re
  4. 28
      executable/FnmApp.re
  5. 14
      executable/Install.re
  6. 2
      executable/Use.re
  7. 11
      feature_tests/log-level-error/run.sh
  8. 13
      feature_tests/log-level-quiet/run.sh
  9. 11
      library/Config.re
  10. 19
      library/LogLevel.re
  11. 17
      library/Logger.re
  12. 2
      library/dune
  13. 3
      package.json
  14. 1
      test/__snapshots__/Smoke_test.4d362c3c.0.snapshot

2
README.md

@ -80,7 +80,7 @@ Lists the installed Node versions.
Lists the Node versions available to download remotely. Lists the Node versions available to download remotely.
### `fnm env [--multi] [--shell=fish|bash|zsh] [--node-dist-mirror=URI] [--use-on-cd] [--base-dir=DIR]` ### `fnm env [--multi] [--shell=fish|bash|zsh] [--node-dist-mirror=URI] [--use-on-cd] [--base-dir=DIR] [--log-level=quiet|error|all]`
Prints the required shell commands in order to configure your shell, Bash compliant by default. Prints the required shell commands in order to configure your shell, Bash compliant by default.

4
executable/Alias.re

@ -10,7 +10,7 @@ let run = (~name, ~version) => {
let%lwt versionInstalled = Lwt_unix.file_exists(versionPath); let%lwt versionInstalled = Lwt_unix.file_exists(versionPath);
if (!versionInstalled) { if (!versionInstalled) {
Console.error( Logger.error(
<Pastel color=Pastel.Red> <Pastel color=Pastel.Red>
"Can't find a version installed in " "Can't find a version installed in "
versionPath versionPath
@ -19,7 +19,7 @@ let run = (~name, ~version) => {
exit(1); exit(1);
}; };
Console.log( Logger.log(
<Pastel> <Pastel>
"Aliasing " "Aliasing "
<Pastel color=Pastel.Cyan> name </Pastel> <Pastel color=Pastel.Cyan> name </Pastel>

12
executable/Env.re

@ -70,7 +70,8 @@ let rec printUseOnCd = (~shell) =>
|} |}
}; };
let run = (~forceShell, ~multishell, ~nodeDistMirror, ~fnmDir, ~useOnCd) => { let run =
(~forceShell, ~multishell, ~nodeDistMirror, ~fnmDir, ~useOnCd, ~logLevel) => {
open Lwt; open Lwt;
open System.Shell; open System.Shell;
@ -103,11 +104,18 @@ let run = (~forceShell, ~multishell, ~nodeDistMirror, ~fnmDir, ~useOnCd) => {
nodeDistMirror, nodeDistMirror,
) )
|> Console.log; |> Console.log;
Printf.sprintf(
"export %s=%s",
Config.FNM_LOGLEVEL.name,
Fnm.LogLevel.toString(logLevel),
)
|> Console.log;
| Fish => | Fish =>
Printf.sprintf("set -gx PATH %s/bin $PATH;", path) |> Console.log; Printf.sprintf("set -gx PATH %s/bin $PATH;", path) |> Console.log;
Printf.sprintf("set -gx %s %s;", Config.FNM_MULTISHELL_PATH.name, path) Printf.sprintf("set -gx %s %s;", Config.FNM_MULTISHELL_PATH.name, path)
|> Console.log; |> Console.log;
Printf.sprintf("set -gx %s %s;", Config.FNM_DIR.name, fnmDir) |> Console.log; Printf.sprintf("set -gx %s %s;", Config.FNM_DIR.name, fnmDir)
|> Console.log;
Printf.sprintf( Printf.sprintf(
"set -gx %s %s", "set -gx %s %s",
Config.FNM_NODE_DIST_MIRROR.name, Config.FNM_NODE_DIST_MIRROR.name,

28
executable/FnmApp.re

@ -7,7 +7,15 @@ module Commands = {
let listLocal = () => Lwt_main.run(ListLocal.run()); let listLocal = () => Lwt_main.run(ListLocal.run());
let install = version => Lwt_main.run(Install.run(~version)); let install = version => Lwt_main.run(Install.run(~version));
let env = let env =
(isFishShell, isMultishell, nodeDistMirror, fnmDir, shell, useOnCd) => (
isFishShell,
isMultishell,
nodeDistMirror,
fnmDir,
shell,
useOnCd,
logLevel,
) =>
Lwt_main.run( Lwt_main.run(
Env.run( Env.run(
~forceShell=Fnm.System.Shell.(isFishShell ? Some(Fish) : shell), ~forceShell=Fnm.System.Shell.(isFishShell ? Some(Fish) : shell),
@ -15,6 +23,7 @@ module Commands = {
~nodeDistMirror, ~nodeDistMirror,
~fnmDir, ~fnmDir,
~useOnCd, ~useOnCd,
~logLevel,
), ),
); );
}; };
@ -187,6 +196,22 @@ let env = {
Arg.(value & flag & info(["use-on-cd"], ~doc)); Arg.(value & flag & info(["use-on-cd"], ~doc));
}; };
let logLevel = {
let doc = "The log level of fnm commands, can be 'quiet', 'error' or 'all'";
Arg.(
value
& opt(
enum([
("quiet", Fnm.LogLevel.Quiet),
("error", Fnm.LogLevel.Error),
("all", Fnm.LogLevel.All),
]),
Fnm.Config.FNM_LOGLEVEL.get(),
)
& info(["log-level"], ~doc)
);
};
( (
Term.( Term.(
const(Commands.env) const(Commands.env)
@ -196,6 +221,7 @@ let env = {
$ fnmDir $ fnmDir
$ shell $ shell
$ useOnCd $ useOnCd
$ logLevel
), ),
Term.info("env", ~version, ~doc, ~exits=Term.default_exits, ~man, ~sdocs), Term.info("env", ~version, ~doc, ~exits=Term.default_exits, ~man, ~sdocs),
); );

14
executable/Install.re

@ -3,7 +3,7 @@ open Fnm;
let mkDownloadsDir = () => { let mkDownloadsDir = () => {
let exists = Lwt_unix.file_exists(Directories.downloads); let exists = Lwt_unix.file_exists(Directories.downloads);
if%lwt (exists |> Lwt.map(x => !x)) { if%lwt (exists |> Lwt.map(x => !x)) {
Console.log( Logger.log(
<Pastel> <Pastel>
"Creating " "Creating "
<Pastel color=Pastel.Cyan> Directories.downloads </Pastel> <Pastel color=Pastel.Cyan> Directories.downloads </Pastel>
@ -29,7 +29,7 @@ let main = (~version as versionName) => {
let versionName = Versions.format(versionName); let versionName = Versions.format(versionName);
let%lwt _ = Versions.throwIfInstalled(versionName); let%lwt _ = Versions.throwIfInstalled(versionName);
Console.log( Logger.log(
<Pastel> <Pastel>
"Looking for node " "Looking for node "
<Pastel color=Pastel.Cyan> versionName </Pastel> <Pastel color=Pastel.Cyan> versionName </Pastel>
@ -53,7 +53,7 @@ let main = (~version as versionName) => {
versionName ++ Versions.Remote.downloadFileSuffix, versionName ++ Versions.Remote.downloadFileSuffix,
); );
Console.log( Logger.log(
<Pastel> <Pastel>
"Downloading " "Downloading "
<Pastel color=Pastel.Cyan> filepath </Pastel> <Pastel color=Pastel.Cyan> filepath </Pastel>
@ -67,7 +67,7 @@ let main = (~version as versionName) => {
let extractionDestination = let extractionDestination =
Filename.concat(Directories.nodeVersions, versionName); Filename.concat(Directories.nodeVersions, versionName);
Console.log( Logger.log(
<Pastel> <Pastel>
"Extracting " "Extracting "
<Pastel color=Pastel.Cyan> tarDestination </Pastel> <Pastel color=Pastel.Cyan> tarDestination </Pastel>
@ -85,7 +85,7 @@ let main = (~version as versionName) => {
let run = (~version) => let run = (~version) =>
try%lwt (main(~version)) { try%lwt (main(~version)) {
| Versions.No_Download_For_System(os, arch) => | Versions.No_Download_For_System(os, arch) =>
Console.log( Logger.log(
<Pastel> <Pastel>
"Version exists, but can't find a file for your system:\n" "Version exists, but can't find a file for your system:\n"
" OS: " " OS: "
@ -97,7 +97,7 @@ let run = (~version) =>
); );
exit(1); exit(1);
| Versions.Already_installed(version) => | Versions.Already_installed(version) =>
Console.log( Logger.log(
<Pastel> <Pastel>
"Version " "Version "
<Pastel color=Pastel.Cyan> version </Pastel> <Pastel color=Pastel.Cyan> version </Pastel>
@ -106,7 +106,7 @@ let run = (~version) =>
) )
|> Lwt.return |> Lwt.return
| Versions.Version_not_found(version) => | Versions.Version_not_found(version) =>
Console.log( Logger.log(
<Pastel> <Pastel>
"Version " "Version "
<Pastel color=Pastel.Cyan> version </Pastel> <Pastel color=Pastel.Cyan> version </Pastel>

2
executable/Use.re

@ -6,7 +6,7 @@ exception Version_Not_Installed(string);
let log = (~quiet, arg) => let log = (~quiet, arg) =>
if (!quiet) { if (!quiet) {
Console.log(arg); Logger.log(arg);
}; };
let switchVersion = (~version, ~quiet) => { let switchVersion = (~version, ~quiet) => {

11
feature_tests/log-level-error/run.sh

@ -0,0 +1,11 @@
#!/bin/bash
set -e
eval $(fnm env --log-level=error)
ALIAS="$(fnm install 8.11.3 && (fnm alias 123 abc 2>&1 || true))"
if [ "$ALIAS" == "" ]; then
echo "Expected the output to contain errors"
exit 1
fi

13
feature_tests/log-level-quiet/run.sh

@ -0,0 +1,13 @@
#!/bin/bash
set -e
eval $(fnm env --log-level=quiet)
INSTALL="$(fnm install v8.11.3 && fnm use v8.11.3 && fnm alias v8.11.3 something)"
OUTPUT="$INSTALL"
if [ "$OUTPUT" != "" ]; then
echo "Expected the output to be empty, instead got:"
echo $OUTPUT
exit 1
fi

11
library/Config.re

@ -63,6 +63,16 @@ module FNM_MULTISHELL_PATH =
let default = ""; let default = "";
}); });
module FNM_LOGLEVEL =
EnvVar({
type t = LogLevel.t;
let parse = LogLevel.fromString;
let unparse = LogLevel.toString;
let name = "FNM_LOGLEVEL";
let doc = "The log level of fnm commands";
let default = LogLevel.All;
});
let parseBooleanOrDie = (~name, str) => let parseBooleanOrDie = (~name, str) =>
switch (bool_of_string_opt(str)) { switch (bool_of_string_opt(str)) {
| Some(boolean) => boolean | Some(boolean) => boolean
@ -97,4 +107,5 @@ let getDocs = () => [
FNM_NODE_DIST_MIRROR.docInfo, FNM_NODE_DIST_MIRROR.docInfo,
FNM_MULTISHELL_PATH.docInfo, FNM_MULTISHELL_PATH.docInfo,
FNM_INTERACTIVE_CLI.docInfo, FNM_INTERACTIVE_CLI.docInfo,
FNM_LOGLEVEL.docInfo,
]; ];

19
library/LogLevel.re

@ -0,0 +1,19 @@
type t =
| Quiet
| Error
| All;
let toString = logLevel =>
switch (logLevel) {
| Quiet => "quiet"
| Error => "error"
| All => "all"
};
let fromString = logLevelString =>
switch (logLevelString) {
| "quiet" => Quiet
| "error" => Error
| "all" => All
| _ => failwith("Unsupported level: " ++ logLevelString)
};

17
library/Logger.re

@ -0,0 +1,17 @@
let configuredLogLevel = Config.FNM_LOGLEVEL.get();
let log = message => {
switch (configuredLogLevel) {
| LogLevel.All => Console.log(message)
| LogLevel.Error
| LogLevel.Quiet => ()
};
};
let error = message => {
switch (configuredLogLevel) {
| LogLevel.All
| LogLevel.Error => Console.error(message)
| LogLevel.Quiet => ()
};
};

2
library/dune

@ -7,6 +7,6 @@
(name Fnm) (name Fnm)
; Other libraries list this name in their package.json 'require' field to use this library. ; Other libraries list this name in their package.json 'require' field to use this library.
(public_name fnm.lib) (public_name fnm.lib)
(libraries pastel.lib 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 console.lib )
(preprocess ( pps lwt_ppx ppx_let )) ; From package.json preprocess field (preprocess ( pps lwt_ppx ppx_let )) ; From package.json preprocess field
) )

3
package.json

@ -41,7 +41,8 @@
"semver", "semver",
"cohttp", "cohttp",
"cohttp-lwt", "cohttp-lwt",
"cohttp-lwt-unix" "cohttp-lwt-unix",
"console.lib"
], ],
"name": "fnm.lib", "name": "fnm.lib",
"namespace": "Fnm" "namespace": "Fnm"

1
test/__snapshots__/Smoke_test.4d362c3c.0.snapshot

@ -3,4 +3,5 @@ export PATH=<sfwRoot>/current/bin:$PATH
export FNM_MULTISHELL_PATH=<sfwRoot>/current export FNM_MULTISHELL_PATH=<sfwRoot>/current
export FNM_DIR=<sfwRoot>/ export FNM_DIR=<sfwRoot>/
export FNM_NODE_DIST_MIRROR=https://nodejs.org/dist export FNM_NODE_DIST_MIRROR=https://nodejs.org/dist
export FNM_LOGLEVEL=all

Loading…
Cancel
Save