2019-02-17 18:12:06 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
|
|
|
"github.com/MichaelMure/git-bug/util/interrupt"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2019-04-03 22:33:53 +03:00
|
|
|
var (
|
|
|
|
userFieldsQuery string
|
|
|
|
)
|
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
func runUser(cmd *cobra.Command, args []string) error {
|
|
|
|
backend, err := cache.NewRepoCache(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer backend.Close()
|
|
|
|
interrupt.RegisterCleaner(backend.Close)
|
|
|
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
return errors.New("only one identity can be displayed at a time")
|
|
|
|
}
|
|
|
|
|
|
|
|
var id *cache.IdentityCache
|
|
|
|
if len(args) == 1 {
|
|
|
|
id, err = backend.ResolveIdentityPrefix(args[0])
|
|
|
|
} else {
|
|
|
|
id, err = backend.GetUserIdentity()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-03 22:33:53 +03:00
|
|
|
if userFieldsQuery != "" {
|
|
|
|
switch userFieldsQuery {
|
|
|
|
case "email":
|
|
|
|
fmt.Printf("%s\n", id.Email())
|
|
|
|
case "humanId":
|
2019-08-12 17:12:14 +03:00
|
|
|
fmt.Printf("%s\n", id.Id().Human())
|
2019-04-03 22:33:53 +03:00
|
|
|
case "id":
|
|
|
|
fmt.Printf("%s\n", id.Id())
|
|
|
|
case "lastModification":
|
|
|
|
fmt.Printf("%s\n", id.LastModification().
|
|
|
|
Time().Format("Mon Jan 2 15:04:05 2006 +0200"))
|
|
|
|
case "lastModificationLamport":
|
|
|
|
fmt.Printf("%d\n", id.LastModificationLamport())
|
|
|
|
case "login":
|
|
|
|
fmt.Printf("%s\n", id.Login())
|
|
|
|
case "metadata":
|
|
|
|
for key, value := range id.ImmutableMetadata() {
|
|
|
|
fmt.Printf("%s\n%s\n", key, value)
|
|
|
|
}
|
|
|
|
case "name":
|
|
|
|
fmt.Printf("%s\n", id.Name())
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("\nUnsupported field: %s\n", userFieldsQuery)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
fmt.Printf("Id: %s\n", id.Id())
|
|
|
|
fmt.Printf("Name: %s\n", id.Name())
|
|
|
|
fmt.Printf("Login: %s\n", id.Login())
|
|
|
|
fmt.Printf("Email: %s\n", id.Email())
|
2019-02-25 01:05:03 +03:00
|
|
|
fmt.Printf("Last modification: %s (lamport %d)\n",
|
|
|
|
id.LastModification().Time().Format("Mon Jan 2 15:04:05 2006 +0200"),
|
|
|
|
id.LastModificationLamport())
|
2019-02-27 00:27:30 +03:00
|
|
|
fmt.Println("Metadata:")
|
|
|
|
for key, value := range id.ImmutableMetadata() {
|
|
|
|
fmt.Printf(" %s --> %s\n", key, value)
|
|
|
|
}
|
2019-02-19 01:16:47 +03:00
|
|
|
// fmt.Printf("Protected: %v\n", id.IsProtected())
|
2019-02-17 18:12:06 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var userCmd = &cobra.Command{
|
2019-03-23 21:03:39 +03:00
|
|
|
Use: "user [<user-id>]",
|
2019-02-24 16:46:08 +03:00
|
|
|
Short: "Display or change the user identity.",
|
2019-02-17 18:12:06 +03:00
|
|
|
PreRunE: loadRepo,
|
|
|
|
RunE: runUser,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(userCmd)
|
|
|
|
userCmd.Flags().SortFlags = false
|
2019-04-03 22:33:53 +03:00
|
|
|
|
|
|
|
userCmd.Flags().StringVarP(&userFieldsQuery, "field", "f", "",
|
|
|
|
"Select field to display. Valid values are [email,humanId,id,lastModification,lastModificationLamport,login,metadata,name]")
|
2019-02-17 18:12:06 +03:00
|
|
|
}
|