github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/commands/command_locks.go (about) 1 package commands 2 3 import ( 4 "encoding/json" 5 "os" 6 "sort" 7 "strings" 8 9 "github.com/git-lfs/git-lfs/errors" 10 "github.com/git-lfs/git-lfs/git" 11 "github.com/git-lfs/git-lfs/locking" 12 "github.com/git-lfs/git-lfs/tools" 13 "github.com/spf13/cobra" 14 ) 15 16 var ( 17 locksCmdFlags = new(locksFlags) 18 ) 19 20 func locksCommand(cmd *cobra.Command, args []string) { 21 filters, err := locksCmdFlags.Filters() 22 if err != nil { 23 Exit("Error building filters: %v", err) 24 } 25 26 if len(lockRemote) > 0 { 27 cfg.SetRemote(lockRemote) 28 } 29 30 refUpdate := git.NewRefUpdate(cfg.Git, cfg.PushRemote(), cfg.CurrentRef(), nil) 31 lockClient := newLockClient() 32 lockClient.RemoteRef = refUpdate.Right() 33 defer lockClient.Close() 34 35 locks, err := lockClient.SearchLocks(filters, locksCmdFlags.Limit, locksCmdFlags.Local) 36 // Print any we got before exiting 37 38 if locksCmdFlags.JSON { 39 if err := json.NewEncoder(os.Stdout).Encode(locks); err != nil { 40 Error(err.Error()) 41 } 42 return 43 } 44 45 var maxPathLen int 46 var maxNameLen int 47 lockPaths := make([]string, 0, len(locks)) 48 locksByPath := make(map[string]locking.Lock) 49 for _, lock := range locks { 50 lockPaths = append(lockPaths, lock.Path) 51 locksByPath[lock.Path] = lock 52 maxPathLen = tools.MaxInt(maxPathLen, len(lock.Path)) 53 if lock.Owner != nil { 54 maxNameLen = tools.MaxInt(maxNameLen, len(lock.Owner.Name)) 55 } 56 } 57 58 sort.Strings(lockPaths) 59 for _, lockPath := range lockPaths { 60 var ownerName string 61 lock := locksByPath[lockPath] 62 if lock.Owner != nil { 63 ownerName = lock.Owner.Name 64 } 65 66 pathPadding := tools.MaxInt(maxPathLen-len(lock.Path), 0) 67 namePadding := tools.MaxInt(maxNameLen-len(ownerName), 0) 68 Print("%s%s\t%s%s\tID:%s", lock.Path, strings.Repeat(" ", pathPadding), 69 ownerName, strings.Repeat(" ", namePadding), 70 lock.Id, 71 ) 72 } 73 74 if err != nil { 75 Exit("Error while retrieving locks: %v", errors.Cause(err)) 76 } 77 } 78 79 // locksFlags wraps up and holds all of the flags that can be given to the 80 // `git lfs locks` command. 81 type locksFlags struct { 82 // Path is an optional filter parameter to filter against the lock's 83 // path 84 Path string 85 // Id is an optional filter parameter used to filtere against the lock's 86 // ID. 87 Id string 88 // limit is an optional request parameter sent to the server used to 89 // limit the 90 Limit int 91 // local limits the scope of lock reporting to the locally cached record 92 // of locks for the current user & doesn't query the server 93 Local bool 94 // JSON is an optional parameter to output data in json format. 95 JSON bool 96 } 97 98 // Filters produces a filter based on locksFlags instance. 99 func (l *locksFlags) Filters() (map[string]string, error) { 100 filters := make(map[string]string) 101 102 if l.Path != "" { 103 path, err := lockPath(l.Path) 104 if err != nil { 105 return nil, err 106 } 107 108 filters["path"] = path 109 } 110 if l.Id != "" { 111 filters["id"] = l.Id 112 } 113 114 return filters, nil 115 } 116 117 func init() { 118 RegisterCommand("locks", locksCommand, func(cmd *cobra.Command) { 119 cmd.Flags().StringVarP(&lockRemote, "remote", "r", "", lockRemoteHelp) 120 cmd.Flags().StringVarP(&locksCmdFlags.Path, "path", "p", "", "filter locks results matching a particular path") 121 cmd.Flags().StringVarP(&locksCmdFlags.Id, "id", "i", "", "filter locks results matching a particular ID") 122 cmd.Flags().IntVarP(&locksCmdFlags.Limit, "limit", "l", 0, "optional limit for number of results to return") 123 cmd.Flags().BoolVarP(&locksCmdFlags.Local, "local", "", false, "only list cached local record of own locks") 124 cmd.Flags().BoolVarP(&locksCmdFlags.JSON, "json", "", false, "print output in json") 125 }) 126 }