2016-05-26 20:29:36 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2016-11-28 15:12:47 +00:00
|
|
|
"github.com/git-lfs/git-lfs/locking"
|
2016-05-26 20:29:36 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-05-27 20:53:14 +00:00
|
|
|
locksCmdFlags = new(locksFlags)
|
2016-05-26 20:29:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func locksCommand(cmd *cobra.Command, args []string) {
|
2016-05-27 21:58:17 +00:00
|
|
|
|
2016-05-27 22:19:16 +00:00
|
|
|
filters, err := locksCmdFlags.Filters()
|
|
|
|
if err != nil {
|
2016-11-28 15:12:47 +00:00
|
|
|
Exit("Error building filters: %v", err)
|
2016-05-27 22:19:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 11:25:02 +00:00
|
|
|
if len(lockRemote) > 0 {
|
|
|
|
cfg.CurrentRemote = lockRemote
|
|
|
|
}
|
2016-12-05 09:47:29 +00:00
|
|
|
lockClient, err := locking.NewClient(cfg)
|
|
|
|
if err != nil {
|
|
|
|
Exit("Unable to create lock system: %v", err.Error())
|
|
|
|
}
|
2016-12-05 15:01:25 +00:00
|
|
|
defer lockClient.Close()
|
2016-12-05 11:25:02 +00:00
|
|
|
var lockCount int
|
2016-12-05 18:16:30 +00:00
|
|
|
locks, err := lockClient.SearchLocks(filters, locksCmdFlags.Limit, locksCmdFlags.Local)
|
2016-11-28 17:01:06 +00:00
|
|
|
// Print any we got before exiting
|
|
|
|
for _, lock := range locks {
|
|
|
|
Print("%s\t%s <%s>", lock.Path, lock.Name, lock.Email)
|
2016-11-28 15:12:47 +00:00
|
|
|
lockCount++
|
2016-05-26 20:29:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 15:12:47 +00:00
|
|
|
if err != nil {
|
|
|
|
Exit("Error while retrieving locks: %v", err)
|
2016-05-26 20:29:36 +00:00
|
|
|
}
|
2016-11-28 15:12:47 +00:00
|
|
|
|
|
|
|
Print("\n%d lock(s) matched query.", lockCount)
|
2016-05-26 20:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// locksFlags wraps up and holds all of the flags that can be given to the
|
|
|
|
// `git lfs locks` command.
|
|
|
|
type locksFlags struct {
|
|
|
|
// Path is an optional filter parameter to filter against the lock's
|
|
|
|
// path
|
|
|
|
Path string
|
|
|
|
// Id is an optional filter parameter used to filtere against the lock's
|
|
|
|
// ID.
|
|
|
|
Id string
|
|
|
|
// limit is an optional request parameter sent to the server used to
|
|
|
|
// limit the
|
|
|
|
Limit int
|
2016-12-05 18:16:30 +00:00
|
|
|
// local limits the scope of lock reporting to the locally cached record
|
|
|
|
// of locks for the current user & doesn't query the server
|
|
|
|
Local bool
|
2016-05-26 20:29:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-28 15:12:47 +00:00
|
|
|
// Filters produces a filter based on locksFlags instance.
|
|
|
|
func (l *locksFlags) Filters() (map[string]string, error) {
|
|
|
|
filters := make(map[string]string)
|
2016-05-26 20:29:36 +00:00
|
|
|
|
|
|
|
if l.Path != "" {
|
2016-05-27 22:19:16 +00:00
|
|
|
path, err := lockPath(l.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-28 15:12:47 +00:00
|
|
|
filters["path"] = path
|
2016-05-26 20:29:36 +00:00
|
|
|
}
|
|
|
|
if l.Id != "" {
|
2016-11-28 15:12:47 +00:00
|
|
|
filters["id"] = l.Id
|
2016-05-26 20:29:36 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 22:19:16 +00:00
|
|
|
return filters, nil
|
2016-05-26 20:29:36 +00:00
|
|
|
}
|
2016-08-10 15:33:25 +00:00
|
|
|
|
|
|
|
func init() {
|
2016-09-01 16:09:38 +00:00
|
|
|
if !isCommandEnabled(cfg, "locks") {
|
|
|
|
return
|
|
|
|
}
|
2016-08-10 15:33:25 +00:00
|
|
|
|
2016-09-01 16:09:38 +00:00
|
|
|
RegisterCommand("locks", locksCommand, func(cmd *cobra.Command) {
|
2016-08-10 15:33:25 +00:00
|
|
|
cmd.Flags().StringVarP(&lockRemote, "remote", "r", cfg.CurrentRemote, lockRemoteHelp)
|
|
|
|
cmd.Flags().StringVarP(&locksCmdFlags.Path, "path", "p", "", "filter locks results matching a particular path")
|
|
|
|
cmd.Flags().StringVarP(&locksCmdFlags.Id, "id", "i", "", "filter locks results matching a particular ID")
|
|
|
|
cmd.Flags().IntVarP(&locksCmdFlags.Limit, "limit", "l", 0, "optional limit for number of results to return")
|
2016-12-05 18:16:30 +00:00
|
|
|
cmd.Flags().BoolVarP(&locksCmdFlags.Local, "local", "", false, "only list cached local record of own locks")
|
2016-08-10 15:33:25 +00:00
|
|
|
})
|
|
|
|
}
|