diff options
author | Andrew Cady <d@jerkface.net> | 2019-10-04 13:48:36 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2019-10-04 13:51:57 -0400 |
commit | cd7dc6266b415cf3c937fc3b3aa14fce999c6f0d (patch) | |
tree | fe89beb18d89d29cee49f2e04326fc9152638bbf /fsmgr.hs | |
parent | a9aab3eddf957e1ac4ef29a1dc6374567827c343 (diff) |
factor out string functions
most of these were copied from MissingH
Diffstat (limited to 'fsmgr.hs')
-rw-r--r-- | fsmgr.hs | 42 |
1 files changed, 3 insertions, 39 deletions
@@ -27,56 +27,20 @@ import System.Directory (getCurrentDirectory, createDirectoryIfMissing) | |||
27 | import System.Posix.Process (getProcessID) | 27 | import System.Posix.Process (getProcessID) |
28 | import System.Posix.Types (CUid (..)) | 28 | import System.Posix.Types (CUid (..)) |
29 | import System.Posix.User (getEffectiveUserID) | 29 | import System.Posix.User (getEffectiveUserID) |
30 | import String | ||
30 | 31 | ||
31 | noParent :: BaseImageSpecification -> Bool | 32 | noParent :: BaseImageSpecification -> Bool |
32 | noParent (EmptyImageOfBytes _) = True | 33 | noParent (EmptyImageOfBytes _) = True |
33 | noParent (ParentImageConfigFile _) = False | 34 | noParent (ParentImageConfigFile _) = False |
34 | 35 | ||
35 | split :: Eq a => [a] -> [a] -> [[a]] | 36 | dynamicNames :: FilePath -> FilePath |
36 | split _ [] = [] | 37 | dynamicNames = replace "$(karch)" uname |
37 | split delim str = | ||
38 | let (firstline, remainder) = breakList (startswith delim) str | ||
39 | in | ||
40 | firstline : case remainder of | ||
41 | [] -> [] | ||
42 | x -> if x == delim | ||
43 | then [] : [] | ||
44 | else split delim | ||
45 | (drop (length delim) x) | ||
46 | |||
47 | spanList :: ([a] -> Bool) -> [a] -> ([a], [a]) | ||
48 | spanList _ [] = ([],[]) | ||
49 | spanList func list@(x:xs) = | ||
50 | if func list | ||
51 | then (x:ys,zs) | ||
52 | else ([],list) | ||
53 | where (ys,zs) = spanList func xs | ||
54 | |||
55 | breakList :: ([a] -> Bool) -> [a] -> ([a], [a]) | ||
56 | breakList func = spanList (not . func) | ||
57 | |||
58 | startswith :: Eq a => [a] -> [a] -> Bool | ||
59 | startswith = isPrefixOf | ||
60 | |||
61 | join :: [a] -> [[a]] -> [a] | ||
62 | join delim l = concat (intersperse delim l) | ||
63 | |||
64 | replace :: Eq a => [a] -> [a] -> [a] -> [a] | ||
65 | replace old new l = join new . split old $ l | ||
66 | |||
67 | wordsBy c s = words (rep <$> s) | ||
68 | where | ||
69 | rep x | x == c = ' ' | ||
70 | | otherwise = x | ||
71 | 38 | ||
72 | uname :: String | 39 | uname :: String |
73 | uname = unsafePerformIO $ do | 40 | uname = unsafePerformIO $ do |
74 | Stdout out <- cmd "uname -r" | 41 | Stdout out <- cmd "uname -r" |
75 | return $ last . wordsBy '-' . head . lines $ out | 42 | return $ last . wordsBy '-' . head . lines $ out |
76 | 43 | ||
77 | dynamicNames :: FilePath -> FilePath | ||
78 | dynamicNames = replace "$(karch)" uname | ||
79 | |||
80 | buildRoot :: DiskImageConfig -> FilePath -> Action () | 44 | buildRoot :: DiskImageConfig -> FilePath -> Action () |
81 | buildRoot config@DiskImageConfig{..} finalOut = do | 45 | buildRoot config@DiskImageConfig{..} finalOut = do |
82 | let out = finalOut <.> "tmp" | 46 | let out = finalOut <.> "tmp" |