2011-09-27 03:02:35 +00:00
/ *
2018-01-04 00:52:49 +00:00
Copyright 2011 The Perkeep Authors
2011-09-27 03:02:35 +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-09-04 21:56:01 +00:00
"encoding/json"
2011-09-27 03:02:35 +00:00
"flag"
"fmt"
2013-09-09 00:46:40 +00:00
"time"
2011-09-27 03:02:35 +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/cmdmain"
"perkeep.org/pkg/schema"
"perkeep.org/pkg/search"
2011-09-27 03:02:35 +00:00
)
type shareCmd struct {
2014-09-04 21:56:01 +00:00
search string
2011-09-27 03:02:35 +00:00
transitive bool
2013-09-09 00:46:40 +00:00
duration time . Duration // zero means forever
2011-09-27 03:02:35 +00:00
}
func init ( ) {
2013-02-18 23:35:43 +00:00
cmdmain . RegisterCommand ( "share" , func ( flags * flag . FlagSet ) cmdmain . CommandRunner {
2011-09-27 03:02:35 +00:00
cmd := new ( shareCmd )
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
flags . StringVar ( & cmd . search , "search" , "" , "share a search result, rather than a single blob. Should be the JSON representation of a search.SearchQuery (see https://perkeep.org/pkg/search/#SearchQuery for details). Exclusive with, and overrides the <blobref> parameter." )
2011-09-27 03:02:35 +00:00
flags . BoolVar ( & cmd . transitive , "transitive" , false , "share everything reachable from the given blobref" )
2013-09-09 00:46:40 +00:00
flags . DurationVar ( & cmd . duration , "duration" , 0 , "how long the share claim is valid for. The default of 0 means forever. For valid formats, see http://golang.org/pkg/time/#ParseDuration" )
2011-09-27 03:02:35 +00:00
return cmd
} )
}
2013-02-26 14:26:18 +00:00
func ( c * shareCmd ) Describe ( ) string {
2014-09-04 21:56:01 +00:00
return ` Grant access to a resource or search by making a "share" blob. `
2013-02-26 14:26:18 +00:00
}
2011-09-27 03:02:35 +00:00
func ( c * shareCmd ) Usage ( ) {
2016-05-11 21:58:15 +00:00
fmt . Fprintf ( cmdmain . Stderr , ` Usage : camput share [ opts ] [ < blobref > ] # blobRef of a file or directory
2011-09-27 03:02:35 +00:00
` )
}
2012-10-19 10:50:43 +00:00
func ( c * shareCmd ) Examples ( ) [ ] string {
return [ ] string {
2014-09-04 21:56:01 +00:00
"-transitive sha1-83896fcb182db73b653181652129d739280766b5" ,
` -search=' { "expression":"tag:blogphotos is:image","limit":42}' ` ,
2012-10-19 10:50:43 +00:00
}
}
2013-02-18 23:35:43 +00:00
func ( c * shareCmd ) RunCommand ( args [ ] string ) error {
2014-09-04 21:56:01 +00:00
unsigned := schema . NewShareRef ( schema . ShareHaveRef , c . transitive )
if c . search != "" {
if len ( args ) != 0 {
return cmdmain . UsageError ( "when using the -search flag, share takes zero arguments" )
}
var q search . SearchQuery
if err := json . Unmarshal ( [ ] byte ( c . search ) , & q ) ; err != nil {
return cmdmain . UsageError ( fmt . Sprintf ( "invalid search: %s" , err ) )
}
2014-09-07 02:19:36 +00:00
unsigned . SetShareSearch ( & q )
2014-09-04 21:56:01 +00:00
} else {
if len ( args ) != 1 {
return cmdmain . UsageError ( "share takes at most one argument" )
}
target , ok := blob . Parse ( args [ 0 ] )
if ! ok {
return cmdmain . UsageError ( "invalid blobref" )
}
unsigned . SetShareTarget ( target )
2011-09-27 03:02:35 +00:00
}
2014-09-04 21:56:01 +00:00
2013-09-09 00:46:40 +00:00
if c . duration != 0 {
unsigned . SetShareExpiration ( time . Now ( ) . Add ( c . duration ) )
}
2016-11-10 23:50:46 +00:00
up := getUploader ( )
shareRoot , err := up . ShareRoot ( )
if err != nil {
return err
}
pr , err := up . UploadAndSignBlob ( unsigned )
if err := handleResult ( "share" , pr , err ) ; err != nil {
// Because handling the failure is left to cmdmain
return nil
}
fmt . Fprintf ( cmdmain . Stdout , "%s%s\n" , shareRoot , pr . BlobRef )
2011-09-27 03:02:35 +00:00
return nil
}