git-lfs/commands/command_locks.go

120 lines
3.3 KiB
Go
Raw Normal View History

package commands
import (
2016-08-30 20:43:21 +00:00
"encoding/json"
"os"
2017-02-23 20:55:04 +00:00
"sort"
"strings"
2016-08-30 20:43:21 +00:00
"github.com/git-lfs/git-lfs/errors"
2017-02-23 20:55:04 +00:00
"github.com/git-lfs/git-lfs/locking"
"github.com/git-lfs/git-lfs/tools"
"github.com/spf13/cobra"
)
var (
2016-05-27 20:53:14 +00:00
locksCmdFlags = new(locksFlags)
)
func locksCommand(cmd *cobra.Command, args []string) {
filters, err := locksCmdFlags.Filters()
if err != nil {
Exit("Error building filters: %v", err)
}
2017-01-03 22:57:17 +00:00
lockClient := newLockClient(lockRemote)
defer lockClient.Close()
locks, err := lockClient.SearchLocks(filters, locksCmdFlags.Limit, locksCmdFlags.Local)
// Print any we got before exiting
2016-08-30 20:43:21 +00:00
if locksCmdFlags.JSON {
if err := json.NewEncoder(os.Stdout).Encode(locks); err != nil {
Error(err.Error())
}
return
}
2017-02-27 21:16:34 +00:00
var maxPathLen int
var maxNameLen int
2017-02-23 20:55:04 +00:00
lockPaths := make([]string, 0, len(locks))
locksByPath := make(map[string]locking.Lock)
for _, lock := range locks {
2017-02-23 20:55:04 +00:00
lockPaths = append(lockPaths, lock.Path)
locksByPath[lock.Path] = lock
2017-02-27 21:16:34 +00:00
maxPathLen = tools.MaxInt(maxPathLen, len(lock.Path))
if lock.Owner != nil {
maxNameLen = tools.MaxInt(maxNameLen, len(lock.Owner.Name))
}
2017-02-23 20:55:04 +00:00
}
sort.Strings(lockPaths)
for _, lockPath := range lockPaths {
2017-02-27 21:16:34 +00:00
var ownerName string
2017-02-23 20:55:04 +00:00
lock := locksByPath[lockPath]
2017-02-27 21:16:34 +00:00
if lock.Owner != nil {
ownerName = lock.Owner.Name
}
pathPadding := tools.MaxInt(maxPathLen-len(lock.Path), 0)
namePadding := tools.MaxInt(maxNameLen-len(ownerName), 0)
Print("%s%s\t%s%s\tID:%s", lock.Path, strings.Repeat(" ", pathPadding),
ownerName, strings.Repeat(" ", namePadding),
lock.Id,
)
}
if err != nil {
Exit("Error while retrieving locks: %v", errors.Cause(err))
}
}
// 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
// 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-08-30 20:43:21 +00:00
// JSON is an optional parameter to output data in json format.
JSON bool
}
// Filters produces a filter based on locksFlags instance.
func (l *locksFlags) Filters() (map[string]string, error) {
filters := make(map[string]string)
if l.Path != "" {
path, err := lockPath(l.Path)
if err != nil {
return nil, err
}
filters["path"] = path
}
if l.Id != "" {
filters["id"] = l.Id
}
return filters, nil
}
func init() {
RegisterCommand("locks", locksCommand, func(cmd *cobra.Command) {
cmd.Flags().StringVarP(&lockRemote, "remote", "r", cfg.Remote(), 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")
cmd.Flags().BoolVarP(&locksCmdFlags.Local, "local", "", false, "only list cached local record of own locks")
2016-08-30 20:43:21 +00:00
cmd.Flags().BoolVarP(&locksCmdFlags.JSON, "json", "", false, "print output in json")
})
}