2014-03-11 02:47:18 +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-03-11 02:47:18 +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 (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
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/cmdmain"
"perkeep.org/pkg/search"
2015-11-20 22:27:00 +00:00
"go4.org/strutil"
2014-03-11 02:47:18 +00:00
)
type searchCmd struct {
server string
limit int
2016-04-10 01:45:21 +00:00
cont string
2014-03-11 02:47:18 +00:00
describe bool
2015-01-18 04:28:59 +00:00
rawQuery bool
2018-02-27 00:14:09 +00:00
one bool
2014-03-11 02:47:18 +00:00
}
func init ( ) {
cmdmain . RegisterCommand ( "search" , func ( flags * flag . FlagSet ) cmdmain . CommandRunner {
cmd := new ( searchCmd )
flags . StringVar ( & cmd . server , "server" , "" , "Server to search. " + serverFlagHelp )
flags . IntVar ( & cmd . limit , "limit" , 0 , "Limit number of results. 0 is default. Negative means no limit." )
2016-04-10 01:45:21 +00:00
flags . StringVar ( & cmd . cont , "continue" , "" , "Continue token from a previously limited search. The query must be identical to the original search." )
2014-03-11 02:47:18 +00:00
flags . BoolVar ( & cmd . describe , "describe" , false , "Describe results as well." )
2016-04-10 01:45:21 +00:00
flags . BoolVar ( & cmd . rawQuery , "rawquery" , false , "If true, the provided JSON is a SearchQuery, and not a Constraint. In this case, the -limit and -continue flags, if non-zero, are applied after parsing the JSON." )
2018-02-27 00:14:09 +00:00
flags . BoolVar ( & cmd . one , "1" , false , "Output one blob ref per line, suitable for piping into xargs." )
2014-03-11 02:47:18 +00:00
return cmd
} )
}
func ( c * searchCmd ) Describe ( ) string {
return "Execute a search query"
}
func ( c * searchCmd ) Usage ( ) {
fmt . Fprintf ( os . Stderr , "Usage: camtool [globalopts] search <expr or Constraint JSON>\n" )
}
func ( c * searchCmd ) Examples ( ) [ ] string {
return [ ] string {
` "loc:paris is:portrait" # expression ` ,
` ' { "blobrefPrefix":"sha1-f00d"}' # SearchConstraint JSON ` ,
` - # piped from stdin ` ,
}
}
func ( c * searchCmd ) RunCommand ( args [ ] string ) error {
if len ( args ) != 1 {
return cmdmain . UsageError ( "requires search expression or Constraint JSON" )
}
q := args [ 0 ]
if q == "-" {
slurp , err := ioutil . ReadAll ( cmdmain . Stdin )
if err != nil {
return err
}
q = string ( slurp )
}
q = strings . TrimSpace ( q )
req := & search . SearchQuery {
2016-04-10 01:45:21 +00:00
Limit : c . limit ,
Continue : c . cont ,
2014-03-11 02:47:18 +00:00
}
2015-01-18 04:28:59 +00:00
if c . rawQuery {
2016-04-10 01:45:21 +00:00
req . Limit = 0 // clear it if they provided it
req . Continue = "" // clear this as well
2015-01-18 04:28:59 +00:00
if err := json . NewDecoder ( strings . NewReader ( q ) ) . Decode ( & req ) ; err != nil {
return err
}
if c . limit != 0 {
req . Limit = c . limit
}
2016-04-10 01:45:21 +00:00
if c . cont != "" {
req . Continue = c . cont
}
2015-01-18 04:28:59 +00:00
} else if strutil . IsPlausibleJSON ( q ) {
2014-03-11 02:47:18 +00:00
cs := new ( search . Constraint )
if err := json . NewDecoder ( strings . NewReader ( q ) ) . Decode ( & cs ) ; err != nil {
return err
}
req . Constraint = cs
} else {
req . Expression = q
}
if c . describe {
req . Describe = & search . DescribeRequest { }
}
cl := newClient ( c . server )
2018-01-16 23:03:16 +00:00
res , err := cl . Query ( ctxbg , req )
2014-03-11 02:47:18 +00:00
if err != nil {
return err
}
2018-02-27 00:14:09 +00:00
if c . one {
for _ , bl := range res . Blobs {
fmt . Println ( bl . Blob )
}
return nil
}
2014-03-11 02:47:18 +00:00
resj , err := json . MarshalIndent ( res , "" , " " )
if err != nil {
return err
}
resj = append ( resj , '\n' )
_ , err = os . Stdout . Write ( resj )
return err
}