From 0ba31ca7427e9e343bc5269027226a5d68a3b9b3 Mon Sep 17 00:00:00 2001 From: Sam Tay Date: Fri, 2 Dec 2016 16:55:34 -0500 Subject: [PATCH] Stubbing out dockmaster funcs/types --- dockmaster.cabal | 4 ++++ src/Dockmaster.hs | 47 +++++++++++++++++++++++++++++++++++++++ src/Dockmaster/Locator.hs | 33 +++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 src/Dockmaster.hs create mode 100644 src/Dockmaster/Locator.hs diff --git a/dockmaster.cabal b/dockmaster.cabal index 0262b8d..46672e1 100644 --- a/dockmaster.cabal +++ b/dockmaster.cabal @@ -20,10 +20,14 @@ description: library hs-source-dirs: src exposed-modules: Dockmaster.Types + , Dockmaster + other-modules: Dockmaster.Locator build-depends: base >= 4.7 && < 5 , yaml , text , unordered-containers + , shelly + , text default-language: Haskell2010 executable dockmaster-exe diff --git a/src/Dockmaster.hs b/src/Dockmaster.hs new file mode 100644 index 0000000..85c20f1 --- /dev/null +++ b/src/Dockmaster.hs @@ -0,0 +1,47 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ExtendedDefaultRules #-} +{-# OPTIONS_GHC -fno-warn-type-defaults #-} +module Dockmaster + ( dm + ) where + +-- Local modules +import Dockmaster.Locator + +-- External modules +import Shelly +import Prelude hiding (FilePath) +import qualified Data.Text as T +default (T.Text) + +-- | Possibly provide sum type data = DcUp | DcDown | ... with to string method +type DCCommand = T.Text + +-- | Runs docker-compose commands against resolved composition locations +-- See usage docs for more info. Tries to find a dockmaster.yml file based on +-- the initial path argument +dm :: FilePath -> DCCommand -> [T.Text] -> Sh (T.Text) +dm path command args = do + gwd <- getWorkDir path + case gwd of + Left err -> do + echo_n_err err + return err -- maybe dm should return Either and executable handles printing? + Right wd -> sub $ do + cd wd + dockermachine -- TODO handle Left errors + hookWrap command $ dockercompose $ command : args + +dockercompose :: [T.Text] -> Sh T.Text +dockercompose = run "docker-compose" + +-- | Executes specific dc command pre/post hooks around action argument +-- (action arg is typically docker-compose command) +hookWrap :: DCCommand -> Sh a -> Sh a +hookWrap = undefined + +-- | Reads from dockmaster.yml to find machine, attempts to connect +-- dm should HALT when machine is unreachable +dockermachine :: Sh (Either T.Text ()) +dockermachine = undefined + diff --git a/src/Dockmaster/Locator.hs b/src/Dockmaster/Locator.hs new file mode 100644 index 0000000..4efed8c --- /dev/null +++ b/src/Dockmaster/Locator.hs @@ -0,0 +1,33 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ExtendedDefaultRules #-} +{-# OPTIONS_GHC -fno-warn-type-defaults #-} +module Dockmaster.Locator + ( workDirExec + , getWorkDir + , cdWorkDir + ) where + +import Shelly +import Prelude hiding (FilePath) +import qualified Data.Text as T +default (T.Text) + +-- | Execute action in workdir (does not affect cwd outside of action) +workDirExec :: FilePath -> Sh a -> Sh (Either T.Text a) +workDirExec = undefined + +-- | Resolve the appropriate docker-compose workdir (based on path arg) +-- TODO This needs error handling when dockmaster.yml not found +getWorkDir :: FilePath -> Sh (Either T.Text FilePath) +getWorkDir p + -- search for dockmaster.yml + -- aggregate searched dirs for verbosity msg ? + | p /= "." = do return $ Left "dockmaster.yml file not found" + | otherwise = do + wd <- pwd + return $ Right wd + +-- | Cd into workdir +-- TODO check if cd retains context when run externally (i.e. do I need to pass action here) +cdWorkDir :: FilePath -> Sh (Either T.Text (Sh ())) +cdWorkDir = undefined