fromkey --json
* fromkey: Added --json. * fromkey --batch output changed to support using it with --json. The old output was not parseable for any useful information, so this is not expected to break anything.
This commit is contained in:
parent
7b46b43c48
commit
b080699a95
5 changed files with 61 additions and 37 deletions
|
@ -12,6 +12,10 @@ git-annex (7.20190130) UNRELEASED; urgency=medium
|
||||||
* Fix race in cleanup of othertmp directory that could result in a failure
|
* Fix race in cleanup of othertmp directory that could result in a failure
|
||||||
attempting to access it.
|
attempting to access it.
|
||||||
* fromkey: Made idempotent.
|
* fromkey: Made idempotent.
|
||||||
|
* fromkey: Added --json.
|
||||||
|
* fromkey --batch output changed to support using it with --json.
|
||||||
|
The old output was not parseable for any useful information, so
|
||||||
|
this is not expected to break anything.
|
||||||
|
|
||||||
-- Joey Hess <id@joeyh.name> Wed, 30 Jan 2019 12:30:22 -0400
|
-- Joey Hess <id@joeyh.name> Wed, 30 Jan 2019 12:30:22 -0400
|
||||||
|
|
||||||
|
|
|
@ -93,15 +93,15 @@ batchCommandAction a = maybe (batchBadInput (Batch BatchLine)) (const noop)
|
||||||
-- to handle them.
|
-- to handle them.
|
||||||
--
|
--
|
||||||
-- File matching options are not checked.
|
-- File matching options are not checked.
|
||||||
allBatchFiles :: BatchFormat -> (FilePath -> CommandStart) -> Annex ()
|
batchStart :: BatchFormat -> (String -> CommandStart) -> Annex ()
|
||||||
allBatchFiles fmt a = batchInput fmt Right $ batchCommandAction . a
|
batchStart fmt a = batchInput fmt Right $ batchCommandAction . a
|
||||||
|
|
||||||
-- Like allBatchFiles, but checks the file matching options
|
-- Like batchStart, but checks the file matching options
|
||||||
-- and skips non-matching files.
|
-- and skips non-matching files.
|
||||||
batchFilesMatching :: BatchFormat -> (FilePath -> CommandStart) -> Annex ()
|
batchFilesMatching :: BatchFormat -> (FilePath -> CommandStart) -> Annex ()
|
||||||
batchFilesMatching fmt a = do
|
batchFilesMatching fmt a = do
|
||||||
matcher <- getMatcher
|
matcher <- getMatcher
|
||||||
allBatchFiles fmt $ \f ->
|
batchStart fmt $ \f ->
|
||||||
ifM (matcher $ MatchingFile $ FileInfo f f)
|
ifM (matcher $ MatchingFile $ FileInfo f f)
|
||||||
( a f
|
( a f
|
||||||
, return Nothing
|
, return Nothing
|
||||||
|
|
|
@ -19,7 +19,7 @@ import qualified Backend.URL
|
||||||
import Network.URI
|
import Network.URI
|
||||||
|
|
||||||
cmd :: Command
|
cmd :: Command
|
||||||
cmd = notDirect $ notBareRepo $
|
cmd = notDirect $ notBareRepo $ withGlobalOptions [jsonOptions] $
|
||||||
command "fromkey" SectionPlumbing "adds a file using a specific key"
|
command "fromkey" SectionPlumbing "adds a file using a specific key"
|
||||||
(paramRepeating (paramPair paramKey paramPath))
|
(paramRepeating (paramPair paramKey paramPath))
|
||||||
(seek <$$> optParser)
|
(seek <$$> optParser)
|
||||||
|
@ -36,13 +36,25 @@ optParser desc = FromKeyOptions
|
||||||
|
|
||||||
seek :: FromKeyOptions -> CommandSeek
|
seek :: FromKeyOptions -> CommandSeek
|
||||||
seek o = case (batchOption o, keyFilePairs o) of
|
seek o = case (batchOption o, keyFilePairs o) of
|
||||||
(Batch fmt, _) -> commandAction $ startMass fmt
|
(Batch fmt, _) -> seekBatch fmt
|
||||||
-- older way of enabling batch input, does not support BatchNull
|
-- older way of enabling batch input, does not support BatchNull
|
||||||
(NoBatch, []) -> commandAction $ startMass BatchLine
|
(NoBatch, []) -> seekBatch BatchLine
|
||||||
(NoBatch, ps) -> do
|
(NoBatch, ps) -> do
|
||||||
force <- Annex.getState Annex.force
|
force <- Annex.getState Annex.force
|
||||||
withPairs (commandAction . start force) ps
|
withPairs (commandAction . start force) ps
|
||||||
|
|
||||||
|
seekBatch :: BatchFormat -> CommandSeek
|
||||||
|
seekBatch fmt = batchInput fmt parse commandAction
|
||||||
|
where
|
||||||
|
parse s =
|
||||||
|
let (keyname, file) = separate (== ' ') s
|
||||||
|
in if not (null keyname) && not (null file)
|
||||||
|
then Right $ go file (mkKey keyname)
|
||||||
|
else Left "Expected pairs of key and filename"
|
||||||
|
go file key = do
|
||||||
|
showStart "fromkey" file
|
||||||
|
next $ perform key file
|
||||||
|
|
||||||
start :: Bool -> (String, FilePath) -> CommandStart
|
start :: Bool -> (String, FilePath) -> CommandStart
|
||||||
start force (keyname, file) = do
|
start force (keyname, file) = do
|
||||||
let key = mkKey keyname
|
let key = mkKey keyname
|
||||||
|
@ -53,22 +65,6 @@ start force (keyname, file) = do
|
||||||
showStart "fromkey" file
|
showStart "fromkey" file
|
||||||
next $ perform key file
|
next $ perform key file
|
||||||
|
|
||||||
startMass :: BatchFormat -> CommandStart
|
|
||||||
startMass fmt = do
|
|
||||||
showStart' "fromkey" (Just "stdin")
|
|
||||||
next (massAdd fmt)
|
|
||||||
|
|
||||||
massAdd :: BatchFormat -> CommandPerform
|
|
||||||
massAdd fmt = go True =<< map (separate (== ' ')) <$> batchLines fmt
|
|
||||||
where
|
|
||||||
go status [] = next $ return status
|
|
||||||
go status ((keyname,f):rest) | not (null keyname) && not (null f) = do
|
|
||||||
let key = mkKey keyname
|
|
||||||
ok <- perform' key f
|
|
||||||
let !status' = status && ok
|
|
||||||
go status' rest
|
|
||||||
go _ _ = giveup "Expected pairs of key and file on stdin, but got something else."
|
|
||||||
|
|
||||||
-- From user input to a Key.
|
-- From user input to a Key.
|
||||||
-- User can input either a serialized key, or an url.
|
-- User can input either a serialized key, or an url.
|
||||||
--
|
--
|
||||||
|
@ -85,18 +81,20 @@ mkKey s = case parseURI s of
|
||||||
Nothing -> giveup $ "bad key/url " ++ s
|
Nothing -> giveup $ "bad key/url " ++ s
|
||||||
|
|
||||||
perform :: Key -> FilePath -> CommandPerform
|
perform :: Key -> FilePath -> CommandPerform
|
||||||
perform key file = do
|
perform key file = lookupFileNotHidden file >>= \case
|
||||||
ok <- perform' key file
|
Nothing -> ifM (liftIO $ doesFileExist file)
|
||||||
next $ return ok
|
( hasothercontent
|
||||||
|
, do
|
||||||
perform' :: Key -> FilePath -> Annex Bool
|
|
||||||
perform' key file = lookupFileNotHidden file >>= \case
|
|
||||||
Nothing -> do
|
|
||||||
link <- calcRepo $ gitAnnexLink file key
|
link <- calcRepo $ gitAnnexLink file key
|
||||||
liftIO $ createDirectoryIfMissing True (parentDir file)
|
liftIO $ createDirectoryIfMissing True (parentDir file)
|
||||||
liftIO $ createSymbolicLink link file
|
liftIO $ createSymbolicLink link file
|
||||||
Annex.Queue.addCommand "add" [Param "--"] [file]
|
Annex.Queue.addCommand "add" [Param "--"] [file]
|
||||||
return True
|
next $ return True
|
||||||
|
)
|
||||||
Just k
|
Just k
|
||||||
| k == key -> return True
|
| k == key -> next $ return True
|
||||||
| otherwise -> giveup $ file ++ " already exists with different content"
|
| otherwise -> hasothercontent
|
||||||
|
where
|
||||||
|
hasothercontent = do
|
||||||
|
warning $ file ++ " already exists with different content"
|
||||||
|
next $ return False
|
||||||
|
|
|
@ -42,6 +42,11 @@ to do that.
|
||||||
(Note that for this to be used, you have to explicitly enable batch mode
|
(Note that for this to be used, you have to explicitly enable batch mode
|
||||||
with `--batch`)
|
with `--batch`)
|
||||||
|
|
||||||
|
* `--json`
|
||||||
|
|
||||||
|
Enable JSON output. This is intended to be parsed by programs that use
|
||||||
|
git-annex. Each line of output is a JSON object.
|
||||||
|
|
||||||
# SEE ALSO
|
# SEE ALSO
|
||||||
|
|
||||||
[[git-annex]](1)
|
[[git-annex]](1)
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
[[!comment format=mdwn
|
||||||
|
username="joey"
|
||||||
|
subject="""comment 1"""
|
||||||
|
date="2019-02-05T17:20:40Z"
|
||||||
|
content="""
|
||||||
|
Many commands do have an interface that works like that. For example
|
||||||
|
`git annex get --batch --json` has json output and does not stop when a get
|
||||||
|
fails.
|
||||||
|
|
||||||
|
So this comes down to specific behaviors of specific commands.
|
||||||
|
I've added --json to fromkey.
|
||||||
|
|
||||||
|
I don't think that setpresentkey can fail, unless there's a disk IO error
|
||||||
|
or something?
|
||||||
|
|
||||||
|
Any others?
|
||||||
|
"""]]
|
Loading…
Add table
Reference in a new issue