summaryrefslogtreecommitdiff
path: root/src/Command/JobId.hs
blob: 173f54344dee1fc234029b20abf9b25450ec0b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module Command.JobId (
    JobIdCommand,
) where

import Control.Monad
import Control.Monad.IO.Class

import Data.Text (Text)
import Data.Text qualified as T

import System.Console.GetOpt

import Command
import Eval
import Job.Types
import Output
import Repo


data JobIdCommand = JobIdCommand JobIdOptions JobRef

data JobIdOptions = JobIdOptions
    { joVerbose :: Bool
    }

instance Command JobIdCommand where
    commandName _ = "jobid"
    commandDescription _ = "Resolve job reference to canonical job ID"

    type CommandArguments JobIdCommand = Text

    commandUsage _ = T.pack $ unlines $
        [ "Usage: minici jobid [<option>...] <job ref>"
        ]

    type CommandOptions JobIdCommand = JobIdOptions
    defaultCommandOptions _ = JobIdOptions
        { joVerbose = False
        }

    commandOptions _ =
        [ Option [ 'v' ] [ "verbose" ]
            (NoArg $ \opts -> opts { joVerbose = True })
            "show detals of the ID"
        ]

    commandInit _ opts = JobIdCommand opts . parseJobRef
    commandExec = cmdJobId


cmdJobId :: JobIdCommand -> CommandExec ()
cmdJobId (JobIdCommand JobIdOptions {..} ref) = do
    einput <- getEvalInput
    out <- getOutput
    JobId ids <- either (tfail . textEvalError) (return . jobId) =<<
        liftIO (runEval (evalJobReference ref) einput)

    outputMessage out $ textJobId $ JobId ids
    when joVerbose $ do
        outputMessage out ""
        forM_ ids $ \case
            JobIdName name -> outputMessage out $ textJobName name <> " (job name)"
            JobIdCommit mbrepo cid -> outputMessage out $ T.concat
                [ textCommitId cid, " (commit"
                , maybe "" (\name -> " from ‘" <> textRepoName name <> "’ repo") mbrepo
                , ")"
                ]
            JobIdTree mbrepo subtree cid -> outputMessage out $ T.concat
                [ textTreeId cid, " (tree"
                , maybe "" (\name -> " from ‘" <> textRepoName name <> "’ repo") mbrepo
                , if not (null subtree) then ", subtree ‘" <> T.pack subtree <> "’" else ""
                , ")"
                ]