2019-02-17 18:12:06 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2019-02-17 18:12:06 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
type userOptions struct {
|
2020-07-28 21:24:24 +03:00
|
|
|
fields string
|
2020-06-28 19:26:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newUserCommand() *cobra.Command {
|
|
|
|
env := newEnv()
|
|
|
|
options := userOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2020-07-28 21:24:24 +03:00
|
|
|
Use: "user [USER-ID]",
|
2020-06-28 20:09:32 +03:00
|
|
|
Short: "Display or change the user identity.",
|
|
|
|
PreRunE: loadBackendEnsureUser(env),
|
|
|
|
PostRunE: closeBackend(env),
|
2020-06-28 19:26:29 +03:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runUser(env, options, args)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.AddCommand(newUserAdoptCommand())
|
|
|
|
cmd.AddCommand(newUserCreateCommand())
|
|
|
|
cmd.AddCommand(newUserLsCommand())
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.SortFlags = false
|
|
|
|
|
2020-07-28 21:24:24 +03:00
|
|
|
flags.StringVarP(&options.fields, "field", "f", "",
|
2020-11-08 21:18:44 +03:00
|
|
|
"Select field to display. Valid values are [email,humanId,id,lastModification,lastModificationLamports,login,metadata,name]")
|
2019-04-03 22:33:53 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runUser(env *Env, opts userOptions, args []string) error {
|
2019-02-17 18:12:06 +03:00
|
|
|
if len(args) > 1 {
|
|
|
|
return errors.New("only one identity can be displayed at a time")
|
|
|
|
}
|
|
|
|
|
|
|
|
var id *cache.IdentityCache
|
2020-06-28 20:09:32 +03:00
|
|
|
var err error
|
2019-02-17 18:12:06 +03:00
|
|
|
if len(args) == 1 {
|
2020-06-28 20:09:32 +03:00
|
|
|
id, err = env.backend.ResolveIdentityPrefix(args[0])
|
2019-02-17 18:12:06 +03:00
|
|
|
} else {
|
2020-06-28 20:09:32 +03:00
|
|
|
id, err = env.backend.GetUserIdentity()
|
2019-02-17 18:12:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-28 21:24:24 +03:00
|
|
|
if opts.fields != "" {
|
|
|
|
switch opts.fields {
|
2019-04-03 22:33:53 +03:00
|
|
|
case "email":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", id.Email())
|
2020-02-25 23:35:57 +03:00
|
|
|
case "login":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", id.Login())
|
2019-04-03 22:33:53 +03:00
|
|
|
case "humanId":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", id.Id().Human())
|
2019-04-03 22:33:53 +03:00
|
|
|
case "id":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", id.Id())
|
2019-04-03 22:33:53 +03:00
|
|
|
case "lastModification":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", id.LastModification().
|
2019-04-03 22:33:53 +03:00
|
|
|
Time().Format("Mon Jan 2 15:04:05 2006 +0200"))
|
|
|
|
case "lastModificationLamport":
|
2020-11-08 21:18:44 +03:00
|
|
|
for name, t := range id.LastModificationLamports() {
|
|
|
|
env.out.Printf("%s\n%d\n", name, t)
|
|
|
|
}
|
2019-04-03 22:33:53 +03:00
|
|
|
case "metadata":
|
|
|
|
for key, value := range id.ImmutableMetadata() {
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n%s\n", key, value)
|
2019-04-03 22:33:53 +03:00
|
|
|
}
|
|
|
|
case "name":
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", id.Name())
|
2019-04-03 22:33:53 +03:00
|
|
|
|
|
|
|
default:
|
2020-07-28 21:24:24 +03:00
|
|
|
return fmt.Errorf("\nUnsupported field: %s\n", opts.fields)
|
2019-04-03 22:33:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("Id: %s\n", id.Id())
|
|
|
|
env.out.Printf("Name: %s\n", id.Name())
|
|
|
|
env.out.Printf("Email: %s\n", id.Email())
|
|
|
|
env.out.Printf("Login: %s\n", id.Login())
|
2020-11-08 21:18:44 +03:00
|
|
|
env.out.Printf("Last modification: %s\n", id.LastModification().Time().Format("Mon Jan 2 15:04:05 2006 +0200"))
|
|
|
|
env.out.Printf("Last moditication (lamport):\n")
|
|
|
|
for name, t := range id.LastModificationLamports() {
|
|
|
|
env.out.Printf("\t%s: %d", name, t)
|
|
|
|
}
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Println("Metadata:")
|
2019-02-27 00:27:30 +03:00
|
|
|
for key, value := range id.ImmutableMetadata() {
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf(" %s --> %s\n", key, value)
|
2019-02-27 00:27:30 +03:00
|
|
|
}
|
2020-06-28 19:26:29 +03:00
|
|
|
// env.out.Printf("Protected: %v\n", id.IsProtected())
|
2019-02-17 18:12:06 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|