update.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package cmd
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "github.com/jsmartx/giter/store"
  7. "github.com/jsmartx/giter/util"
  8. "github.com/urfave/cli"
  9. )
  10. func Update(c *cli.Context) error {
  11. name := c.Args().First()
  12. if name == "" {
  13. name = util.Prompt(&util.PromptConfig{
  14. Prompt: "user name: ",
  15. })
  16. }
  17. s := store.New()
  18. users := s.List(name, true)
  19. if len(users) == 0 {
  20. return errors.New("user not found")
  21. }
  22. u := users[0]
  23. if len(users) > 1 {
  24. fmt.Printf("There are %d users:\n", len(users))
  25. for i, item := range users {
  26. fmt.Printf("%4d) %s\n", i+1, item.String())
  27. }
  28. str := util.Prompt(&util.PromptConfig{
  29. Prompt: "Enter number to select user: ",
  30. })
  31. i, err := strconv.Atoi(str)
  32. if err != nil {
  33. return err
  34. }
  35. if i < 1 || i > len(users) {
  36. return errors.New("Out of range")
  37. }
  38. u = users[i-1]
  39. }
  40. email := util.Prompt(&util.PromptConfig{
  41. Prompt: "user email: ",
  42. Default: u.Email,
  43. })
  44. urlStr := util.Prompt(&util.PromptConfig{
  45. Prompt: "git server: ",
  46. Default: u.FullHost(),
  47. })
  48. url, err := util.ParseURL(urlStr)
  49. util.CheckError(err)
  50. host, port := util.SplitHostPort(url.Host)
  51. user := &store.User{
  52. Name: name,
  53. Email: email,
  54. Scheme: url.Scheme,
  55. Host: host,
  56. Port: port,
  57. }
  58. options := &store.Options{}
  59. if user.IsSSH() {
  60. if u.IsSSH() {
  61. txt := util.Prompt(&util.PromptConfig{
  62. Prompt: fmt.Sprintf("Regenerate the SSH key [Y/n]? "),
  63. })
  64. if txt != "y" && txt != "Y" {
  65. p, err := u.KeyPath()
  66. if err != nil {
  67. return err
  68. }
  69. options.KeyPath = p
  70. }
  71. }
  72. } else {
  73. if !u.IsSSH() {
  74. txt := util.Prompt(&util.PromptConfig{
  75. Prompt: fmt.Sprintf("Reset password [Y/n]? "),
  76. })
  77. if txt == "y" || txt == "Y" {
  78. pwd := util.Prompt(&util.PromptConfig{
  79. Prompt: "user password: ",
  80. Silent: true,
  81. })
  82. options.Password = pwd
  83. }
  84. } else {
  85. pwd := util.Prompt(&util.PromptConfig{
  86. Prompt: "user password: ",
  87. Silent: true,
  88. })
  89. options.Password = pwd
  90. }
  91. }
  92. return s.Update(u.Hash(), user, options)
  93. }