1
1
mirror of https://github.com/nmattia/niv.git synced 2024-09-18 19:07:19 +03:00

Prepend environment variables with NIV_

Here we make sure that some environment variables are prepended
(GITHUB_PATH, GITHUB_INSECURE). For consistency's sake, other
environment variables (GITHUB_TOKEN, etc) _can_ also be prepended with
NIV_. This also issues a warning if `GITHUB_PATH` or `GITHUB_INSECURE`
is set.
This commit is contained in:
Nicolas Mattia 2020-09-17 12:49:52 +02:00
parent dd13098d01
commit 48dee993d2
6 changed files with 63 additions and 22 deletions

View File

@ -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

View File

@ -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).

View File

@ -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).

View File

@ -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

View File

@ -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 "/"

View File

@ -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