aboutsummaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-02-17 16:12:06 +0100
committerMichael Muré <batolettre@gmail.com>2019-03-01 22:40:25 +0100
commit864eae0d6bd0732260c0c56583bb77f9b25b60f6 (patch)
treeb1a0384f2fdb0af0c8aaf0f1b0fbc1c445c418f3 /commands
parentda558b05ef79f4c80df10c6969a9ae5f4f764f96 (diff)
downloadgit-bug-864eae0d6bd0732260c0c56583bb77f9b25b60f6.tar.gz
identity: work on higher level now, cache, first two identity commands
Diffstat (limited to 'commands')
-rw-r--r--commands/id.go49
-rw-r--r--commands/user.go56
-rw-r--r--commands/user_create.go76
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
+}