summaryrefslogtreecommitdiff
path: root/src/Propellor/Keyring.hs
blob: c3018eb98fdfc62fc096da4378baf5678555311b (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
module Propellor.Keyring where

import Propellor
import Utility.SafeCommand

keyring :: FilePath
keyring = privDataDir </> "keyring.gpg"

addKey :: String -> IO ()
addKey keyid = exitBool =<< allM id [ gpg, gitadd, gitconfig, gitcommit ]
  where
	gpg = do
		createDirectoryIfMissing True privDataDir
		boolSystem "sh"
			[ Param "-c"
			, Param $ "gpg --export " ++ keyid ++ " | gpg " ++
				unwords (gpgopts ++ ["--import"])
			]
	gitadd = boolSystem "git"
		[ Param "add"
		, File keyring
		]

	gitconfig = boolSystem "git"
		[ Param "config"
		, Param "user.signingkey"
		, Param keyid
		]

	gitcommit = gitCommit
		[ File keyring
		, Param "-m"
		, Param "propellor addkey"
		]

	gpgopts =
		[ "--options"
		, "/dev/null"
		, "--no-default-keyring"
		, "--keyring", keyring
		]

{- Automatically sign the commit if there'a a keyring. -}
gitCommit :: [CommandParam] -> IO Bool
gitCommit ps = do
	k <- doesFileExist keyring
	boolSystem "git" $ catMaybes $
		[ Just (Param "commit")
		, if k then Just (Param "--gpg-sign") else Nothing
		] ++ map Just ps