summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2019-10-04 13:48:36 -0400
committerAndrew Cady <d@jerkface.net>2019-10-04 13:51:57 -0400
commitcd7dc6266b415cf3c937fc3b3aa14fce999c6f0d (patch)
treefe89beb18d89d29cee49f2e04326fc9152638bbf
parenta9aab3eddf957e1ac4ef29a1dc6374567827c343 (diff)
factor out string functions
most of these were copied from MissingH
-rw-r--r--fsmgr.cabal2
-rw-r--r--fsmgr.hs42
-rw-r--r--src/String.hs44
3 files changed, 48 insertions, 40 deletions
diff --git a/fsmgr.cabal b/fsmgr.cabal
index 7f7708d..4d2de80 100644
--- a/fsmgr.cabal
+++ b/fsmgr.cabal
@@ -19,7 +19,7 @@ source-repository head
19 19
20library 20library
21 exposed-modules: 21 exposed-modules:
22 Crypto.Hash.Types.Digest.Read, ConfigFile 22 Crypto.Hash.Types.Digest.Read, ConfigFile, String
23 other-modules: 23 other-modules:
24 Paths_fsmgr 24 Paths_fsmgr
25 hs-source-dirs: 25 hs-source-dirs:
diff --git a/fsmgr.hs b/fsmgr.hs
index 66bcaa8..770c8bc 100644
--- a/fsmgr.hs
+++ b/fsmgr.hs
@@ -27,56 +27,20 @@ import System.Directory (getCurrentDirectory, createDirectoryIfMissing)
27import System.Posix.Process (getProcessID) 27import System.Posix.Process (getProcessID)
28import System.Posix.Types (CUid (..)) 28import System.Posix.Types (CUid (..))
29import System.Posix.User (getEffectiveUserID) 29import System.Posix.User (getEffectiveUserID)
30import String
30 31
31noParent :: BaseImageSpecification -> Bool 32noParent :: BaseImageSpecification -> Bool
32noParent (EmptyImageOfBytes _) = True 33noParent (EmptyImageOfBytes _) = True
33noParent (ParentImageConfigFile _) = False 34noParent (ParentImageConfigFile _) = False
34 35
35split :: Eq a => [a] -> [a] -> [[a]] 36dynamicNames :: FilePath -> FilePath
36split _ [] = [] 37dynamicNames = replace "$(karch)" uname
37split 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
47spanList :: ([a] -> Bool) -> [a] -> ([a], [a])
48spanList _ [] = ([],[])
49spanList func list@(x:xs) =
50 if func list
51 then (x:ys,zs)
52 else ([],list)
53 where (ys,zs) = spanList func xs
54
55breakList :: ([a] -> Bool) -> [a] -> ([a], [a])
56breakList func = spanList (not . func)
57
58startswith :: Eq a => [a] -> [a] -> Bool
59startswith = isPrefixOf
60
61join :: [a] -> [[a]] -> [a]
62join delim l = concat (intersperse delim l)
63
64replace :: Eq a => [a] -> [a] -> [a] -> [a]
65replace old new l = join new . split old $ l
66
67wordsBy c s = words (rep <$> s)
68 where
69 rep x | x == c = ' '
70 | otherwise = x
71 38
72uname :: String 39uname :: String
73uname = unsafePerformIO $ do 40uname = 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
77dynamicNames :: FilePath -> FilePath
78dynamicNames = replace "$(karch)" uname
79
80buildRoot :: DiskImageConfig -> FilePath -> Action () 44buildRoot :: DiskImageConfig -> FilePath -> Action ()
81buildRoot config@DiskImageConfig{..} finalOut = do 45buildRoot config@DiskImageConfig{..} finalOut = do
82 let out = finalOut <.> "tmp" 46 let out = finalOut <.> "tmp"
diff --git a/src/String.hs b/src/String.hs
new file mode 100644
index 0000000..633c920
--- /dev/null
+++ b/src/String.hs
@@ -0,0 +1,44 @@
1{-# LANGUAGE NoImplicitPrelude #-}
2
3module String where
4
5import Rebase.Prelude hiding (bool, hash, join, o, (<.>))
6
7split :: Eq a => [a] -> [a] -> [[a]]
8split _ [] = []
9split delim str =
10 let (firstline, remainder) = breakList (startswith delim) str
11 in
12 firstline : case remainder of
13 [] -> []
14 x -> if x == delim
15 then [] : []
16 else split delim
17 (drop (length delim) x)
18
19spanList :: ([a] -> Bool) -> [a] -> ([a], [a])
20spanList _ [] = ([],[])
21spanList func list@(x:xs) =
22 if func list
23 then (x:ys,zs)
24 else ([],list)
25 where (ys,zs) = spanList func xs
26
27breakList :: ([a] -> Bool) -> [a] -> ([a], [a])
28breakList func = spanList (not . func)
29
30startswith :: Eq a => [a] -> [a] -> Bool
31startswith = isPrefixOf
32
33join :: [a] -> [[a]] -> [a]
34join delim l = concat (intersperse delim l)
35
36replace :: Eq a => [a] -> [a] -> [a] -> [a]
37replace old new l = join new . split old $ l
38
39wordsBy :: Char -> [Char] -> [String]
40wordsBy c s = words (rep <$> s)
41 where
42 rep x | x == c = ' '
43 | otherwise = x
44