From a50f78151d1080967b342fc5bd3a31fee7f6a4c9 Mon Sep 17 00:00:00 2001 From: Wiky Lyu Date: Fri, 3 Feb 2023 22:06:40 +0800 Subject: [PATCH] mctl --- .gitignore | 1 + Makefile | 5 ++++- db/user.go | 22 +++++++++++++++++-- mctl/main.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++------ mtop.yaml | 4 +++- 5 files changed, 81 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 5be8d0b..c5ffa8b 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ /keys /bin /vendor +/dist \ No newline at end of file diff --git a/Makefile b/Makefile index f215ba3..2d35765 100644 --- a/Makefile +++ b/Makefile @@ -9,4 +9,7 @@ climber: CGO_ENABLED=0 go build -o bin/climber climber/main.go mctl: - CGO_ENABLED=0 go build -o bin/mctl mctl/main.go \ No newline at end of file + CGO_ENABLED=0 go build -o bin/mctl mctl/main.go + +dist: mtop climber mctl + tar -C bin/ -jcvf dist/mtop_linux64.tar.bz2 . \ No newline at end of file diff --git a/db/user.go b/db/user.go index 4629037..ec27230 100644 --- a/db/user.go +++ b/db/user.go @@ -38,7 +38,7 @@ func randomSalt(length int) string { return string(b) } -func CreateUser(username, password, salt string) (*User, error) { +func CreateUser(username, password, salt string, update bool) (*User, error) { if salt == "" { salt = randomSalt(12) } @@ -47,9 +47,27 @@ func CreateUser(username, password, salt string) (*User, error) { Salt: salt, } user.Password = user.encryptPassword(password) - if _, err := DB().NewInsert().Model(&user).Exec(context.Background()); err != nil { + q := DB().NewInsert().Model(&user) + + if update { + q = q.On(`CONFLICT (username) DO UPDATE`). + Set(`"salt" = ?`, user.Salt).Set(`"password"=?`, user.Password) + } + + if _, err := q.Exec(context.Background()); err != nil { log.Errorf("DB Error: %v", err) return nil, err } return &user, nil } + +func DeleteUser(username string) error { + user := User{ + Username: username, + } + if _, err := DB().NewDelete().Model(&user).WherePK().Exec(context.Background()); err != nil { + log.Errorf("DB Error: %v", err) + return err + } + return nil +} diff --git a/mctl/main.go b/mctl/main.go index 0c214a2..58d8377 100644 --- a/mctl/main.go +++ b/mctl/main.go @@ -2,6 +2,8 @@ package main import ( "flag" + "fmt" + "os" log "github.com/sirupsen/logrus" @@ -34,25 +36,69 @@ func initDatabase() { } func main() { + + if len(os.Args) < 2 { + help(os.Args[0]) + return + } + cmd := os.Args[1] + switch cmd { + case "user-add": + userAdd(os.Args[2:]) + case "user-del": + userDel(os.Args[2:]) + default: + help(os.Args[0]) + } + +} + +func userAdd(args []string) { var username, password, salt string - flag.StringVar(&username, "username", "", "Username, can't be duplicated") - flag.StringVar(&password, "password", "", "Plain password") - flag.StringVar(&salt, "salt", "", "Password salt, leave it empty if you want to generate a random one automatically") - flag.Parse() + var update bool + f := flag.NewFlagSet("user-add", flag.ExitOnError) + f.StringVar(&username, "username", "", "Username, can't be duplicated") + f.StringVar(&password, "password", "", "Plain password") + f.BoolVar(&update, "update", false, "update password.") + f.StringVar(&salt, "salt", "", "Password salt, leave it empty if you want to generate a random one automatically") + f.Parse(args) if username == "" || password == "" { - flag.Usage() + f.Usage() return } user, err := db.GetUserByUsername(username) if err != nil { panic(err) - } else if user != nil { + } else if user != nil && !update { log.Infof("user %s already exists", username) return } - user, err = db.CreateUser(username, password, salt) + user, err = db.CreateUser(username, password, salt, update) if err != nil { panic(err) } log.Infof("user %s created", user.Username) } + +func userDel(args []string) { + var username string + f := flag.NewFlagSet("user-add", flag.ExitOnError) + f.StringVar(&username, "username", "", "Username") + f.Parse(args) + if username == "" { + f.Usage() + return + } + if err := db.DeleteUser(username); err != nil { + panic(err) + } + log.Infof("user %s deleted", username) +} + +func help(appname string) { + fmt.Printf("%s [command]\n\n", appname) + fmt.Printf("available commands:\n") + fmt.Printf("\tuser-add\tAdd a user\n") + fmt.Printf("\tuser-del\tDelete a user\n") + fmt.Printf("\n") +} diff --git a/mtop.yaml b/mtop.yaml index c311c29..fe9f20a 100644 --- a/mtop.yaml +++ b/mtop.yaml @@ -1,3 +1,5 @@ +# this is a config template + tls: listen: ":4433" crt: ./keys/server.crt @@ -7,6 +9,6 @@ log: level: "debug" db: - debug: true + debug: false driverName: psql # mysql dsn: "postgres://postgres@127.0.0.1/mtop?sslmode=disable"