Skip to content

Commit

Permalink
Merge pull request #1 from vst/vst/cli-initial-functionality
Browse files Browse the repository at this point in the history
Initial Codebase and Functionality
  • Loading branch information
vst authored Mar 17, 2024
2 parents ad490d4 + ca8e2bd commit a35c126
Show file tree
Hide file tree
Showing 13 changed files with 959 additions and 32 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "Release"

on:
push:
branches:
- "main"

permissions:
contents: "write"
pull-requests: "write"

jobs:
build:
runs-on: "ubuntu-22.04"

steps:
- name: "Release"
uses: "google-github-actions/release-please-action@v4"
5 changes: 5 additions & 0 deletions .hlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
- extensions:
- default: false # All extension are banned by default.
- name:
- DeriveGeneric
- DerivingVia
- FlexibleContexts
- OverloadedStrings
- TemplateHaskell
- TypeApplications

################
# CUSTOM RULES #
Expand Down
3 changes: 3 additions & 0 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
".": "0.0.0"
}
12 changes: 12 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,20 @@ library:
- -Werror
- -Wunused-packages
dependencies:
- aeson
- aeson-combinators
- autodocodec
- autodocodec-schema
- bytestring
- file-embed
- monad-parallel
- mtl
- optparse-applicative
- path
- scientific
- text
- time
- typed-process

executables:
lhp:
Expand Down
18 changes: 18 additions & 0 deletions release-please-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json",
"packages": {
".": {
"release-type": "simple",
"changelog-path": "CHANGELOG.md",
"include-v-in-tag": true,
"bump-minor-pre-major": true,
"bump-patch-for-minor-pre-major": true,
"draft": false,
"prerelease": false,
"initial-version": "0.0.1",
"extra-files": [
"package.yaml"
]
}
}
}
77 changes: 45 additions & 32 deletions src/Lhp/Cli.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

-- | This module provides top-level definitions for the CLI program.
module Lhp.Cli where

import Control.Applicative ((<**>), (<|>))
import qualified Autodocodec.Schema as ADC.Schema
import Control.Applicative ((<**>))
import Control.Monad (join)
import Control.Monad.Except (runExceptT)
import qualified Control.Monad.Parallel as MP
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy.Char8 as BLC
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import qualified Lhp.Meta as Meta
import Lhp.Remote (compileReport)
import Lhp.Types (Report)
import Options.Applicative ((<|>))
import qualified Options.Applicative as OA
import System.Exit (ExitCode (..))
import System.IO (hPutStrLn, stderr)


-- * Entrypoint
Expand All @@ -34,51 +43,55 @@ cli =
-- | Option parser for top-level commands.
optProgram :: OA.Parser (IO ExitCode)
optProgram =
commandGreet
<|> commandFarewell
commandCompile
<|> commandSchema


-- * Commands


-- ** greet
-- ** compile


-- | Definition for @greet@ CLI command.
commandGreet :: OA.Parser (IO ExitCode)
commandGreet = OA.hsubparser (OA.command "greet" (OA.info parser infomod) <> OA.metavar "greet")
-- | Definition for @compile@ CLI command.
commandCompile :: OA.Parser (IO ExitCode)
commandCompile = OA.hsubparser (OA.command "compile" (OA.info parser infomod) <> OA.metavar "compile")
where
infomod = OA.fullDesc <> infoModHeader <> OA.progDesc "Greet user." <> OA.footer "This command prints a greeting message to the console."
infomod = OA.fullDesc <> infoModHeader <> OA.progDesc "Compile remote host information." <> OA.footer "This command fetches and compiles remote host information."
parser =
doGreet
<$> OA.strOption (OA.short 'n' <> OA.long "name" <> OA.value "World" <> OA.showDefault <> OA.help "Whom to greet.")


-- | @greet@ CLI command program.
doGreet :: T.Text -> IO ExitCode
doGreet n = do
TIO.putStrLn ("Hello " <> n <> "!")
doCompile
<$> OA.many (OA.strOption (OA.short 'h' <> OA.long "host" <> OA.help "Remote host (in SSH destination format)."))
<*> OA.switch (OA.short 's' <> OA.long "stream" <> OA.help "Streaming results.")


-- | @compile@ CLI command program.
doCompile :: [T.Text] -> Bool -> IO ExitCode
doCompile hosts False = do
res <- runExceptT (MP.mapM compileReport hosts)
case res of
Left err -> BLC.hPutStrLn stderr (Aeson.encode err) >> pure (ExitFailure 1)
Right sr -> BLC.putStrLn (Aeson.encode sr) >> pure ExitSuccess
doCompile hosts True = do
mapM_ go hosts
pure ExitSuccess
where
go h = do
hPutStrLn stderr ("Patrolling " <> T.unpack h)
res <- runExceptT (compileReport h)
case res of
Left err -> BLC.hPutStrLn stderr (Aeson.encode err)
Right sr -> BLC.putStrLn (Aeson.encode sr)


-- ** farewell
-- ** schema


-- | Definition for @farewell@ CLI command.
commandFarewell :: OA.Parser (IO ExitCode)
commandFarewell = OA.hsubparser (OA.command "farewell" (OA.info parser infomod) <> OA.metavar "farewell")
-- | Definition for @schema@ CLI command.
commandSchema :: OA.Parser (IO ExitCode)
commandSchema = OA.hsubparser (OA.command "schema" (OA.info parser infomod) <> OA.metavar "schema")
where
infomod = OA.fullDesc <> infoModHeader <> OA.progDesc "Say farewell to user." <> OA.footer "This command prints a farewell message to the console."
parser =
doFarewell
<$> OA.strOption (OA.short 'n' <> OA.long "name" <> OA.value "World" <> OA.showDefault <> OA.help "Whom to say farewell to.")


-- | @farewell@ CLI command program.
doFarewell :: T.Text -> IO ExitCode
doFarewell n = do
TIO.putStrLn ("Thanks for all the fish, " <> n <> "!")
pure ExitSuccess
infomod = OA.fullDesc <> infoModHeader <> OA.progDesc "Produce JSON schema for report." <> OA.footer "This command produces JSON schema for report data type."
parser = pure (BLC.putStrLn (Aeson.encode (ADC.Schema.jsonSchemaViaCodec @Report)) >> pure ExitSuccess)


-- * Helpers
Expand Down
Loading

0 comments on commit a35c126

Please sign in to comment.