packages feed

hifi 0.1.2.0 → 0.1.3.0

raw patch · 4 files changed

+76/−61 lines, 4 filesdep ~directorydep ~filepathdep ~mustachePVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependency ranges changed: directory, filepath, mustache, parsec, process, text, unix

API changes (from Hackage documentation)

- Config: dataDir :: IO FilePath
- Config: scriptDir :: IO FilePath
- Config: templateDir :: IO FilePath
+ Config: dataDirectory :: IO FilePath
+ Config: scriptDirectory :: IO FilePath
+ Config: templateDirectory :: IO FilePath

Files

app/Main.hs view
@@ -4,17 +4,13 @@ import           Templating  main :: IO ()-main =-  do-    args <- getArgs-    progName <- getProgName--    case args of-      [filename, iface, ssid, passphrase] ->-        do-          (scriptFile, dataFile) <- createFiles filename iface ssid passphrase-          putStrLn $ "Created " ++ scriptFile ++ " and " ++ dataFile--      _ ->-        putStrLn $ "Usage:\n\t"-        ++ progName ++ " <filename> <interface> <ssid> <passphrase>"+main = do+  args <- getArgs+  progName <- getProgName+  case args of+    [filename, iface, ssid, passphrase] -> do+      (scriptFile, dataFile) <- createFiles filename iface ssid passphrase+      putStrLn $ "Created " ++ scriptFile ++ " and " ++ dataFile+    _ ->+      putStrLn $+      "Usage:\n\t" ++ progName ++ " <filename> <interface> <ssid> <passphrase>"
hifi.cabal view
@@ -1,5 +1,5 @@ name:                hifi-version:             0.1.2.0+version:             0.1.3.0 synopsis:            WiFi connection script generator description:         A CLI tool generating scripts for connecting to WiFi, circumventing big WiFi management tools. homepage:            https://gitlab.com/gonz/hifi@@ -18,13 +18,13 @@   hs-source-dirs:      src   exposed-modules:     Templating, Config   build-depends:       base >= 4.7 && < 5-                     , mustache-                     , directory-                     , text-                     , process-                     , parsec-                     , filepath-                     , unix+                     , mustache >= 2.3 && < 3+                     , directory >= 1.3 && < 2+                     , text >= 1.2.3 && < 2+                     , process >= 1.6 && < 2+                     , parsec >= 3.1 && < 4+                     , filepath >= 1.4 && < 2+                     , unix >= 2.7 && < 3   default-language:    Haskell2010   other-modules:       Paths_hifi 
src/Config.hs view
@@ -5,37 +5,41 @@ -- -- This usually defaults to '$HOME/.local' in most Linux distributions, -- but can also be set manually if one chooses to do so.-module Config (templateDir, dataDir, scriptDir) where+module Config+  ( templateDirectory+  , dataDirectory+  , scriptDirectory+  ) where -import System.Directory-import Paths_hifi+import           Paths_hifi+import           System.Directory  createIfMissingPath :: FilePath -> IO FilePath-createIfMissingPath path = createDirectoryIfMissing True path >> return path+createIfMissingPath path = createDirectoryIfMissing True path >> pure path --- | 'xdgDir' finds the user's '$XDG_DATA_HOME' directory and appends a given+-- | 'xdgDirectory' finds the user's '$XDG_DATA_HOME' directory and appends a given -- path to it. '$XDG_DATA_HOME' defaults to '$HOME/.local' in many Linux -- distributions, but can also be set manually.-xdgDir :: FilePath -> IO FilePath-xdgDir = getXdgDirectory XdgData+xdgDirectory :: FilePath -> IO FilePath+xdgDirectory = getXdgDirectory XdgData --- | 'templateDir' will point to the directory where the supplied data files (templates)+-- | 'templateDirectory' will point to the directory where the supplied data files (templates) -- end up. The data dir will, when using `stack` end up in the `.stack-work` -- folder. This directory is then used in order to read the templates at runtime -- so that they can be used when writing new script/data files.-templateDir :: IO FilePath-templateDir = getDataFileName "templates"+templateDirectory :: IO FilePath+templateDirectory = getDataFileName "templates" --- | 'dataDir' points to a folder in the XDG data directory where it will store+-- | 'dataDirectory' points to a folder in the XDG data directory where it will store -- the data files used to connect to networks. The subdirectory that will be used -- is '$XDG_DATA_HOME/hifi/data'. If this directory does not already exist when--- 'dataDir' is evaluated, it will be created.-dataDir :: IO FilePath-dataDir = xdgDir "hifi/data" >>= createIfMissingPath+-- 'dataDirectory' is evaluated, it will be created.+dataDirectory :: IO FilePath+dataDirectory = xdgDirectory "hifi/data" >>= createIfMissingPath --- | 'scriptDir' points to a folder in the XDG data directory where it will store+-- | 'scriptDirectory' points to a folder in the XDG data directory where it will store -- the scripts it generates to connect to networks. The subdirectory that will be -- used is '$XDG_DATA_HOME/hifi/scripts'. If this directory does not already exist when--- 'scriptDir' is evaluated, it will be created.-scriptDir :: IO FilePath-scriptDir = xdgDir "hifi/scripts" >>= createIfMissingPath+-- 'scriptDirectory' is evaluated, it will be created.+scriptDirectory :: IO FilePath+scriptDirectory = xdgDirectory "hifi/scripts" >>= createIfMissingPath
src/Templating.hs view
@@ -2,7 +2,9 @@  -- | Handles the execution of 'wpa_*' commands as well as the template loading, -- compilation and substitution.-module Templating (createFiles) where+module Templating+  ( createFiles+  ) where  import qualified Config import qualified Data.Text.IO       as TextIO@@ -13,61 +15,74 @@ import           Text.Mustache import           Text.Parsec.Error  (ParseError) -data ScriptSpec = ScriptSpec { interface :: String, dataFilename :: String }+data ScriptSpec = ScriptSpec+  { interface    :: String+  , dataFilename :: String+  }  instance ToMustache ScriptSpec where-  toMustache spec = object ["interface" ~> interface spec,-                            "data_filename" ~> dataFilename spec]+  toMustache spec =+    object ["interface" ~> interface spec, "data_filename" ~> dataFilename spec] -newtype DataSpec = DataSpec { wpaPassphraseOutput :: String}+newtype DataSpec = DataSpec+  { wpaPassphraseOutput :: String+  }  instance ToMustache DataSpec where   toMustache spec = object ["wpa_passphrase_output" ~> wpaPassphraseOutput spec]  type Interface = String+ type SSID = String+ type Passphrase = String+ type FileResult = Either ParseError FilePath +type WPAPassphraseOutput = String+ createScript :: FilePath -> Interface -> FilePath -> IO FileResult createScript filename iface dataFile =-  createFile filename Config.scriptDir scriptSpec "script.mustache"-  where scriptSpec = ScriptSpec { interface = iface, dataFilename = dataFile }+  createFile filename Config.scriptDirectory scriptSpec "script.mustache"+  where+    scriptSpec = ScriptSpec {interface = iface, dataFilename = dataFile} -runWPAPassphrase :: SSID -> Passphrase -> IO String+runWPAPassphrase :: SSID -> Passphrase -> IO WPAPassphraseOutput runWPAPassphrase ssid passphrase = do-  (_, Just hout, _, _) <- createProcess wpa_passphrase_process-                          { std_out = CreatePipe }+  (_, Just hout, _, _) <-+    createProcess wpa_passphrase_process {std_out = CreatePipe}   IO.hGetContents hout-  where wpa_passphrase_process = proc "wpa_passphrase" [ssid, passphrase]+  where+    wpa_passphrase_process = proc "wpa_passphrase" [ssid, passphrase] -createData :: FilePath -> String -> IO FileResult+createData :: FilePath -> WPAPassphraseOutput -> IO FileResult createData filename wpaPPoutput =-  createFile filename Config.dataDir dataSpec "data.mustache"-  where dataSpec = DataSpec {wpaPassphraseOutput = wpaPPoutput}+  createFile filename Config.dataDirectory dataSpec "data.mustache"+  where+    dataSpec = DataSpec {wpaPassphraseOutput = wpaPPoutput}  createFile ::-  ToMustache a => FilePath -> IO FilePath -> a -> FilePath -> IO FileResult+     ToMustache a => FilePath -> IO FilePath -> a -> FilePath -> IO FileResult createFile filename outputDirFunc spec templateFile = do-  templateDir <- Config.templateDir+  templateDir <- Config.templateDirectory   outputDir <- outputDirFunc   compiled <- automaticCompile [templateDir] templateFile   let outputPath = outputDir </> filename   case compiled of-    Left err -> return (Left err)+    Left err -> pure (Left err)     Right template -> do       TextIO.writeFile outputPath $ substitute template spec-      return (Right outputPath)+      pure (Right outputPath)  -- | 'createFiles' creates both the script- and data-file for a 'FilePath', -- 'Interface', 'SSID' and 'Passphrase'. These filenames are then returned in a -- tuple so that they may be used to notify the user of their creation.-createFiles :: FilePath -> Interface -> SSID -> Passphrase-  -> IO (FilePath, FilePath)+createFiles ::+     FilePath -> Interface -> SSID -> Passphrase -> IO (FilePath, FilePath) createFiles filename iface ssid passphrase = do   wpaPPoutput <- runWPAPassphrase ssid passphrase   (Right dataFile) <- createData filename wpaPPoutput   (Right scriptFile) <- createScript filename iface dataFile   setFileMode scriptFile ownerModes   setFileMode dataFile (unionFileModes ownerWriteMode ownerReadMode)-  return (dataFile, scriptFile)+  pure (dataFile, scriptFile)