add getting user data cli
This commit is contained in:
@@ -15,6 +15,18 @@ var UserData = &cli.Command{
|
|||||||
Name: "userdata",
|
Name: "userdata",
|
||||||
Usage: "lab user data maintenance",
|
Usage: "lab user data maintenance",
|
||||||
Subcommands: []*cli.Command{
|
Subcommands: []*cli.Command{
|
||||||
|
{
|
||||||
|
Name: "user-get",
|
||||||
|
Usage: "Get specified user info",
|
||||||
|
Action: getUserInfo,
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "username",
|
||||||
|
Aliases: []string{"n"},
|
||||||
|
Usage: "Specific user name",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "transaction-check",
|
Name: "transaction-check",
|
||||||
Usage: "Check whether user all transactions and accounts are correct",
|
Usage: "Check whether user all transactions and accounts are correct",
|
||||||
@@ -47,6 +59,26 @@ var UserData = &cli.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getUserInfo(c *cli.Context) error {
|
||||||
|
_, err := initializeSystem(c)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
userName := c.String("username")
|
||||||
|
user, err := clis.UserData.GetUserByUsername(c, userName)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.BootErrorf("[user_data.getUserInfo] error occurs when getting user data")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.PrintObjectFields(user)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func checkUserTransactionAndAccount(c *cli.Context) error {
|
func checkUserTransactionAndAccount(c *cli.Context) error {
|
||||||
_, err := initializeSystem(c)
|
_, err := initializeSystem(c)
|
||||||
|
|
||||||
|
|||||||
+18
-6
@@ -35,6 +35,23 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetUserByUsername returns user by user name
|
||||||
|
func (a *UserDataCli) GetUserByUsername(c *cli.Context, username string) (*models.User, error) {
|
||||||
|
if username == "" {
|
||||||
|
log.BootErrorf("[user_data.GetUserByUsername] user name is empty")
|
||||||
|
return nil, errs.ErrUsernameIsEmpty
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := a.users.GetUserByUsername(username)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.BootErrorf("[user_data.GetUserByUsername] failed to get user by user name \"%s\", because %s", username, err.Error())
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|
||||||
// CheckTransactionAndAccount checks whether all user transactions and all user accounts are correct
|
// CheckTransactionAndAccount checks whether all user transactions and all user accounts are correct
|
||||||
func (a *UserDataCli) CheckTransactionAndAccount(c *cli.Context, uid int64) (bool, error) {
|
func (a *UserDataCli) CheckTransactionAndAccount(c *cli.Context, uid int64) (bool, error) {
|
||||||
accountMap, categoryMap, tagMap, tagIndexs, err := a.getUserEssentialData(uid)
|
accountMap, categoryMap, tagMap, tagIndexs, err := a.getUserEssentialData(uid)
|
||||||
@@ -171,12 +188,7 @@ func (a *UserDataCli) ExportTransaction(c *cli.Context, uid int64) ([]byte, erro
|
|||||||
|
|
||||||
// GetUserIdByUsername returns user id by user name
|
// GetUserIdByUsername returns user id by user name
|
||||||
func (a *UserDataCli) GetUserIdByUsername(c *cli.Context, username string) (int64, error) {
|
func (a *UserDataCli) GetUserIdByUsername(c *cli.Context, username string) (int64, error) {
|
||||||
if username == "" {
|
user, err := a.GetUserByUsername(c, username)
|
||||||
log.BootErrorf("[user_data.GetUserIdByUsername] user name is empty")
|
|
||||||
return 0, errs.ErrUsernameIsEmpty
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := a.users.GetUserByUsername(username)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.BootErrorf("[user_data.GetUserIdByUsername] failed to get user by user name \"%s\", because %s", username, err.Error())
|
log.BootErrorf("[user_data.GetUserIdByUsername] failed to get user by user name \"%s\", because %s", username, err.Error())
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Clone deep-clones src object to dst object
|
// Clone deep-clones src object to dst object
|
||||||
@@ -21,3 +23,18 @@ func Clone(src, dst interface{}) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrintObjectFields prints all fields in specified object
|
||||||
|
func PrintObjectFields(obj interface{}) {
|
||||||
|
if obj == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
elem := reflect.ValueOf(obj).Elem()
|
||||||
|
typ := elem.Type()
|
||||||
|
|
||||||
|
for i := 0; i < elem.NumField(); i++ {
|
||||||
|
field := elem.Field(i)
|
||||||
|
fmt.Printf("[%s] %v\n", typ.Field(i).Name, field.Interface())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user