user_command.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2016 Nippon Telegraph and Telephone Corporation.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "fmt"
  17. "strings"
  18. "github.com/bgentry/speakeasy"
  19. "github.com/spf13/cobra"
  20. "golang.org/x/net/context"
  21. )
  22. // NewUserCommand returns the cobra command for "user".
  23. func NewUserCommand() *cobra.Command {
  24. ac := &cobra.Command{
  25. Use: "user <subcommand>",
  26. Short: "user related command",
  27. }
  28. ac.AddCommand(NewUserAddCommand())
  29. ac.AddCommand(NewUserDeleteCommand())
  30. ac.AddCommand(NewUserChangePasswordCommand())
  31. return ac
  32. }
  33. var (
  34. passwordInteractive bool
  35. )
  36. func NewUserAddCommand() *cobra.Command {
  37. cmd := cobra.Command{
  38. Use: "add <user name>",
  39. Short: "add a new user",
  40. Run: userAddCommandFunc,
  41. }
  42. cmd.Flags().BoolVar(&passwordInteractive, "interactive", true, "read password from stdin instead of interactive terminal")
  43. return &cmd
  44. }
  45. func NewUserDeleteCommand() *cobra.Command {
  46. return &cobra.Command{
  47. Use: "delete <user name>",
  48. Short: "delete a user",
  49. Run: userDeleteCommandFunc,
  50. }
  51. }
  52. func NewUserChangePasswordCommand() *cobra.Command {
  53. cmd := cobra.Command{
  54. Use: "passwd <user name>",
  55. Short: "change password of user",
  56. Run: userChangePasswordCommandFunc,
  57. }
  58. cmd.Flags().BoolVar(&passwordInteractive, "interactive", true, "read password from stdin instead of interactive terminal")
  59. return &cmd
  60. }
  61. // userAddCommandFunc executes the "user add" command.
  62. func userAddCommandFunc(cmd *cobra.Command, args []string) {
  63. if len(args) != 1 {
  64. ExitWithError(ExitBadArgs, fmt.Errorf("user add command requires user name as its argument."))
  65. }
  66. var password string
  67. if !passwordInteractive {
  68. fmt.Scanf("%s", &password)
  69. } else {
  70. password = readPasswordInteractive(args[0])
  71. }
  72. _, err := mustClientFromCmd(cmd).Auth.UserAdd(context.TODO(), args[0], password)
  73. if err != nil {
  74. ExitWithError(ExitError, err)
  75. }
  76. fmt.Printf("User %s created\n", args[0])
  77. }
  78. // userDeleteCommandFunc executes the "user delete" command.
  79. func userDeleteCommandFunc(cmd *cobra.Command, args []string) {
  80. if len(args) != 1 {
  81. ExitWithError(ExitBadArgs, fmt.Errorf("user delete command requires user name as its argument."))
  82. }
  83. _, err := mustClientFromCmd(cmd).Auth.UserDelete(context.TODO(), args[0])
  84. if err != nil {
  85. ExitWithError(ExitError, err)
  86. }
  87. fmt.Printf("User %s deleted\n", args[0])
  88. }
  89. // userChangePasswordCommandFunc executes the "user passwd" command.
  90. func userChangePasswordCommandFunc(cmd *cobra.Command, args []string) {
  91. if len(args) != 1 {
  92. ExitWithError(ExitBadArgs, fmt.Errorf("user passwd command requires user name as its argument."))
  93. }
  94. var password string
  95. if !passwordInteractive {
  96. fmt.Scanf("%s", &password)
  97. } else {
  98. password = readPasswordInteractive(args[0])
  99. }
  100. _, err := mustClientFromCmd(cmd).Auth.UserChangePassword(context.TODO(), args[0], password)
  101. if err != nil {
  102. ExitWithError(ExitError, err)
  103. }
  104. fmt.Println("Password updated")
  105. }
  106. func readPasswordInteractive(name string) string {
  107. prompt1 := fmt.Sprintf("Password of %s: ", name)
  108. password1, err1 := speakeasy.Ask(prompt1)
  109. if err1 != nil {
  110. ExitWithError(ExitBadArgs, fmt.Errorf("failed to ask password: %s.", err1))
  111. }
  112. if len(password1) == 0 {
  113. ExitWithError(ExitBadArgs, fmt.Errorf("empty password"))
  114. }
  115. prompt2 := fmt.Sprintf("Type password of %s again for confirmation: ", name)
  116. password2, err2 := speakeasy.Ask(prompt2)
  117. if err2 != nil {
  118. ExitWithError(ExitBadArgs, fmt.Errorf("failed to ask password: %s.", err2))
  119. }
  120. if strings.Compare(password1, password2) != 0 {
  121. ExitWithError(ExitBadArgs, fmt.Errorf("given passwords are different."))
  122. }
  123. return password1
  124. }