packages feed

qr-imager-0.1.2.0: src/Data/QRCodes/Exe.hs

module Data.QRCodes.Exe (exec) where

import Options.Applicative
import qualified Data.ByteString as B
import System.Environment (getArgs) --fix soon!
import Data.QRCodes

data Prog = Prog { cmd     :: Com
                 , secured :: Bool
                 , verify  :: Bool
                 , file    :: String} --whether to verify the encoding worked

data Com = Input | Output--whether to be writing *to* file or reading *from it

exec :: IO ()
exec = execParser full >>= act
    where 
        full = info (helper <*> program)
            ( fullDesc
            <> progDesc "Read/Write QR Codes with files"
            <> header "qrpipe - QR utilities made in Haskell" )

act :: Prog -> IO ()
act (Prog Output True True filepath) = do
    pipeIn <- B.getContents
    byteStringToQRSec pipeIn filepath
    readQRStrSec filepath >>= print
act (Prog Output False True filepath) = do
    pipeIn <- B.getContents
    byteStringToQR pipeIn filepath
    readQRString filepath >>= print
act (Prog Output True False filepath) = do
    pipeIn <- B.getContents
    byteStringToQRSec pipeIn filepath
act (Prog Output False False filepath) = do
    pipeIn <- B.getContents
    byteStringToQR pipeIn filepath
act (Prog Input True _ filepath) = do
    readQRStrSec filepath >>= print
act (Prog Input False _ filepath) = do
    readQRString filepath >>= print

program :: Parser Prog
program = Prog
    <$> subparser
        ( command "write" (info (pure Output)
            ( progDesc "Create a QR Code from stdin" ))
        <> command "read" (info (pure Input)
            ( progDesc "Read a QR code from file" )))
    <*> switch
        ( long "signed"
        <> short 's'
        <> help "Whether to sign the QR code" )
    <*> switch
        ( long "verify"
        <> short 'v'
        <> help "Attempt to read the resultant file?" )
    <*> argument str (metavar "FILE")