Skip to content

Commit

Permalink
Merge pull request #16 from Nike-Inc/refactor/clean-up
Browse files Browse the repository at this point in the history
Refactor/clean up
  • Loading branch information
dogonthehorizon authored Nov 23, 2016
2 parents e3f0d44 + d54dfb2 commit 5adcaf8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
49 changes: 28 additions & 21 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ module Main where

import Prelude hiding (putStr)

import Bartlett.Types
import qualified Bartlett.Configuration as C
import qualified Bartlett.Actions.Info as AI
import qualified Bartlett.Actions.Build as AB
import Bartlett.Types
import Bartlett.Parsers (parseOptions, withInfo)
import qualified Bartlett.Configuration as C
import qualified Bartlett.Actions.Info as AI
import qualified Bartlett.Actions.Build as AB
import qualified Bartlett.Actions.Config as AC
import qualified Bartlett.Parsers as P

import Control.Exception
import Data.ByteString.Lazy.Char8 hiding (foldl)
import Control.Exception (bracket_)
import Data.ByteString.Lazy.Char8 (ByteString, pack, unpack, hPutStr)
import Data.Maybe (fromMaybe)
import Data.Monoid
import Options.Applicative
import System.IO hiding (putStr, hPutStrLn, hPutStr)
import System.Exit (die)
import System.IO (hFlush, stdout, stdin, stderr, hSetEcho, hGetEcho, hPutChar)
import qualified System.Keyring as SK

-- | Wrapper determining if the given action should be echoed to stdout.
Expand Down Expand Up @@ -62,6 +62,14 @@ selectPassword shouldStorePassword profile usr = do
else
return pwd

-- | Bind an option, but if it is not provided exit the program.
bindOption :: Maybe a -> Maybe ByteString -> IO a
bindOption a failMessage =
case a of
Nothing ->
die . unpack . fromMaybe "" $ failMessage
Just a ->
return a

-- | Execute the given command with the given username and jenkins instance.
executeCommand :: Command -> User -> JenkinsInstance -> IO ()
Expand All @@ -82,21 +90,20 @@ executeCommand cmd usr jenkinsInstance =
run :: Options -> IO ()
run (Options username jenkinsInstance profile cmd) = do
let profileName = fromMaybe "default" profile
cfg <- C.getConfiguration profileName
cfg <- C.getConfiguration profileName

cfgJenkins <- C.getJenkinsInstance cfg
jenkins <- bindOption (jenkinsInstance <|> cfgJenkins)
(Just "Could not determine the Jenkins instance to use.")

cfgUser <- C.getUsername cfg
usr <- bindOption (username <|> cfgUser)
(Just "Could not determine username to use.")

shouldStorePassword <- fromMaybe False <$> C.getStorePassword cfg
pwd <- selectPassword shouldStorePassword profileName usr

case jenkinsInstance <|> cfgJenkins of
Nothing ->
hPutStrLn stderr "Could not determine the Jenkins instance to use."
Just inst ->
case username <|> cfgUser of
Nothing ->
hPutStrLn stderr "Could not determine username to use."
Just usr -> do
pwd <- selectPassword shouldStorePassword profileName usr
executeCommand cmd (User usr pwd) inst
executeCommand cmd (User usr pwd) jenkins

main :: IO ()
main = run =<< execParser (P.parseOptions `P.withInfo` "")
main = run =<< execParser (parseOptions `withInfo` "")
2 changes: 1 addition & 1 deletion bartlett.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: bartlett
version: 1.1.2
version: 1.1.3
synopsis: The Jenkins command-line tool to serve your needs.
description: Please see README.md
homepage: https://github.com/Nike-inc/bartlett
Expand Down
3 changes: 1 addition & 2 deletions src/Bartlett/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ execRequest requestType opts reqUrl postBody =
case requestType of
-- TODO Need to get a CSRF crumb
-- JENKINS_URL/crumbIssuer/api/json?xpath=?xpath=concat(//crumbRequestField,":",//crumb)')
-- TODO create a proper sum type for requestType
Post ->
postSession reqUrl
`E.catch`
Expand All @@ -57,7 +56,7 @@ execRequest requestType opts reqUrl postBody =

-- | Handler that returns a JSON representation of the error status.
simpleErrorHandler :: NHC.HttpException -> IO a
simpleErrorHandler e@(NHC.StatusCodeException status _ _) =
simpleErrorHandler (NHC.StatusCodeException status _ _) =
die . unpack . encodePretty . toResponseStatus $ status

-- | Attempt to recover from non-fatal errors with the provided action, otherwise
Expand Down

0 comments on commit 5adcaf8

Please sign in to comment.