whereis --json: Make url list be included in machine-parseable form.

This commit is contained in:
Joey Hess 2016-01-06 12:33:32 -04:00
parent 6a194e09ff
commit b96cfdc094
Failed to extract signature
4 changed files with 21 additions and 12 deletions

View file

@ -7,8 +7,6 @@
module Command.Whereis where module Command.Whereis where
import qualified Data.Map as M
import Common.Annex import Common.Annex
import Command import Command
import Remote import Remote
@ -16,6 +14,8 @@ import Logs.Trust
import Logs.Web import Logs.Web
import Remote.Web (getWebUrls) import Remote.Web (getWebUrls)
import qualified Data.Map as M
cmd :: Command cmd :: Command
cmd = noCommit $ withGlobalOptions (jsonOption : annexedMatchingOptions) $ cmd = noCommit $ withGlobalOptions (jsonOption : annexedMatchingOptions) $
command "whereis" SectionQuery command "whereis" SectionQuery
@ -65,21 +65,21 @@ perform remotemap key = do
-- Since other remotes than the web remote can set urls -- Since other remotes than the web remote can set urls
-- where a key can be downloaded, get and show all such urls -- where a key can be downloaded, get and show all such urls
-- as a special case. -- as a special case.
showRemote "web" =<< getWebUrls key showRemoteUrls "web" =<< getWebUrls key
forM_ (mapMaybe (`M.lookup` remotemap) locations) $ forM_ (mapMaybe (`M.lookup` remotemap) locations) $
performRemote key performRemoteUrls key
if null safelocations then stop else next $ return True if null safelocations then stop else next $ return True
where where
copiesplural 1 = "copy" copiesplural 1 = "copy"
copiesplural _ = "copies" copiesplural _ = "copies"
untrustedheader = "The following untrusted locations may also have copies:\n" untrustedheader = "The following untrusted locations may also have copies:\n"
performRemote :: Key -> Remote -> Annex () performRemoteUrls :: Key -> Remote -> Annex ()
performRemote key remote = do performRemoteUrls key remote = do
ls <- (++) ls <- (++)
<$> askremote <$> askremote
<*> claimedurls <*> claimedurls
showRemote (name remote) ls showRemoteUrls (name remote) ls
where where
askremote = maybe (pure []) (flip id key) (whereisKey remote) askremote = maybe (pure []) (flip id key) (whereisKey remote)
claimedurls = do claimedurls = do
@ -89,8 +89,10 @@ performRemote key remote = do
<$> getUrls key <$> getUrls key
filterM (\u -> (==) <$> pure remote <*> claimingUrl u) us filterM (\u -> (==) <$> pure remote <*> claimingUrl u) us
showRemote :: String -> [String] -> Annex () showRemoteUrls :: String -> [String] -> Annex ()
showRemote n ls showRemoteUrls nm us
| null ls = return () | null us = return ()
| otherwise = showLongNote $ unlines $ | otherwise = do
map (\l -> n ++ ": " ++ l) ls let ls = unlines $ map (\u -> nm ++ ": " ++ u) us
outputMessage noop ('\n' : indent ls ++ "\n")
maybeShowJSON [("urls", us)]

View file

@ -35,6 +35,7 @@ module Messages (
disableDebugOutput, disableDebugOutput,
debugEnabled, debugEnabled,
commandProgressDisabled, commandProgressDisabled,
outputMessage,
) where ) where
import Text.JSON import Text.JSON

1
debian/changelog vendored
View file

@ -38,6 +38,7 @@ git-annex (6.20151219) UNRELEASED; urgency=medium
* info --json: Improve json for "backend usage", using a nested object * info --json: Improve json for "backend usage", using a nested object
with fields for each backend instead of the previous weird nested lists. with fields for each backend instead of the previous weird nested lists.
This may break existing parsers of this json output, if there were any. This may break existing parsers of this json output, if there were any.
* whereis --json: Make url list be included in machine-parseable form.
-- Joey Hess <id@joeyh.name> Sat, 19 Dec 2015 13:31:17 -0400 -- Joey Hess <id@joeyh.name> Sat, 19 Dec 2015 13:31:17 -0400

View file

@ -6,6 +6,9 @@ http://git-annex.branchable.com/tips/using_the_web_as_a_special_remote/#comment-
If the "note" properties are meant to be comments, it might be a good idea to find another property for the url(s). Please note that I haven't looked at multiple urls for an object, so I'm not sure that only the last listed url will appear in parsed json object. If the "note" properties are meant to be comments, it might be a good idea to find another property for the url(s). Please note that I haven't looked at multiple urls for an object, so I'm not sure that only the last listed url will appear in parsed json object.
> The note properties are any notes git-annex normally displays while
> running a command.
### What steps will reproduce the problem? ### What steps will reproduce the problem?
git-annex whereis --json git-annex whereis --json
@ -42,3 +45,5 @@ umeboshi@bard:~/tmp/testweb$
### Have you had any luck using git-annex before? (Sometimes we get tired of reading bug reports all day and a lil' positive end note does wonders) ### Have you had any luck using git-annex before? (Sometimes we get tired of reading bug reports all day and a lil' positive end note does wonders)
Lol! Positive note! I spend a couple of days making specially crafted rss files for importfeed, so I can have appropriate filenames, then look back and see that you are working on '--batch --with-files' options to addurl. Have a Happy New Year! :) Lol! Positive note! I spend a couple of days making specially crafted rss files for importfeed, so I can have appropriate filenames, then look back and see that you are working on '--batch --with-files' options to addurl. Have a Happy New Year! :)
> [[fixed|done]] --[[Joey]]