aboutsummaryrefslogtreecommitdiff
path: root/Command/Config.hs
blob: 690c8d288855efd5f133c0afcb2a5800d8439e3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{- git-annex command
 -
 - Copyright 2017 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Command.Config where

import Command
import Logs.Config

cmd :: Command
cmd = command "config" SectionSetup "configuration stored in git-annex branch"
	paramNothing (seek <$$> optParser)

data Action
	= SetConfig ConfigName ConfigValue
	| GetConfig ConfigName
	| UnsetConfig ConfigName

type Name = String
type Value = String

optParser :: CmdParamsDesc -> Parser Action
optParser _ = setconfig <|> getconfig <|> unsetconfig
  where
	setconfig = SetConfig
		<$> strOption
			( long "set"
			<> help "set configuration"
			<> metavar paramName
			)
		<*> strArgument
			( metavar paramValue
			)
	getconfig = GetConfig <$> strOption
		( long "get"
		<> help "get configuration"
		<> metavar paramName
		)
	unsetconfig = UnsetConfig <$> strOption
		( long "unset"
		<> help "unset configuration"
		<> metavar paramName
		)

seek :: Action -> CommandSeek
seek (SetConfig name val) = commandAction $ do
	showStart name val
	next $ next $ do
		setGlobalConfig name val
		return True
seek (UnsetConfig name) = commandAction $ do
	showStart name "unset"
	next $ next $ do
		unsetGlobalConfig name
		return True
seek (GetConfig name) = commandAction $ do
	mv <- getGlobalConfig name
	case mv of
		Nothing -> stop
		Just v -> do
			liftIO $ putStrLn v
			stop