Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Lists the installed Node versions.

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.

Expand Down
4 changes: 2 additions & 2 deletions executable/Alias.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let run = (~name, ~version) => {
let%lwt versionInstalled = Lwt_unix.file_exists(versionPath);

if (!versionInstalled) {
Console.error(
Logger.error(
<Pastel color=Pastel.Red>
"Can't find a version installed in "
versionPath
Expand All @@ -19,7 +19,7 @@ let run = (~name, ~version) => {
exit(1);
};

Console.log(
Logger.log(
<Pastel>
"Aliasing "
<Pastel color=Pastel.Cyan> name </Pastel>
Expand Down
12 changes: 10 additions & 2 deletions executable/Env.re
Original file line number Diff line number Diff line change
Expand Up @@ -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 System.Shell;

Expand Down Expand Up @@ -103,11 +104,18 @@ let run = (~forceShell, ~multishell, ~nodeDistMirror, ~fnmDir, ~useOnCd) => {
nodeDistMirror,
)
|> Console.log;
Printf.sprintf(
"export %s=%s",
Config.FNM_LOGLEVEL.name,
Fnm.LogLevel.toString(logLevel),
)
|> Console.log;
| Fish =>
Printf.sprintf("set -gx PATH %s/bin $PATH;", path) |> Console.log;
Printf.sprintf("set -gx %s %s;", Config.FNM_MULTISHELL_PATH.name, path)
|> 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(
"set -gx %s %s",
Config.FNM_NODE_DIST_MIRROR.name,
Expand Down
28 changes: 27 additions & 1 deletion executable/FnmApp.re
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ module Commands = {
let listLocal = () => Lwt_main.run(ListLocal.run());
let install = version => Lwt_main.run(Install.run(~version));
let env =
(isFishShell, isMultishell, nodeDistMirror, fnmDir, shell, useOnCd) =>
(
isFishShell,
isMultishell,
nodeDistMirror,
fnmDir,
shell,
useOnCd,
logLevel,
) =>
Lwt_main.run(
Env.run(
~forceShell=Fnm.System.Shell.(isFishShell ? Some(Fish) : shell),
~multishell=isMultishell,
~nodeDistMirror,
~fnmDir,
~useOnCd,
~logLevel,
),
);
};
Expand Down Expand Up @@ -187,6 +196,22 @@ let env = {
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.(
const(Commands.env)
Expand All @@ -196,6 +221,7 @@ let env = {
$ fnmDir
$ shell
$ useOnCd
$ logLevel
),
Term.info("env", ~version, ~doc, ~exits=Term.default_exits, ~man, ~sdocs),
);
Expand Down
14 changes: 7 additions & 7 deletions executable/Install.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ open Fnm;
let mkDownloadsDir = () => {
let exists = Lwt_unix.file_exists(Directories.downloads);
if%lwt (exists |> Lwt.map(x => !x)) {
Console.log(
Logger.log(
<Pastel>
"Creating "
<Pastel color=Pastel.Cyan> Directories.downloads </Pastel>
Expand All @@ -29,7 +29,7 @@ let main = (~version as versionName) => {
let versionName = Versions.format(versionName);
let%lwt _ = Versions.throwIfInstalled(versionName);

Console.log(
Logger.log(
<Pastel>
"Looking for node "
<Pastel color=Pastel.Cyan> versionName </Pastel>
Expand All @@ -53,7 +53,7 @@ let main = (~version as versionName) => {
versionName ++ Versions.Remote.downloadFileSuffix,
);

Console.log(
Logger.log(
<Pastel>
"Downloading "
<Pastel color=Pastel.Cyan> filepath </Pastel>
Expand All @@ -67,7 +67,7 @@ let main = (~version as versionName) => {
let extractionDestination =
Filename.concat(Directories.nodeVersions, versionName);

Console.log(
Logger.log(
<Pastel>
"Extracting "
<Pastel color=Pastel.Cyan> tarDestination </Pastel>
Expand All @@ -85,7 +85,7 @@ let main = (~version as versionName) => {
let run = (~version) =>
try%lwt (main(~version)) {
| Versions.No_Download_For_System(os, arch) =>
Console.log(
Logger.log(
<Pastel>
"Version exists, but can't find a file for your system:\n"
" OS: "
Expand All @@ -97,7 +97,7 @@ let run = (~version) =>
);
exit(1);
| Versions.Already_installed(version) =>
Console.log(
Logger.log(
<Pastel>
"Version "
<Pastel color=Pastel.Cyan> version </Pastel>
Expand All @@ -106,7 +106,7 @@ let run = (~version) =>
)
|> Lwt.return
| Versions.Version_not_found(version) =>
Console.log(
Logger.log(
<Pastel>
"Version "
<Pastel color=Pastel.Cyan> version </Pastel>
Expand Down
2 changes: 1 addition & 1 deletion executable/Use.re
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exception Version_Not_Installed(string);

let log = (~quiet, arg) =>
if (!quiet) {
Console.log(arg);
Logger.log(arg);
};

let switchVersion = (~version, ~quiet) => {
Expand Down
11 changes: 11 additions & 0 deletions feature_tests/log-level-error/run.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions feature_tests/log-level-quiet/run.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions library/Config.re
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ module FNM_MULTISHELL_PATH =
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) =>
switch (bool_of_string_opt(str)) {
| Some(boolean) => boolean
Expand Down Expand Up @@ -97,4 +107,5 @@ let getDocs = () => [
FNM_NODE_DIST_MIRROR.docInfo,
FNM_MULTISHELL_PATH.docInfo,
FNM_INTERACTIVE_CLI.docInfo,
FNM_LOGLEVEL.docInfo,
];
19 changes: 19 additions & 0 deletions library/LogLevel.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type t =
Copy link
Owner

@Schniz Schniz Apr 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider moving this file to the Logger module

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's where it was initially, but I had a cyclic dependency Config -> Logger -> Config because Config needs the log level and Logger needs the config value in runtime

| 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 changes: 17 additions & 0 deletions library/Logger.re
Original file line number Diff line number Diff line change
@@ -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 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 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
)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"semver",
"cohttp",
"cohttp-lwt",
"cohttp-lwt-unix"
"cohttp-lwt-unix",
"console.lib"
],
"name": "fnm.lib",
"namespace": "Fnm"
Expand Down
1 change: 1 addition & 0 deletions test/__snapshots__/Smoke_test.4d362c3c.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export PATH=<sfwRoot>/current/bin:$PATH
export FNM_MULTISHELL_PATH=<sfwRoot>/current
export FNM_DIR=<sfwRoot>/
export FNM_NODE_DIST_MIRROR=https://nodejs.org/dist
export FNM_LOGLEVEL=all