packages feed

dotenv-0.12.0.0: app/Main.hs

{-# LANGUAGE CPP             #-}
{-# LANGUAGE RecordWildCards #-}

module Main where

import           Data.Version         (showVersion)
#if !MIN_VERSION_base(4,13,0)
import           Data.Monoid          ((<>))
#endif

import           Options.Applicative
import           Paths_dotenv         (version)

import           Configuration.Dotenv (Config (..), defaultConfig, loadFile)
import           System.Exit          (exitWith)
import           System.Process       (system)

data Options = Options
  { dotenvFiles        :: [String]
  , dotenvExampleFiles :: [String]
  , override           :: Bool
  , verbose            :: Bool
  , dryRun             :: Bool
  , duplicates         :: Bool
  , program            :: String
  , args               :: [String]
  } deriving (Show)

main :: IO ()
main = do
  Options{..} <- execParser opts
  let configDotenv =
        Config
          { configExamplePath = dotenvExampleFiles
          , configOverride = override
          , configVerbose = verbose
          , configDryRun = dryRun
          , allowDuplicates = duplicates
          , configPath =
              if null dotenvFiles
                then configPath defaultConfig
                else dotenvFiles
          }
   in do
    loadFile configDotenv
    if configDryRun configDotenv
      then putStrLn "[INFO]: Dry run mode enabled. Not executing the program."
      else system (program ++ concatMap (" " ++) args) >>= exitWith
       where
         opts = info (helper <*> versionOption <*> config)
           ( fullDesc
          <> progDesc "Runs PROGRAM after loading options from FILE"
          <> header "dotenv - loads options from dotenv files" )
         versionOption =
           infoOption
             (showVersion version)
             (long "version" <> short 'v' <> help "Show version of the program")

config :: Parser Options
config = Options
     <$> many (strOption (
                  long "dotenv"
                  <> short 'f'
                  <> metavar "DOTENV"
                  <> help "File to read for environmental variables" ))

     <*> many (strOption (
                  long "example"
                  <> short 'x'
                  <> metavar "DOTENV_EXAMPLE"
                  <> help "File to read for needed environmental variables" ))

     <*> switch ( long "overload"
                  <> short 'o'
                  <> help "Specify this flag to override existing variables" )

     <*> switch (  long "verbose"
                  <> help "Specify this flag to print out the variables loaded and other useful insights" )

     <*> switch (  long "dry-run"
                  <> help "Specify this flag to print out the variables loaded without executing the program" )

     <*> flag True False (  long "no-dups"
                  <> short 'D'
                  <> help "Specify this flag to forbid duplicate variables"
                  )

     <*> argument str (metavar "PROGRAM")

     <*> many (argument str (metavar "ARG"))