diff options
author | joe <joe@jerkface.net> | 2016-05-02 00:21:02 +0000 |
---|---|---|
committer | joe <joe@jerkface.net> | 2016-05-02 00:21:02 +0000 |
commit | b04dd1bba833852e22844696ecad365a11248e98 (patch) | |
tree | 344581d239dbb97da1f033a350f37693915e64e0 | |
parent | c070e563c3c1a04dc722c495acea5f9739c3334b (diff) |
implemented cokiki hosts command to merge cache hosts with etc hosts.
-rw-r--r-- | cokiki.hs | 14 |
1 files changed, 14 insertions, 0 deletions
@@ -19,6 +19,7 @@ import System.Exit | |||
19 | import System.IO | 19 | import System.IO |
20 | import System.Posix.User | 20 | import System.Posix.User |
21 | import CommandLine | 21 | import CommandLine |
22 | import qualified Hosts | ||
22 | 23 | ||
23 | usage = unlines | 24 | usage = unlines |
24 | [ "cokiki <command> [--chroot=ROOTDIR]" | 25 | [ "cokiki <command> [--chroot=ROOTDIR]" |
@@ -52,6 +53,9 @@ usage = unlines | |||
52 | , "" | 53 | , "" |
53 | , " tor Modify /etc/tor/torrc to configure a tor hidden" | 54 | , " tor Modify /etc/tor/torrc to configure a tor hidden" |
54 | , " service for email (smtp), ssh, and http ports." | 55 | , " service for email (smtp), ssh, and http ports." |
56 | , "" | ||
57 | , " hosts Merge hostnames from /var/cache/kiki/config/hosts" | ||
58 | , " into system file /etc/hosts." | ||
55 | ] | 59 | ] |
56 | 60 | ||
57 | main = do | 61 | main = do |
@@ -62,6 +66,7 @@ main = do | |||
62 | ["ssh-server"] -> Just $ sshServer uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir | 66 | ["ssh-server"] -> Just $ sshServer uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir |
63 | ["strongswan"] -> Just $ strongswan uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir | 67 | ["strongswan"] -> Just $ strongswan uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir |
64 | ["tor"] -> Just $ configureTor uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir | 68 | ["tor"] -> Just $ configureTor uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir |
69 | ["hosts"] -> Just $ configureHosts uid <$> Kiki.ㄧchroot <*> Kiki.ㄧhomedir | ||
65 | _ -> Nothing | 70 | _ -> Nothing |
66 | spec = uncurry fancy Kiki.kikiOptions "" | 71 | spec = uncurry fancy Kiki.kikiOptions "" |
67 | errorQuit msg = do | 72 | errorQuit msg = do |
@@ -77,6 +82,7 @@ main = do | |||
77 | maybeReadFile :: FilePath -> IO (Maybe L.ByteString) | 82 | maybeReadFile :: FilePath -> IO (Maybe L.ByteString) |
78 | maybeReadFile path = do | 83 | maybeReadFile path = do |
79 | doesFileExist path >>= bool (return Nothing) (Just <$> L.readFile path) | 84 | doesFileExist path >>= bool (return Nothing) (Just <$> L.readFile path) |
85 | |||
80 | myWriteFile f bs = do | 86 | myWriteFile f bs = do |
81 | createDirectoryIfMissing True (takeDirectory f) | 87 | createDirectoryIfMissing True (takeDirectory f) |
82 | hPutStrLn stderr $ "Writing "++f | 88 | hPutStrLn stderr $ "Writing "++f |
@@ -212,6 +218,14 @@ configureTor uid root cmn = whenRoot uid root cmn $ do | |||
212 | Kiki.importAndRefresh root cmn | 218 | Kiki.importAndRefresh root cmn |
213 | return () | 219 | return () |
214 | 220 | ||
221 | configureHosts uid root cmn = whenRoot uid root cmn $ do | ||
222 | Kiki.importAndRefresh root cmn | ||
223 | hosts <- Hosts.decode . fromMaybe "" <$> maybeReadFile (root "/etc/hosts") | ||
224 | kikihosts <- Hosts.decode . fromMaybe "" <$> maybeReadFile (root "/var/cache/kiki/config/hosts") | ||
225 | let hosts' = hosts `Hosts.plus` kikihosts | ||
226 | case Hosts.diff hosts hosts' of | ||
227 | [] -> hPutStrLn stderr "kiki hosts are already merged." | ||
228 | _ -> myWriteFile (root "/etc/hosts") $ Hosts.encode hosts' | ||
215 | 229 | ||
216 | parseSshConfig :: L.ByteString -> [[L.ByteString]] | 230 | parseSshConfig :: L.ByteString -> [[L.ByteString]] |
217 | parseSshConfig bs = map tokenize $ L8.lines bs | 231 | parseSshConfig bs = map tokenize $ L8.lines bs |