You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user.go 1.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package repl
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "os"
  6. "github.com/spf13/cobra"
  7. "gitlink.org.cn/cloudream/jcs-pub/coordinator/internal/db"
  8. cortypes "gitlink.org.cn/cloudream/jcs-pub/coordinator/types"
  9. "golang.org/x/crypto/bcrypt"
  10. "golang.org/x/term"
  11. )
  12. func init() {
  13. userCmd := &cobra.Command{
  14. Use: "user",
  15. Short: "user command",
  16. }
  17. RootCmd.AddCommand(userCmd)
  18. createCmd := &cobra.Command{
  19. Use: "create [account] [nickName]",
  20. Short: "create a new user account",
  21. Args: cobra.ExactArgs(2),
  22. Run: func(cmd *cobra.Command, args []string) {
  23. userCreate(GetCmdCtx(cmd), args[0], args[1])
  24. },
  25. }
  26. userCmd.AddCommand(createCmd)
  27. }
  28. func userCreate(ctx *CommandContext, account string, nickName string) {
  29. _, err := ctx.repl.db.User().GetByAccount(ctx.repl.db.DefCtx(), account)
  30. if err == nil {
  31. fmt.Printf("user %s already exists\n", account)
  32. return
  33. }
  34. fmt.Printf("input account password: ")
  35. pass, err := term.ReadPassword(int(os.Stdin.Fd()))
  36. if err != nil {
  37. fmt.Println("error reading password:", err)
  38. return
  39. }
  40. passHash, err := bcrypt.GenerateFromPassword(pass, bcrypt.DefaultCost)
  41. if err != nil {
  42. fmt.Println("error hashing password:", err)
  43. return
  44. }
  45. user, err := db.DoTx02(ctx.repl.db, func(tx db.SQLContext) (cortypes.User, error) {
  46. return ctx.repl.db.User().Create(tx, account, hex.EncodeToString(passHash), nickName)
  47. })
  48. if err != nil {
  49. fmt.Println("error creating user:", err)
  50. return
  51. }
  52. fmt.Printf("user %s created\n", user.Account)
  53. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。