github.com/weplanx/server@v0.2.6-0.20240318110640-f7e75155779a/cmd/user.go (about)

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  	"github.com/spf13/cobra"
     6  	"github.com/weplanx/server/api"
     7  	"github.com/weplanx/server/bootstrap"
     8  	"github.com/weplanx/server/common"
     9  	"github.com/weplanx/server/model"
    10  	"time"
    11  )
    12  
    13  func User() *cobra.Command {
    14  	var email string
    15  	var password string
    16  	userCmd := &cobra.Command{
    17  		Use:   "user",
    18  		Short: "Create an email account",
    19  		RunE: func(cmd *cobra.Command, args []string) (err error) {
    20  			ctx := cmd.Context()
    21  			values := ctx.Value("values").(*common.Values)
    22  
    23  			var x *api.API
    24  			if x, err = bootstrap.NewAPI(values); err != nil {
    25  				return
    26  			}
    27  			ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
    28  			defer cancel()
    29  			if _, err = x.Db.Collection("users").InsertOne(
    30  				ctx,
    31  				model.NewUser(email, password),
    32  			); err != nil {
    33  				return
    34  			}
    35  			return
    36  		},
    37  	}
    38  	userCmd.PersistentFlags().StringVarP(&email,
    39  		"email", "u", "",
    40  		"User's email <Must be email>",
    41  	)
    42  	userCmd.PersistentFlags().StringVarP(&password,
    43  		"password", "p", "",
    44  		"User's password <between 8-20>",
    45  	)
    46  	return userCmd
    47  }