Browse Source

add show cmd

bugtaker 6 years ago
parent
commit
f8d95df468
4 changed files with 87 additions and 1 deletions
  1. 9 0
      .goreleaser.yml
  2. 67 0
      cmd/show.go
  3. 6 1
      cmd/use.go
  4. 5 0
      main.go

+ 9 - 0
.goreleaser.yml

@@ -19,3 +19,12 @@ changelog:
     exclude:
     - '^docs:'
     - '^test:'
+brew:
+  github:
+    owner: jsmartx
+    name: homebrew-giter
+  folder: Formula
+  homepage:  https://github.com/jsmartx/
+  description: Git users manager
+  test: |
+    system "#{bin}/giter -v"

+ 67 - 0
cmd/show.go

@@ -0,0 +1,67 @@
+package cmd
+
+import (
+	"errors"
+	"fmt"
+	"github.com/jsmartx/giter/git"
+	"github.com/jsmartx/giter/store"
+	"github.com/jsmartx/giter/util"
+	"github.com/urfave/cli"
+	"strconv"
+)
+
+func Show(c *cli.Context) error {
+	g, err := git.New(".")
+	if err != nil {
+		return err
+	}
+	name := c.Args().First()
+	if name == "" {
+		defaultName := ""
+		if u := g.GetUser(); u != nil {
+			defaultName = u.Name
+		}
+		name = util.Prompt(&util.PromptConfig{
+			Prompt:  "user name: ",
+			Default: defaultName,
+		})
+	}
+	s := store.New()
+	users := s.List(name, true)
+	if len(users) == 0 {
+		return errors.New("User not found!")
+	}
+	u := users[0]
+	if len(users) > 1 {
+		fmt.Printf("There are %d users:\n", len(users))
+		for i, item := range users {
+			fmt.Printf("%4d) %s\n", i+1, item.String())
+		}
+		str := util.Prompt(&util.PromptConfig{
+			Prompt: "Enter number to select user: ",
+		})
+		i, err := strconv.Atoi(str)
+		if err != nil {
+			return err
+		}
+		if i < 1 || i > len(users) {
+			return errors.New("Out of range")
+		}
+		u = users[i-1]
+	}
+	keyPath, err := u.KeyPath()
+	if err != nil {
+		return err
+	}
+	if u.IsSSH() {
+		fmt.Printf("       User: %s\n", u.String())
+		fmt.Printf("      Email: %s\n", u.Email)
+		fmt.Printf("Private Key: %s\n", keyPath)
+		fmt.Printf(" Public Key: %s.pub\n", keyPath)
+	} else {
+		fmt.Printf("      User: %s\n", u.String())
+		fmt.Printf("     Email: %s\n", u.Email)
+		fmt.Printf("Credential: %s.credential\n", keyPath)
+	}
+	return nil
+}

+ 6 - 1
cmd/use.go

@@ -18,8 +18,13 @@ func Use(c *cli.Context) error {
 	}
 	name := c.Args().First()
 	if name == "" {
+		defaultName := ""
+		if u := g.GetUser(); u != nil {
+			defaultName = u.Name
+		}
 		name = util.Prompt(&util.PromptConfig{
-			Prompt: "user name: ",
+			Prompt:  "user name: ",
+			Default: defaultName,
 		})
 	}
 	s := store.New()

+ 5 - 0
main.go

@@ -25,6 +25,11 @@ func main() {
 			Usage:  "Change git user config to username",
 			Action: cmd.Use,
 		},
+		{
+			Name:   "show",
+			Usage:  "Show git user detail",
+			Action: cmd.Show,
+		},
 		{
 			Name:    "add",
 			Aliases: []string{"new"},