Allow to specify config value as JSON
This commit is contained in:
parent
69df100b18
commit
2a11e85a95
@ -28,6 +28,7 @@ import Data.Functor
|
|||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import Haskus.Utils.Variant.Excepts
|
import Haskus.Utils.Variant.Excepts
|
||||||
import Options.Applicative hiding ( style )
|
import Options.Applicative hiding ( style )
|
||||||
|
import Options.Applicative.Help.Pretty ( text )
|
||||||
import Prelude hiding ( appendFile )
|
import Prelude hiding ( appendFile )
|
||||||
import System.Exit
|
import System.Exit
|
||||||
|
|
||||||
@ -46,7 +47,7 @@ import Control.Exception.Safe (MonadMask)
|
|||||||
|
|
||||||
data ConfigCommand
|
data ConfigCommand
|
||||||
= ShowConfig
|
= ShowConfig
|
||||||
| SetConfig String String
|
| SetConfig String (Maybe String)
|
||||||
| InitConfig
|
| InitConfig
|
||||||
|
|
||||||
|
|
||||||
@ -67,8 +68,8 @@ configP = subparser
|
|||||||
where
|
where
|
||||||
initP = info (pure InitConfig) (progDesc "Write default config to ~/.ghcup/config.yaml")
|
initP = info (pure InitConfig) (progDesc "Write default config to ~/.ghcup/config.yaml")
|
||||||
showP = info (pure ShowConfig) (progDesc "Show current config (default)")
|
showP = info (pure ShowConfig) (progDesc "Show current config (default)")
|
||||||
setP = info argsP (progDesc "Set config KEY to VALUE")
|
setP = info argsP (progDesc "Set config KEY to VALUE (or specify as single json value)" <> footerDoc (Just $ text configSetFooter))
|
||||||
argsP = SetConfig <$> argument str (metavar "KEY") <*> argument str (metavar "VALUE")
|
argsP = SetConfig <$> argument str (metavar "<JSON_VALUE | YAML_KEY>") <*> optional (argument str (metavar "YAML_VALUE"))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -88,7 +89,19 @@ configFooter = [s|Examples:
|
|||||||
ghcup config init
|
ghcup config init
|
||||||
|
|
||||||
# set <key> <value> configuration pair
|
# set <key> <value> configuration pair
|
||||||
ghcup config <key> <value>|]
|
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>"}}'|]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -147,22 +160,27 @@ config configCommand settings keybindings runLogger = case configCommand of
|
|||||||
liftIO $ putStrLn $ formatConfig $ fromSettings settings (Just keybindings)
|
liftIO $ putStrLn $ formatConfig $ fromSettings settings (Just keybindings)
|
||||||
pure ExitSuccess
|
pure ExitSuccess
|
||||||
|
|
||||||
(SetConfig k v) -> do
|
(SetConfig k (Just v)) ->
|
||||||
case v of
|
case v of
|
||||||
"" -> do
|
"" -> do
|
||||||
runLogger $ logError "Empty values are not allowed"
|
runLogger $ logError "Empty values are not allowed"
|
||||||
pure $ ExitFailure 55
|
pure $ ExitFailure 55
|
||||||
_ -> do
|
_ -> doConfig (k <> ": " <> v <> "\n")
|
||||||
r <- runE @'[JSONError] $ do
|
|
||||||
settings' <- updateSettings (UTF8.fromString (k <> ": " <> v <> "\n")) settings
|
|
||||||
path <- liftIO getConfigFilePath
|
|
||||||
liftIO $ writeFile path $ formatConfig $ fromSettings settings' (Just keybindings)
|
|
||||||
lift $ runLogger $ logDebug $ T.pack $ show settings'
|
|
||||||
pure ()
|
|
||||||
|
|
||||||
case r of
|
(SetConfig json Nothing) -> doConfig json
|
||||||
VRight _ -> pure ExitSuccess
|
|
||||||
VLeft (V (JSONDecodeError e)) -> do
|
where
|
||||||
runLogger $ logError $ "Error decoding config: " <> T.pack e
|
doConfig val = do
|
||||||
pure $ ExitFailure 65
|
r <- runE @'[JSONError] $ do
|
||||||
VLeft _ -> pure $ ExitFailure 65
|
settings' <- updateSettings (UTF8.fromString val) settings
|
||||||
|
path <- liftIO getConfigFilePath
|
||||||
|
liftIO $ writeFile path $ formatConfig $ fromSettings settings' (Just keybindings)
|
||||||
|
lift $ runLogger $ logDebug $ T.pack $ show settings'
|
||||||
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user