blob: 4336b2994782117c855d455ed7ed20d69194d0a5 (
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
module Command.Extract (
ExtractCommand,
) where
import Control.Monad
import Control.Monad.Except
import Control.Monad.IO.Class
import Data.Text qualified as T
import System.Console.GetOpt
import System.Directory
import System.FilePath
import Command
import Eval
import Job.Types
data ExtractCommand = ExtractCommand ExtractOptions ExtractArguments
data ExtractArguments = ExtractArguments
{ extractArtifacts :: [ ( JobRef, ArtifactName ) ]
, extractDestination :: FilePath
}
instance CommandArgumentsType ExtractArguments where
argsFromStrings = \case
args@(_:_:_) -> do
extractArtifacts <- mapM toArtifactRef (init args)
extractDestination <- return (last args)
return ExtractArguments {..}
where
toArtifactRef tref = case T.breakOnEnd "." (T.pack tref) of
(jobref', aref) | Just ( jobref, '.' ) <- T.unsnoc jobref'
-> return ( parseJobRef jobref, ArtifactName aref )
_ -> throwError $ "too few parts in artifact ref ‘" <> tref <> "’"
_ -> throwError "too few arguments"
data ExtractOptions = ExtractOptions
{ extractForce :: Bool
}
instance Command ExtractCommand where
commandName _ = "extract"
commandDescription _ = "Extract artifacts generated by jobs"
type CommandArguments ExtractCommand = ExtractArguments
commandUsage _ = T.pack $ unlines $
[ "Usage: minici jobid [<option>...] <job ref>.<artifact>... <destination>"
]
type CommandOptions ExtractCommand = ExtractOptions
defaultCommandOptions _ = ExtractOptions
{ extractForce = False
}
commandOptions _ =
[ Option [ 'f' ] [ "force" ]
(NoArg $ \opts -> opts { extractForce = True })
"owerwrite existing files"
]
commandInit _ = ExtractCommand
commandExec = cmdExtract
cmdExtract :: ExtractCommand -> CommandExec ()
cmdExtract (ExtractCommand ExtractOptions {..} ExtractArguments {..}) = do
einput <- getEvalInput
storageDir <- getStorageDir
isdir <- liftIO (doesDirectoryExist extractDestination) >>= \case
True -> return True
False -> case extractArtifacts of
_:_:_ -> tfail $ "destination ‘" <> T.pack extractDestination <> "’ is not a directory"
_ -> return False
forM_ extractArtifacts $ \( ref, ArtifactName aname ) -> do
jid@(JobId ids) <- either (tfail . textEvalError) return =<<
liftIO (runEval (evalJobReference ref) einput)
let jdir = joinPath $ (storageDir :) $ ("jobs" :) $ map (T.unpack . textJobIdPart) ids
adir = jdir </> "artifacts" </> T.unpack aname
liftIO (doesDirectoryExist jdir) >>= \case
True -> return ()
False -> tfail $ "job ‘" <> textJobId jid <> "’ not yet executed"
liftIO (doesDirectoryExist adir) >>= \case
True -> return ()
False -> tfail $ "artifact ‘" <> aname <> "’ of job ‘" <> textJobId jid <> "’ not found"
afile <- liftIO (listDirectory adir) >>= \case
[ file ] -> return file
[] -> tfail $ "artifact ‘" <> aname <> "’ of job ‘" <> textJobId jid <> "’ not found"
_:_:_ -> tfail $ "unexpected files in ‘" <> T.pack adir <> "’"
let tpath | isdir = extractDestination </> afile
| otherwise = extractDestination
when (not extractForce) $ do
liftIO (doesPathExist tpath) >>= \case
True -> tfail $ "destination ‘" <> T.pack tpath <> "’ already exists"
False -> return ()
liftIO $ copyFile (adir </> afile) tpath
|