2021-10-15 20:24:23 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
|
|
|
{-# LANGUAGE RankNTypes #-}
|
|
|
|
|
|
|
|
module GHCup.OptParse.Config where
|
|
|
|
|
|
|
|
|
|
|
|
import GHCup.Errors
|
|
|
|
import GHCup.Types
|
|
|
|
import GHCup.Utils
|
2022-05-21 20:54:18 +00:00
|
|
|
import GHCup.Prelude
|
|
|
|
import GHCup.Prelude.Logger
|
|
|
|
import GHCup.Prelude.String.QQ
|
2022-03-10 19:26:51 +00:00
|
|
|
import GHCup.OptParse.Common
|
2021-10-15 20:24:23 +00:00
|
|
|
|
|
|
|
#if !MIN_VERSION_base(4,13,0)
|
|
|
|
import Control.Monad.Fail ( MonadFail )
|
|
|
|
#endif
|
2021-10-21 21:17:26 +00:00
|
|
|
import Control.Exception ( displayException )
|
2021-10-15 20:24:23 +00:00
|
|
|
import Control.Monad.Reader
|
|
|
|
import Control.Monad.Trans.Resource
|
|
|
|
import Data.Functor
|
|
|
|
import Data.Maybe
|
|
|
|
import Haskus.Utils.Variant.Excepts
|
2022-03-10 19:26:51 +00:00
|
|
|
import Options.Applicative hiding ( style, ParseError )
|
2021-10-19 18:38:53 +00:00
|
|
|
import Options.Applicative.Help.Pretty ( text )
|
2021-10-15 20:24:23 +00:00
|
|
|
import Prelude hiding ( appendFile )
|
|
|
|
import System.Exit
|
2022-03-10 19:26:51 +00:00
|
|
|
import URI.ByteString hiding ( uriParser )
|
2021-10-15 20:24:23 +00:00
|
|
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.ByteString.UTF8 as UTF8
|
2021-10-21 21:17:26 +00:00
|
|
|
import qualified Data.Yaml.Aeson as Y
|
2021-10-15 20:24:23 +00:00
|
|
|
import Control.Exception.Safe (MonadMask)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
----------------
|
|
|
|
--[ Commands ]--
|
|
|
|
----------------
|
|
|
|
|
|
|
|
|
|
|
|
data ConfigCommand
|
|
|
|
= ShowConfig
|
2021-10-19 18:38:53 +00:00
|
|
|
| SetConfig String (Maybe String)
|
2021-10-15 20:24:23 +00:00
|
|
|
| InitConfig
|
2022-03-10 19:26:51 +00:00
|
|
|
| AddReleaseChannel URI
|
2021-10-15 20:24:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---------------
|
|
|
|
--[ Parsers ]--
|
|
|
|
---------------
|
|
|
|
|
2022-12-03 16:15:13 +00:00
|
|
|
|
2021-10-15 20:24:23 +00:00
|
|
|
configP :: Parser ConfigCommand
|
|
|
|
configP = subparser
|
|
|
|
( command "init" initP
|
|
|
|
<> command "set" setP -- [set] KEY VALUE at help lhs
|
|
|
|
<> command "show" showP
|
2022-03-10 19:26:51 +00:00
|
|
|
<> command "add-release-channel" addP
|
2021-10-15 20:24:23 +00:00
|
|
|
)
|
|
|
|
<|> argsP -- add show for a single option
|
|
|
|
<|> pure ShowConfig
|
|
|
|
where
|
|
|
|
initP = info (pure InitConfig) (progDesc "Write default config to ~/.ghcup/config.yaml")
|
|
|
|
showP = info (pure ShowConfig) (progDesc "Show current config (default)")
|
2021-10-19 18:38:53 +00:00
|
|
|
setP = info argsP (progDesc "Set config KEY to VALUE (or specify as single json value)" <> footerDoc (Just $ text configSetFooter))
|
|
|
|
argsP = SetConfig <$> argument str (metavar "<JSON_VALUE | YAML_KEY>") <*> optional (argument str (metavar "YAML_VALUE"))
|
2022-03-10 19:26:51 +00:00
|
|
|
addP = info (AddReleaseChannel <$> argument (eitherReader uriParser) (metavar "URI" <> completer fileUri))
|
|
|
|
(progDesc "Add a release channel from a URI")
|
2021-10-15 20:24:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--------------
|
|
|
|
--[ Footer ]--
|
|
|
|
--------------
|
|
|
|
|
|
|
|
|
|
|
|
configFooter :: String
|
|
|
|
configFooter = [s|Examples:
|
|
|
|
|
|
|
|
# show current config
|
|
|
|
ghcup config
|
|
|
|
|
|
|
|
# initialize config
|
|
|
|
ghcup config init
|
|
|
|
|
|
|
|
# set <key> <value> configuration pair
|
2021-10-19 18:38:53 +00:00
|
|
|
ghcup config set <key> <value>|]
|
|
|
|
|
|
|
|
|
|
|
|
configSetFooter :: String
|
|
|
|
configSetFooter = [s|Examples:
|
|
|
|
# disable caching
|
|
|
|
ghcup config set cache false
|
|
|
|
|
|
|
|
# switch downloader to wget
|
|
|
|
ghcup config set downloader Wget
|
|
|
|
|
|
|
|
# set mirror for ghcup metadata
|
|
|
|
ghcup config set '{url-source: { OwnSource: "<url>"}}'|]
|
2021-10-15 20:24:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-----------------
|
|
|
|
--[ Utilities ]--
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
|
|
|
|
formatConfig :: UserSettings -> String
|
2021-10-21 21:17:26 +00:00
|
|
|
formatConfig = UTF8.toString . Y.encode
|
2021-10-15 20:24:23 +00:00
|
|
|
|
|
|
|
|
2022-03-10 19:26:51 +00:00
|
|
|
updateSettings :: UserSettings -> Settings -> Settings
|
|
|
|
updateSettings UserSettings{..} Settings{..} =
|
|
|
|
let cache' = fromMaybe cache uCache
|
|
|
|
metaCache' = fromMaybe metaCache uMetaCache
|
2023-01-01 11:04:00 +00:00
|
|
|
metaMode' = fromMaybe metaMode uMetaMode
|
2022-03-10 19:26:51 +00:00
|
|
|
noVerify' = fromMaybe noVerify uNoVerify
|
|
|
|
keepDirs' = fromMaybe keepDirs uKeepDirs
|
|
|
|
downloader' = fromMaybe downloader uDownloader
|
|
|
|
verbose' = fromMaybe verbose uVerbose
|
|
|
|
urlSource' = fromMaybe urlSource uUrlSource
|
|
|
|
noNetwork' = fromMaybe noNetwork uNoNetwork
|
|
|
|
gpgSetting' = fromMaybe gpgSetting uGPGSetting
|
2022-11-12 06:12:13 +00:00
|
|
|
platformOverride' = uPlatformOverride <|> platformOverride
|
2022-12-03 16:15:13 +00:00
|
|
|
mirrors' = fromMaybe mirrors uMirrors
|
|
|
|
in Settings cache' metaCache' metaMode' noVerify' keepDirs' downloader' verbose' urlSource' noNetwork' gpgSetting' noColor platformOverride' mirrors'
|
2021-10-15 20:24:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
------------------
|
|
|
|
--[ Entrypoint ]--
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-10 19:26:51 +00:00
|
|
|
config :: forall m. ( Monad m
|
2021-10-15 20:24:23 +00:00
|
|
|
, MonadMask m
|
|
|
|
, MonadUnliftIO m
|
|
|
|
, MonadFail m
|
|
|
|
)
|
|
|
|
=> ConfigCommand
|
|
|
|
-> Settings
|
|
|
|
-> KeyBindings
|
|
|
|
-> (ReaderT LeanAppState m () -> m ())
|
|
|
|
-> m ExitCode
|
|
|
|
config configCommand settings keybindings runLogger = case configCommand of
|
|
|
|
InitConfig -> do
|
|
|
|
path <- getConfigFilePath
|
|
|
|
liftIO $ writeFile path $ formatConfig $ fromSettings settings (Just keybindings)
|
|
|
|
runLogger $ logDebug $ "config.yaml initialized at " <> T.pack path
|
|
|
|
pure ExitSuccess
|
|
|
|
|
|
|
|
ShowConfig -> do
|
|
|
|
liftIO $ putStrLn $ formatConfig $ fromSettings settings (Just keybindings)
|
|
|
|
pure ExitSuccess
|
|
|
|
|
2022-03-10 19:26:51 +00:00
|
|
|
(SetConfig k mv) -> do
|
|
|
|
r <- runE @'[JSONError, ParseError] $ do
|
|
|
|
case mv of
|
|
|
|
Just "" ->
|
|
|
|
throwE $ ParseError "Empty values are not allowed"
|
|
|
|
Nothing -> do
|
|
|
|
usersettings <- decodeSettings k
|
|
|
|
lift $ doConfig usersettings
|
|
|
|
pure ()
|
|
|
|
Just v -> do
|
|
|
|
usersettings <- decodeSettings (k <> ": " <> v <> "\n")
|
|
|
|
lift $ doConfig usersettings
|
|
|
|
pure ()
|
|
|
|
case r of
|
|
|
|
VRight _ -> pure ExitSuccess
|
|
|
|
VLeft (V (JSONDecodeError e)) -> do
|
|
|
|
runLogger $ logError $ "Error decoding config: " <> T.pack e
|
|
|
|
pure $ ExitFailure 65
|
|
|
|
VLeft _ -> pure $ ExitFailure 65
|
|
|
|
|
|
|
|
AddReleaseChannel uri -> do
|
|
|
|
case urlSource settings of
|
|
|
|
AddSource xs -> do
|
|
|
|
doConfig (defaultUserSettings { uUrlSource = Just $ AddSource (xs <> [Right uri]) })
|
|
|
|
pure ExitSuccess
|
2022-12-19 16:57:56 +00:00
|
|
|
GHCupURL -> do
|
2022-03-10 19:26:51 +00:00
|
|
|
doConfig (defaultUserSettings { uUrlSource = Just $ AddSource [Right uri] })
|
|
|
|
pure ExitSuccess
|
2022-12-19 16:57:56 +00:00
|
|
|
OwnSource xs -> do
|
|
|
|
doConfig (defaultUserSettings { uUrlSource = Just $ OwnSource (xs <> [Right uri]) })
|
|
|
|
pure ExitSuccess
|
|
|
|
OwnSpec spec -> do
|
|
|
|
doConfig (defaultUserSettings { uUrlSource = Just $ OwnSource ([Left spec, Right uri]) })
|
|
|
|
pure ExitSuccess
|
2021-10-19 18:38:53 +00:00
|
|
|
|
|
|
|
where
|
2022-03-10 19:26:51 +00:00
|
|
|
doConfig :: MonadIO m => UserSettings -> m ()
|
|
|
|
doConfig usersettings = do
|
|
|
|
let settings' = updateSettings usersettings settings
|
|
|
|
path <- liftIO getConfigFilePath
|
|
|
|
liftIO $ writeFile path $ formatConfig $ fromSettings settings' (Just keybindings)
|
|
|
|
runLogger $ logDebug $ T.pack $ show settings'
|
|
|
|
pure ()
|
|
|
|
|
|
|
|
decodeSettings = lE' (JSONDecodeError . displayException) . Y.decodeEither' . UTF8.fromString
|