From fd648c279ec9d57d5a8c71ef43bdffe70a48aca4 Mon Sep 17 00:00:00 2001 From: "Jaress, Brian (ETW - FLEX Resource)" Date: Tue, 6 Dec 2016 10:46:33 -0800 Subject: [PATCH] Make username and password optional. --- app/Main.hs | 17 +++++++++++------ src/Bartlett/Actions/Build.hs | 13 +++++++------ src/Bartlett/Actions/Config.hs | 11 ++++++----- src/Bartlett/Actions/Info.hs | 11 ++++++----- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 1a9420e..9172fc4 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -72,7 +72,7 @@ bindOption a failMessage = return a -- | Execute the given command with the given username and jenkins instance. -executeCommand :: Command -> User -> JenkinsInstance -> IO () +executeCommand :: Command -> Maybe User -> JenkinsInstance -> IO () executeCommand cmd usr jenkinsInstance = case cmd of Info jobPaths -> @@ -96,14 +96,19 @@ run (Options username jenkinsInstance profile cmd) = do jenkins <- bindOption (jenkinsInstance <|> cfgJenkins) (Just "Could not determine the Jenkins instance to use.") + shouldStorePassword <- fromMaybe False <$> C.getStorePassword cfg cfgUser <- C.getUsername cfg - usr <- bindOption (username <|> cfgUser) - (Just "Could not determine username to use.") + usr <- userWithPassword (username <|> cfgUser) + (selectPassword shouldStorePassword profileName) - shouldStorePassword <- fromMaybe False <$> C.getStorePassword cfg - pwd <- selectPassword shouldStorePassword profileName usr + executeCommand cmd usr jenkins - executeCommand cmd (User usr pwd) jenkins +-- There is probably a better way to do this +userWithPassword :: Maybe Username -> (Username -> IO Password) -> IO (Maybe User) +userWithPassword Nothing _ = return Nothing +userWithPassword (Just username) getPwd = do + password <- getPwd username + return $ Just (User username password) main :: IO () main = run =<< execParser (parseOptions `withInfo` "") diff --git a/src/Bartlett/Actions/Build.hs b/src/Bartlett/Actions/Build.hs index d0b5938..c03c429 100644 --- a/src/Bartlett/Actions/Build.hs +++ b/src/Bartlett/Actions/Build.hs @@ -18,7 +18,8 @@ import Bartlett.Network (execRequest) import Bartlett.Types import qualified Bartlett.Util as BU -import Control.Lens ((?~), (^.), (&)) +import Control.Lens (set, (^.), (&)) +import Data.Maybe (Maybe) import Data.Aeson.Encode.Pretty (encodePretty) import qualified Data.ByteString.Lazy.Char8 as BL import Network.Wreq (Options, responseStatus, auth) @@ -34,15 +35,15 @@ consBuildType (Just jobParameters) = -- | Trigger a build for the given job with optional build parameters. postBuild :: - BasicAuthUser b => b -- ^ The user to authenticate with. - -> JenkinsInstance -- ^ The Jenkins instance to make requests against. - -> JobPath -- ^ The job to trigger a build against. - -> Maybe JobParameters -- ^ Optional set of job parameters to trigger with. + BasicAuthUser b => Maybe b -- ^ The user to authenticate with. + -> JenkinsInstance -- ^ The Jenkins instance to make requests against. + -> JobPath -- ^ The job to trigger a build against. + -> Maybe JobParameters -- ^ Optional set of job parameters to trigger with. -> IO () postBuild user base path parameters = do resp <- execRequest Post reqOpts reqUri Nothing BL.putStrLn . encodePretty . BU.toResponseStatus $ resp ^. responseStatus where (suffix, buildOpts) = consBuildType parameters - reqOpts = buildOpts & auth ?~ getBasicAuth user + reqOpts = buildOpts & set auth (getBasicAuth <$> user) reqUri = BU.mkUrl base path suffix diff --git a/src/Bartlett/Actions/Config.hs b/src/Bartlett/Actions/Config.hs index eeae355..0598f5f 100644 --- a/src/Bartlett/Actions/Config.hs +++ b/src/Bartlett/Actions/Config.hs @@ -14,7 +14,8 @@ import Bartlett.Network (execRequest) import Bartlett.Types import Bartlett.Util (toResponseStatus, mkUrl) -import Control.Lens ((^.), (?~), (&)) +import Control.Lens (set, (^.), (&)) +import Data.Maybe (Maybe) import Data.Aeson.Encode.Pretty (encodePretty) import qualified Data.ByteString.Lazy.Char8 as BL import Network.Wreq (responseStatus, responseBody, defaults, auth) @@ -26,18 +27,18 @@ configUri base path = -- | Retrieve the XML configuration for the given job. getConfig :: BasicAuthUser a => - a -- The user to authenticate with. + Maybe a -- The user to authenticate with. -> JenkinsInstance -- The Jenkins instance to interact with. -> JobPath -- The Job for the given Jenkins instance to interact with. -> IO () -- The XML configuration for the given job. getConfig user base path = do resp <- execRequest Get reqOpts (configUri base path) Nothing BL.putStrLn $ resp ^. responseBody - where reqOpts = defaults & auth ?~ getBasicAuth user + where reqOpts = defaults & set auth (getBasicAuth <$> user) -- | Update the XML configuration for the given job. updateConfig :: BasicAuthUser a => - a -- The user to authenticate with. + Maybe a -- The user to authenticate with. -> JenkinsInstance -- The Jenkins instance to interact with. -> JobPath -- The Job for the given Jenkins instance to interact with. -> ConfigPath -- Path to the XML configuration to upload to Jenkins. @@ -46,4 +47,4 @@ updateConfig user base path configPath = do configFile <- BL.readFile configPath resp <- execRequest Post reqOpts (configUri base path) (Just configFile) BL.putStrLn . encodePretty . toResponseStatus $ resp ^. responseStatus - where reqOpts = defaults & auth ?~ getBasicAuth user + where reqOpts = defaults & set auth (getBasicAuth <$> user) diff --git a/src/Bartlett/Actions/Info.hs b/src/Bartlett/Actions/Info.hs index 0b70db6..6012af2 100644 --- a/src/Bartlett/Actions/Info.hs +++ b/src/Bartlett/Actions/Info.hs @@ -16,7 +16,8 @@ import Bartlett.Network (execRequest) import Bartlett.Types import Bartlett.Util (toPrettyJson, mkUrl) -import Control.Lens ((^.), (?~), (&)) +import Control.Lens (set, (^.), (&)) +import Data.Maybe (Maybe) import qualified Data.ByteString.Lazy.Char8 as BL import Network.Wreq (responseBody, defaults, auth) @@ -27,14 +28,14 @@ import Network.Wreq (responseBody, defaults, auth) -- 'JenkinsInstance'. If not protocol is specified it will attempt to contact -- Jenkins over SSL. getInfo :: - BasicAuthUser b => b -- ^ The user to authenticate with. - -> JenkinsInstance -- ^ The Jenkins instance to authenticate against. - -> [JobPath] -- ^ The jobs to get information from. + BasicAuthUser b => Maybe b -- ^ The user to authenticate with. + -> JenkinsInstance -- ^ The Jenkins instance to authenticate against. + -> [JobPath] -- ^ The jobs to get information from. -> IO () getInfo user base [] = return () getInfo user base (path:paths) = do resp <- execRequest Get reqOpts reqUri Nothing BL.putStrLn . toPrettyJson $ resp ^. responseBody getInfo user base paths - where reqOpts = defaults & auth ?~ getBasicAuth user + where reqOpts = defaults & set auth (getBasicAuth <$> user) reqUri = mkUrl base path "/api/json"