2014-01-11 23:28:40 +00:00
/ *
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
Copyright 2014 The Perkeep Authors .
2014-01-11 23:28:40 +00:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package main
import (
2014-01-30 15:21:09 +00:00
"bufio"
2017-11-26 09:05:38 +00:00
"context"
2014-01-11 23:28:40 +00:00
"flag"
"fmt"
2014-01-30 15:21:09 +00:00
"log"
2014-01-11 23:28:40 +00:00
"os"
2014-01-30 15:21:09 +00:00
"strings"
2014-01-11 23:28:40 +00:00
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
"perkeep.org/pkg/blob"
"perkeep.org/pkg/client"
"perkeep.org/pkg/cmdmain"
"perkeep.org/pkg/search"
2014-01-11 23:28:40 +00:00
)
type listCmd struct {
* syncCmd
2014-01-30 15:21:09 +00:00
describe bool // whether to describe each blob.
cl * client . Client // client used for the describe requests.
2014-01-11 23:28:40 +00:00
}
func init ( ) {
cmdmain . RegisterCommand ( "list" , func ( flags * flag . FlagSet ) cmdmain . CommandRunner {
cmd := & listCmd {
2014-01-30 15:21:09 +00:00
syncCmd : & syncCmd {
2014-01-11 23:28:40 +00:00
dest : "stdout" ,
} ,
2014-01-30 15:21:09 +00:00
describe : false ,
2014-01-11 23:28:40 +00:00
}
2014-01-30 15:21:09 +00:00
flags . StringVar ( & cmd . syncCmd . src , "src" , "" , "Source blobserver is either a URL prefix (with optional path), a host[:port], a path (starting with /, ./, or ../), or blank to use the Camlistore client config's default host." )
flags . BoolVar ( & cmd . describe , "describe" , false , "Use describe requests to get each blob's type. Requires a source server with a search endpoint. Mostly used for demos. Requires many extra round-trips to the server currently." )
2014-01-11 23:28:40 +00:00
return cmd
} )
}
2014-01-30 15:21:09 +00:00
const describeBatchSize = 50
2014-01-11 23:28:40 +00:00
func ( c * listCmd ) Describe ( ) string {
return "List blobs on a server."
}
func ( c * listCmd ) Usage ( ) {
fmt . Fprintf ( os . Stderr , "Usage: camtool [globalopts] list [listopts] \n" )
}
func ( c * listCmd ) Examples ( ) [ ] string {
return nil
}
2014-01-30 15:21:09 +00:00
func ( c * listCmd ) RunCommand ( args [ ] string ) error {
if ! c . describe {
return c . syncCmd . RunCommand ( args )
}
2014-03-07 15:22:46 +00:00
stdout := cmdmain . Stdout
defer func ( ) { cmdmain . Stdout = stdout } ( )
2014-01-30 15:21:09 +00:00
pr , pw , err := os . Pipe ( )
if err != nil {
return fmt . Errorf ( "Could not create pipe to read from stdout: %v" , err )
}
defer pr . Close ( )
2014-03-07 15:22:46 +00:00
cmdmain . Stdout = pw
2014-01-30 15:21:09 +00:00
if err := c . setClient ( ) ; err != nil {
return err
}
scanner := bufio . NewScanner ( pr )
go func ( ) {
err := c . syncCmd . RunCommand ( args )
if err != nil {
log . Printf ( "Error when enumerating source with sync: %v" , err )
}
pw . Close ( )
} ( )
blobRefs := make ( [ ] blob . Ref , 0 , describeBatchSize )
describe := func ( ) error {
if len ( blobRefs ) == 0 {
return nil
}
// TODO(mpl): setting depth to 1, not 0, because otherwise r.depth() in pkg/search/handler.go defaults to 4. Can't remember why we disallowed 0 right now, and I do not want to change that in pkg/search/handler.go and risk breaking things.
2016-04-22 04:34:24 +00:00
described , err := c . cl . Describe ( context . Background ( ) , & search . DescribeRequest {
2014-01-30 15:21:09 +00:00
BlobRefs : blobRefs ,
Depth : 1 ,
} )
if err != nil {
return fmt . Errorf ( "Error when describing blobs %v: %v" , blobRefs , err )
}
for _ , v := range blobRefs {
2014-03-07 15:22:46 +00:00
blob , ok := described . Meta [ v . String ( ) ]
if ! ok {
// This can happen if the index is out of sync with the storage we enum from.
fmt . Fprintf ( stdout , "%v <not described>\n" , v )
continue
}
2014-01-30 15:21:09 +00:00
detailed := detail ( blob )
if detailed != "" {
detailed = fmt . Sprintf ( "\t%v" , detailed )
}
fmt . Fprintf ( stdout , "%v %v%v\n" , v , blob . Size , detailed )
}
blobRefs = make ( [ ] blob . Ref , 0 , describeBatchSize )
return nil
}
for scanner . Scan ( ) {
fields := strings . Fields ( scanner . Text ( ) )
if len ( fields ) != 2 {
return fmt . Errorf ( "Bogus output from sync: got %q, wanted \"blobref size\"" , scanner . Text ( ) )
}
blobRefs = append ( blobRefs , blob . MustParse ( fields [ 0 ] ) )
if len ( blobRefs ) == describeBatchSize {
if err := describe ( ) ; err != nil {
return err
}
}
}
if err := describe ( ) ; err != nil {
return err
}
if err := scanner . Err ( ) ; err != nil {
return fmt . Errorf ( "Error reading on pipe from stdout: %v" , err )
}
return nil
}
// setClient configures a client for c, for the describe requests.
func ( c * listCmd ) setClient ( ) error {
ss , err := c . syncCmd . storageFromParam ( "src" , c . syncCmd . src )
if err != nil {
fmt . Errorf ( "Could not set client for describe requests: %v" , err )
}
var ok bool
c . cl , ok = ss . ( * client . Client )
if ! ok {
return fmt . Errorf ( "storageFromParam returned a %T, was expecting a *client.Client" , ss )
}
return nil
}
func detail ( blob * search . DescribedBlob ) string {
// TODO(mpl): attrType, value for claim. but I don't think they're accessible just with a describe req.
if blob . CamliType == "file" {
return fmt . Sprintf ( "%v (%v size=%v)" , blob . CamliType , blob . File . FileName , blob . File . Size )
}
return blob . CamliType
}