diff options
Diffstat (limited to 'commands')
-rw-r--r-- | commands/id.go | 49 | ||||
-rw-r--r-- | commands/user.go | 56 | ||||
-rw-r--r-- | commands/user_create.go | 76 |
3 files changed, 132 insertions, 49 deletions
diff --git a/commands/id.go b/commands/id.go deleted file mode 100644 index 7eacd986..00000000 --- a/commands/id.go +++ /dev/null @@ -1,49 +0,0 @@ -package commands - -import ( - "errors" - "fmt" - - "github.com/MichaelMure/git-bug/identity" - "github.com/spf13/cobra" -) - -func runId(cmd *cobra.Command, args []string) error { - if len(args) > 1 { - return errors.New("only one identity can be displayed at a time") - } - - var id *identity.Identity - var err error - - if len(args) == 1 { - id, err = identity.ReadLocal(repo, args[0]) - } else { - id, err = identity.GetUserIdentity(repo) - } - - if err != nil { - return err - } - - fmt.Printf("Id: %s\n", id.Id()) - fmt.Printf("Identity: %s\n", id.DisplayName()) - fmt.Printf("Name: %s\n", id.Name()) - fmt.Printf("Login: %s\n", id.Login()) - fmt.Printf("Email: %s\n", id.Email()) - fmt.Printf("Protected: %v\n", id.IsProtected()) - - return nil -} - -var idCmd = &cobra.Command{ - Use: "id [<id>]", - Short: "Display or change the user identity", - PreRunE: loadRepo, - RunE: runId, -} - -func init() { - RootCmd.AddCommand(idCmd) - selectCmd.Flags().SortFlags = false -} diff --git a/commands/user.go b/commands/user.go new file mode 100644 index 00000000..55ad813c --- /dev/null +++ b/commands/user.go @@ -0,0 +1,56 @@ +package commands + +import ( + "errors" + "fmt" + + "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/util/interrupt" + "github.com/spf13/cobra" +) + +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 { + // TODO + return errors.New("this is not working yet, cache need to be hacked on") + id, err = backend.ResolveIdentityPrefix(args[0]) + } else { + id, err = backend.GetUserIdentity() + } + + if err != nil { + return err + } + + 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()) + fmt.Printf("Protected: %v\n", id.IsProtected()) + + return nil +} + +var userCmd = &cobra.Command{ + Use: "user [<id>]", + Short: "Display or change the user identity", + PreRunE: loadRepo, + RunE: runUser, +} + +func init() { + RootCmd.AddCommand(userCmd) + userCmd.Flags().SortFlags = false +} diff --git a/commands/user_create.go b/commands/user_create.go new file mode 100644 index 00000000..50acac3e --- /dev/null +++ b/commands/user_create.go @@ -0,0 +1,76 @@ +package commands + +import ( + "fmt" + + "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/input" + "github.com/MichaelMure/git-bug/util/interrupt" + "github.com/spf13/cobra" +) + +func runUserCreate(cmd *cobra.Command, args []string) error { + backend, err := cache.NewRepoCache(repo) + if err != nil { + return err + } + defer backend.Close() + interrupt.RegisterCleaner(backend.Close) + + preName, err := backend.GetUserName() + if err != nil { + return err + } + + name, err := input.PromptValueRequired("Name", preName) + if err != nil { + return err + } + + preEmail, err := backend.GetUserEmail() + if err != nil { + return err + } + + email, err := input.PromptValueRequired("Email", preEmail) + if err != nil { + return err + } + + login, err := input.PromptValue("Avatar URL", "") + if err != nil { + return err + } + + id, err := backend.NewIdentityRaw(name, email, "", login, nil) + if err != nil { + return err + } + + err = id.CommitAsNeeded() + if err != nil { + return err + } + + err = backend.SetUserIdentity(id) + if err != nil { + return err + } + + fmt.Println() + fmt.Println(id.Id()) + + return nil +} + +var userCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create a new identity", + PreRunE: loadRepo, + RunE: runUserCreate, +} + +func init() { + userCmd.AddCommand(userCreateCmd) + userCreateCmd.Flags().SortFlags = false +} |