summaryrefslogtreecommitdiff
path: root/doc/forum/Script_to_convert_config_files_for_inclusion_in_Propellor_config.mdwn
blob: c9f5ec8b4f217b06479fbee2fd1fdd3952919119 (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
This script turns

    Section "Monitor"
    	Identifier "Configured Monitor"
    EndSection

into this:

    [ "Section \"Monitor\""
    , "\tIdentifier \"Configured Monitor\""
    , "EndSection"
    ]

for the inclusion of short config files in your Propellor config using `File.hasContent`.

[[!format haskell """
#!/usr/bin/runhaskell

main = interact $ unlines . propellorLines . lines

propellorLines        :: [String] -> [String]
propellorLines (x:xs) = ("[ " ++ wrapEscapeLine x) : propellorLines' xs

propellorLines' :: [String] -> [String]
propellorLines' = foldr step ["]"]
  where
    step x xs = (", " ++ wrapEscapeLine x) : xs

wrapEscapeLine      :: String -> String
wrapEscapeLine line = "\"" ++ (foldr step "" line) ++ "\""
  where
    step x xs
    	| x == '\t' = "\\t" ++ xs
        | x == '\\' = x : x : xs
        | x == '"'  = '\\' : x : xs
        | otherwise = x : xs
"""]]

Usage: `cat config_file | propellor_lines` (or in Emacs, dump the config file into your propellor config, select the region and use `C-u M-|` to pipe it through).

-- [[spwhitton|https://spwhitton.name]]