diff --git a/CHANGELOG.md b/CHANGELOG.md index 302ec6a..5f4d079 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## UNRELEASED +## Changed +* `GITHUB_PATH` was renamed to `NIV_GITHUB_PATH` https://github.com/nmattia/niv/issues/280 +* `GITHUB_INSECURE` was renamed to `NIV_GITHUB_INSECURE` https://github.com/nmattia/niv/issues/280 + ## [0.2.17] 2020-09-08 ## Added * There is a new flag `-r/--rev` for specifying a revision during update and add diff --git a/README.md b/README.md index feb2489..5581bc5 100644 --- a/README.md +++ b/README.md @@ -69,12 +69,12 @@ The following environment variables are read by `niv`: | Name | Note | | --------------- | ---- | -| GITHUB_TOKEN | When set, the value is used to authenticate GitHub API requests. | -| GITHUB_HOST | The GitHub host to use when fetching packages. Port may be appended here. | -| GITHUB_API_HOST | The host used when performing GitHub API requests. Use `GITHUB_API_PORT` for specifying the port. | -| GITHUB_API_PORT | The port used when performing GitHub API requests. Defaults to `443` for secure requests. Defaults to `80` for insecure requests. See also: `GITHUB_INSECURE`. | -| GITHUB_INSECURE | When set to anything but the empty string, requests are performed over `http` instead of `https`. | -| GITHUB_PATH | The base path used when performing GitHub API requests. | +| GITHUB_TOKEN or NIV_GITHUB_TOKEN | When set, the value is used to authenticate GitHub API requests. | +| GITHUB_HOST or NIV_GITHUB_HOST | The GitHub host to use when fetching packages. Port may be appended here. | +| GITHUB_API_HOST or NIV_GITHUB_API_HOST | The host used when performing GitHub API requests. Use `GITHUB_API_PORT` for specifying the port. | +| GITHUB_API_PORT or NIV_GITHUB_API_PORT | The port used when performing GitHub API requests. Defaults to `443` for secure requests. Defaults to `80` for insecure requests. See also: `GITHUB_INSECURE`. | +| NIV_GITHUB_INSECURE | When set to anything but the empty string, requests are performed over `http` instead of `https`. | +| NIV_GITHUB_PATH | The base path used when performing GitHub API requests. | The next two sections cover [common use cases](#getting-started) and [full command description](#commands). diff --git a/README.tpl.md b/README.tpl.md index 8651764..636e8ef 100644 --- a/README.tpl.md +++ b/README.tpl.md @@ -69,12 +69,12 @@ The following environment variables are read by `niv`: | Name | Note | | --------------- | ---- | -| GITHUB_TOKEN | When set, the value is used to authenticate GitHub API requests. | -| GITHUB_HOST | The GitHub host to use when fetching packages. Port may be appended here. | -| GITHUB_API_HOST | The host used when performing GitHub API requests. Use `GITHUB_API_PORT` for specifying the port. | -| GITHUB_API_PORT | The port used when performing GitHub API requests. Defaults to `443` for secure requests. Defaults to `80` for insecure requests. See also: `GITHUB_INSECURE`. | -| GITHUB_INSECURE | When set to anything but the empty string, requests are performed over `http` instead of `https`. | -| GITHUB_PATH | The base path used when performing GitHub API requests. | +| GITHUB_TOKEN or NIV_GITHUB_TOKEN | When set, the value is used to authenticate GitHub API requests. | +| GITHUB_HOST or NIV_GITHUB_HOST | The GitHub host to use when fetching packages. Port may be appended here. | +| GITHUB_API_HOST or NIV_GITHUB_API_HOST | The host used when performing GitHub API requests. Use `GITHUB_API_PORT` for specifying the port. | +| GITHUB_API_PORT or NIV_GITHUB_API_PORT | The port used when performing GitHub API requests. Defaults to `443` for secure requests. Defaults to `80` for insecure requests. See also: `GITHUB_INSECURE`. | +| NIV_GITHUB_INSECURE | When set to anything but the empty string, requests are performed over `http` instead of `https`. | +| NIV_GITHUB_PATH | The base path used when performing GitHub API requests. | The next two sections cover [common use cases](#getting-started) and [full command description](#commands). diff --git a/src/Niv/Cli.hs b/src/Niv/Cli.hs index a0f508b..7718939 100644 --- a/src/Niv/Cli.hs +++ b/src/Niv/Cli.hs @@ -24,6 +24,7 @@ import Data.Text.Extended import Data.Version (showVersion) import Niv.Cmd import Niv.Git.Cmd +import Niv.GitHub.API (warnGitHubEnvVars) import Niv.GitHub.Cmd import Niv.Local.Cmd import Niv.Logger @@ -52,6 +53,7 @@ li = liftIO cli :: IO () cli = do + warnGitHubEnvVars (fsj, nio) <- execParserPure' Opts.defaultPrefs opts <$> getArgs >>= Opts.handleParseResult diff --git a/src/Niv/GitHub/API.hs b/src/Niv/GitHub/API.hs index 6d710fc..f56b95e 100644 --- a/src/Niv/GitHub/API.hs +++ b/src/Niv/GitHub/API.hs @@ -15,6 +15,7 @@ import qualified Data.Text as T import qualified Data.Text.Encoding as T import Data.Text.Extended import qualified Network.HTTP.Simple as HTTP +import Niv.Logger import System.Environment (lookupEnv) import System.Exit (exitFailure) import System.IO.Unsafe (unsafePerformIO) @@ -80,7 +81,7 @@ Make sure the repository exists. defaultRequest :: [T.Text] -> IO HTTP.Request defaultRequest (map T.encodeUtf8 -> parts) = do let path = T.encodeUtf8 githubPath <> BS8.intercalate "/" (parts) - mtoken <- lookupEnv "GITHUB_TOKEN" + mtoken <- lookupEnv' "GITHUB_TOKEN" pure $ ( flip (maybe id) mtoken $ \token -> HTTP.addRequestHeader "authorization" ("token " <> BS8.pack token) @@ -134,33 +135,66 @@ For more information on rate-limiting, see |] +-- Some environment variables may have different meanings (see for instance +-- https://github.com/nmattia/niv/issues/280) +-- For ambiguous ones, we prepend NIV_. +-- +warnGitHubEnvVars :: IO () +warnGitHubEnvVars = + mapM_ + warnEnvVar + [ "GITHUB_INSECURE", + "GITHUB_PATH" + ] + where + warnEnvVar vn = lookupEnv (T.unpack vn) >>= \case + Nothing -> pure () + Just {} -> do + twarn $ + T.unwords + [ "The environment variable", + vn, + "was renamed to", + "NIV_" <> vn + ] + +-- | Like lookupEnv "foo" but also looks up "NIV_foo" +lookupEnv' :: String -> IO (Maybe String) +lookupEnv' vn = lookupEnv vn >>= \case + Just x -> pure (Just x) + Nothing -> lookupEnv ("NIV_" <> vn) + githubHost :: T.Text githubHost = unsafePerformIO $ do - lookupEnv "GITHUB_HOST" >>= \case + lookupEnv' "GITHUB_HOST" >>= \case Just (T.pack -> x) -> pure x Nothing -> pure "github.com" githubApiPort :: Int githubApiPort = unsafePerformIO $ do - lookupEnv "GITHUB_API_PORT" >>= \case + lookupEnv' "GITHUB_API_PORT" >>= \case Just (readMaybe -> Just x) -> pure x _ -> pure $ if githubSecure then 443 else 80 githubApiHost :: T.Text githubApiHost = unsafePerformIO $ do - lookupEnv "GITHUB_API_HOST" >>= \case + lookupEnv' "GITHUB_API_HOST" >>= \case Just (T.pack -> x) -> pure x Nothing -> pure "api.github.com" +-- For these two we prepend NIV_ to the variable name because the variable +-- names can have different meanings, see +-- https://github.com/nmattia/niv/issues/280 + githubSecure :: Bool githubSecure = unsafePerformIO $ do - lookupEnv "GITHUB_INSECURE" >>= \case + lookupEnv "NIV_GITHUB_INSECURE" >>= \case Just "" -> pure True Just _ -> pure False Nothing -> pure True githubPath :: T.Text githubPath = unsafePerformIO $ do - lookupEnv "GITHUB_PATH" >>= \case + lookupEnv "NIV_GITHUB_PATH" >>= \case Just (T.pack -> x) -> pure $ fromMaybe x (T.stripSuffix "/" x) <> "/" Nothing -> pure "/" diff --git a/tests/github/default.nix b/tests/github/default.nix index 04ebf2f..bca9609 100644 --- a/tests/github/default.nix +++ b/tests/github/default.nix @@ -32,10 +32,10 @@ pkgs.runCommand "test" '' set -euo pipefail - export GITHUB_HOST="localhost:3333" - export GITHUB_API_HOST="localhost" - export GITHUB_API_PORT="3333" - export GITHUB_INSECURE="true" + export NIV_GITHUB_HOST="localhost:3333" + export NIV_GITHUB_API_HOST="localhost" + export NIV_GITHUB_API_PORT="3333" + export NIV_GITHUB_INSECURE="true" echo *** Starting the webserver... mkdir -p mock