Skip to content

Commit

Permalink
Simplify main using Applicative Maybe's alternative operator
Browse files Browse the repository at this point in the history
  • Loading branch information
dogonthehorizon committed Nov 22, 2016
1 parent cbb4aa9 commit 38333f0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
22 changes: 7 additions & 15 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ requestPassword = do
hPutChar stderr '\n'
return $ pack s

-- | Given a list of possible values, choose the leftmost non-Nothing value.
chooseValue :: [Maybe a] -> Maybe a
chooseValue = getFirst . foldl mappend (First Nothing) . fmap First

-- | Construct a namespaced service name for the OSX Keychain service.
keychainService :: Profile -> String
keychainService = unpack . mappend "bartlett."
Expand Down Expand Up @@ -86,23 +82,19 @@ executeCommand cmd usr jenkinsInstance =
run :: Options -> IO ()
run (Options username jenkinsInstance profile cmd) = do
let profileName = fromMaybe "default" profile
cfg <- C.getConfiguration profileName

-- TODO this is all very messy, surely there should be some way
-- to simplify it.

cfg <- C.getConfiguration profileName
cfgJenkins <- C.getJenkinsInstance cfg
case chooseValue [jenkinsInstance, cfgJenkins] of
cfgUser <- C.getUsername cfg
shouldStorePassword <- fromMaybe False <$> C.getStorePassword cfg

case jenkinsInstance <|> cfgJenkins of
Nothing ->
hPutStrLn stderr "Could not determine the Jenkins instance to use."
Just inst -> do

cfgUser <- C.getUsername cfg
case chooseValue [username, cfgUser] of
Just inst ->
case username <|> cfgUser of
Nothing ->
hPutStrLn stderr "Could not determine username to use."
Just usr -> do
shouldStorePassword <- fromMaybe False <$> C.getStorePassword cfg
pwd <- selectPassword shouldStorePassword profileName usr
executeCommand cmd (User usr pwd) inst

Expand Down
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.1
version: 1.1.2
synopsis: The Jenkins command-line tool to serve your needs.
description: Please see README.md
homepage: https://github.com/Nike-inc/bartlett
Expand Down

0 comments on commit 38333f0

Please sign in to comment.