From d7f7c030f919983b1abeaf8bb7ed9be4f06c1c10 Mon Sep 17 00:00:00 2001 From: Sam Tay Date: Wed, 14 Dec 2016 18:29:36 -0500 Subject: [PATCH] Stubbing out dmc cli --- TODO.md | 8 +++-- app/Dmc.hs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ dockmaster.cabal | 11 +++++++ 3 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 app/Dmc.hs diff --git a/TODO.md b/TODO.md index c45ca64..98be143 100644 --- a/TODO.md +++ b/TODO.md @@ -1,9 +1,11 @@ # todo ## mvp -1. DMC +1. `dmc` +2. Follow up on [#234](https://github.com/pcapriotti/optparse-applicative/pull/234) to remove '--' +3. MORE TESTS for dm/dmc runtime ## later -0. --local flag to execute on local machine instead of docker-machine targets -1. Use monad transformer to stack Sh on top of Either (error transformer) +0. `--local` flag to execute on local machine instead of docker-machine targets +1. Use monad transformer to stack `Sh` on top of `Either` (error transformer) 2. Read up on exception handling link in haskell-fun-times resources diff --git a/app/Dmc.hs b/app/Dmc.hs new file mode 100644 index 0000000..d86841b --- /dev/null +++ b/app/Dmc.hs @@ -0,0 +1,82 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ExtendedDefaultRules #-} +{-# OPTIONS_GHC -fno-warn-type-defaults #-} +module Main where + +-- Local packages +import Dockmaster + +-- External packages +import Shelly hiding (command) +import Options.Applicative + +-- Base packages +import Prelude hiding (FilePath) +import Data.Monoid ((<>)) +import qualified Data.Text as T + +default (T.Text) + +-- | Represents cli options/arguments +data Dmc + = Set SetOptions + | Get GetOptions + | Unshift SetOptions + | Shift GetOptions + | Push SetOptions + | Pop GetOptions + deriving (Eq, Show) + +-- | Arguments necessary for setting value +data SetOptions = SetOptions + { sName :: String + , sValue :: String + } deriving (Eq, Show) + +-- | Arguments necessary for getting value or shifting/popping array +data GetOptions = GetOptions + { gName :: String + } deriving (Eq, Show) + +-- | Main runtime +main :: IO () +main = execParser opts >>= shelly . runtime + +-- | Runtime shell execution +runtime :: Dmc -> Sh () +runtime opts = undefined + +-- | Parser for /set/ commands +setOptions :: Parser SetOptions +setOptions = SetOptions + <$> argument str (metavar "NAME" <> help "Name of the setting to modify") + <*> argument str (metavar "VALUE" <> help "Value to add/set") + +-- | Parser for /get/ commands +getOptions :: Parser GetOptions +getOptions = GetOptions + <$> argument str (metavar "NAME" <> help "Name of the setting to retrieve") + +-- | Parser for 'Dmc'. +parser :: Parser Dmc +parser = subparser + ( (command "set" $ commandInfo (Set <$> setOptions) "Set value") + <> (command "get" $ commandInfo (Get <$> getOptions) "Get value") + <> (command "unshift" $ commandInfo (Unshift <$> setOptions) "Unshift value (for arrays)") + <> (command "shift" $ commandInfo (Shift <$> getOptions) "Shift value (for arrays)") + <> (command "push" $ commandInfo (Push <$> setOptions) "Push value (for arrays)") + <> (command "pop" $ commandInfo (Pop <$> getOptions) "Pop value (for arrays)") + ) + +-- | Generate 'ParserInfo' for 'Dmc'. +opts :: ParserInfo Dmc +opts = info (helper <*> parser) + ( fullDesc + <> header "dmc - dockmaster configuration modifiers" + ) + +-- | Helper to generate subcommand parser info +commandInfo :: Parser Dmc -> String -> ParserInfo Dmc +commandInfo opts desc = info + (helper <*> opts) + (fullDesc <> progDesc desc) diff --git a/dockmaster.cabal b/dockmaster.cabal index 0b581be..cc1b9f6 100644 --- a/dockmaster.cabal +++ b/dockmaster.cabal @@ -49,6 +49,17 @@ executable dm , optparse-applicative default-language: Haskell2010 +executable dmc + hs-source-dirs: app + main-is: Dmc.hs + ghc-options: -threaded -rtsopts -with-rtsopts=-N + build-depends: base + , dockmaster + , shelly + , text + , optparse-applicative + default-language: Haskell2010 + test-suite dockmaster-test type: exitcode-stdio-1.0 hs-source-dirs: test