Skip to content

Commit

Permalink
mctl
Browse files Browse the repository at this point in the history
  • Loading branch information
wikylyu committed Feb 3, 2023
1 parent 58e19de commit a50f781
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
/keys
/bin
/vendor
/dist
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
CGO_ENABLED=0 go build -o bin/mctl mctl/main.go

dist: mtop climber mctl
tar -C bin/ -jcvf dist/mtop_linux64.tar.bz2 .
22 changes: 20 additions & 2 deletions db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
60 changes: 53 additions & 7 deletions mctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"flag"
"fmt"
"os"

log "github.com/sirupsen/logrus"

Expand Down Expand Up @@ -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")
}
4 changes: 3 additions & 1 deletion mtop.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# this is a config template

tls:
listen: ":4433"
crt: ./keys/server.crt
Expand All @@ -7,6 +9,6 @@ log:
level: "debug"

db:
debug: true
debug: false
driverName: psql # mysql
dsn: "postgres://[email protected]/mtop?sslmode=disable"

0 comments on commit a50f781

Please sign in to comment.