<commit_before>module Main (main) where
  
import           Options.Applicative
import           Data.Monoid
import qualified Data.Text as T
import qualified Data.Either.Strict as S
import qualified Data.HashMap.Strict as HM
import           Text.PrettyPrint.ANSI.Leijen (pretty)

import Puppet.Interpreter.Types
import Puppet.Interpreter.PrettyPrinter()
import Hiera.Server

data Config
  = Config
  { _filepath    :: FilePath
  , _query       :: String
  , _queryType   :: HieraQueryType
  , _variables   :: [(T.Text,T.Text)]
  }

readQT :: String -> Maybe HieraQueryType
readQT s
  = case s of
    "first"  -> Just QFirst
    "unique" -> Just QUnique
    "hash"   -> Just QHash
    _        -> Nothing

parseVariable :: String -> Either String (T.Text, T.Text)
parseVariable s =
  case break (=='=') s of
    ([], []) -> Left "Empty variable"
    ([], _) -> Left "Nothing on the left side of the = symbol"
    (_, []) -> Left "Nothing on the right side of the = symbol"
    (var, '=':val) -> Right (T.pack var, T.pack val)
    _ -> Left "???"

configParser :: Parser Config
configParser = Config <$> strOption (long "config" <> short 'c' <> metavar "CONFIG" <> value "hiera.yaml")
                      <*> strOption (long "query" <> short 'q' <> metavar "QUERY")
                      <*> option (maybeReader readQT) (long "querytype" <> short 't' <> metavar "QUERYTYPE" <> value QFirst <> help "values: first (default), unique, hash")
                      <*> many (argument (eitherReader parseVariable) (metavar "VARIABLE" <> help "Variables, in the form key=value"))

configInfo :: ParserInfo Config
configInfo = info (configParser <**> helper) mempty

main :: IO ()
main = do
  Config fp query qtype vars <- execParser configInfo
  ehiera <- startHiera fp
  case ehiera of
    Left rr -> error rr
    Right hiera -> do
      r <- hiera (HM.fromList vars) (T.pack query) qtype
      case r of
        S.Left rr -> error (show rr)
        S.Right Nothing -> putStrLn "no match"
        S.Right (Just res) -> print (pretty res)<commit_msg>Use startHiera' instead of startHiera in yera client<commit_after>{-# LANGUAGE LambdaCase #-}
module Main (main) where

import           Options.Applicative
import           Data.Monoid
import qualified Data.Text as T
import qualified Data.Either.Strict as S
import qualified Data.HashMap.Strict as HM
import           Text.PrettyPrint.ANSI.Leijen (pretty)
import System.Exit

import Puppet.Interpreter.Types
import Puppet.Interpreter.PrettyPrinter()
import Hiera.Server

data Config
  = Config
  { _filepath    :: FilePath
  , _query       :: String
  , _queryType   :: HieraQueryType
  , _variables   :: [(T.Text,T.Text)]
  }

readQT :: String -> Maybe HieraQueryType
readQT s
  = case s of
    "first"  -> Just QFirst
    "unique" -> Just QUnique
    "hash"   -> Just QHash
    _        -> Nothing

parseVariable :: String -> Either String (T.Text, T.Text)
parseVariable s =
  case break (=='=') s of
    ([], []) -> Left "Empty variable"
    ([], _) -> Left "Nothing on the left side of the = symbol"
    (_, []) -> Left "Nothing on the right side of the = symbol"
    (var, '=':val) -> Right (T.pack var, T.pack val)
    _ -> Left "???"

configParser :: Parser Config
configParser = Config <$> strOption (long "config" <> short 'c' <> metavar "CONFIG" <> value "hiera.yaml")
                      <*> strOption (long "query" <> short 'q' <> metavar "QUERY")
                      <*> option (maybeReader readQT) (long "querytype" <> short 't' <> metavar "QUERYTYPE" <> value QFirst <> help "values: first (default), unique, hash")
                      <*> many (argument (eitherReader parseVariable) (metavar "VARIABLE" <> help "Variables, in the form key=value"))

configInfo :: ParserInfo Config
configInfo = info (configParser <**> helper) mempty

main :: IO ()
main = do
  Config fp query qtype vars <- execParser configInfo
  hiera <- startHiera' fp
  hiera (HM.fromList vars) (T.pack query) qtype >>= \case
    S.Left rr -> error (show rr)
    S.Right Nothing -> putStrLn "no match" >> exitFailure
    S.Right (Just res) -> print (pretty res)
{'fraction_non_alphanumeric': 0.13051963893632593, 'fraction_numerical': 0.0, 'mean_word_length': 2.8104089219330857, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1592748', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1282, 'n_words': 421}