user_command.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. return ac
  31. }
  32. var (
  33. passwordInteractive bool
  34. )
  35. func NewUserAddCommand() *cobra.Command {
  36. cmd := cobra.Command{
  37. Use: "add <user name>",
  38. Short: "add a new user",
  39. Run: userAddCommandFunc,
  40. }
  41. cmd.Flags().BoolVar(&passwordInteractive, "interactive", true, "read password from stdin instead of interactive terminal")
  42. return &cmd
  43. }
  44. func NewUserDeleteCommand() *cobra.Command {
  45. return &cobra.Command{
  46. Use: "delete <user name>",
  47. Short: "delete a user",
  48. Run: userDeleteCommandFunc,
  49. }
  50. }
  51. // userAddCommandFunc executes the "user add" command.
  52. func userAddCommandFunc(cmd *cobra.Command, args []string) {
  53. if len(args) != 1 {
  54. ExitWithError(ExitBadArgs, fmt.Errorf("user add command requires user name as its argument."))
  55. }
  56. var password string
  57. if !passwordInteractive {
  58. fmt.Scanf("%s", &password)
  59. } else {
  60. prompt1 := fmt.Sprintf("Password of %s: ", args[0])
  61. password1, err1 := speakeasy.Ask(prompt1)
  62. if err1 != nil {
  63. ExitWithError(ExitBadArgs, fmt.Errorf("failed to ask password: %s.", err1))
  64. }
  65. if len(password1) == 0 {
  66. ExitWithError(ExitBadArgs, fmt.Errorf("empty password"))
  67. }
  68. prompt2 := fmt.Sprintf("Type password of %s again for confirmation: ", args[0])
  69. password2, err2 := speakeasy.Ask(prompt2)
  70. if err2 != nil {
  71. ExitWithError(ExitBadArgs, fmt.Errorf("failed to ask password: %s.", err2))
  72. }
  73. if strings.Compare(password1, password2) != 0 {
  74. ExitWithError(ExitBadArgs, fmt.Errorf("given passwords are different."))
  75. }
  76. password = password1
  77. }
  78. _, err := mustClientFromCmd(cmd).Auth.UserAdd(context.TODO(), args[0], password)
  79. if err != nil {
  80. ExitWithError(ExitError, err)
  81. }
  82. }
  83. // userDeleteCommandFunc executes the "user delete" command.
  84. func userDeleteCommandFunc(cmd *cobra.Command, args []string) {
  85. if len(args) != 1 {
  86. ExitWithError(ExitBadArgs, fmt.Errorf("user delete command requires user name as its argument."))
  87. }
  88. _, err := mustClientFromCmd(cmd).Auth.UserDelete(context.TODO(), args[0])
  89. if err != nil {
  90. ExitWithError(ExitError, err)
  91. }
  92. }