Skip to content

Commit

Permalink
users
Browse files Browse the repository at this point in the history
  • Loading branch information
kdudkov committed Apr 21, 2023
1 parent 1960468 commit 944eefa
Showing 1 changed file with 28 additions and 70 deletions.
98 changes: 28 additions & 70 deletions cmd/userctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,92 +46,50 @@ func write(users []*UserInfo) error {
func main() {
users := read()

action := flag.String("a", "", "action")
passwd := flag.String("password", "", "password")
user := flag.String("user", "", "user")

flag.Parse()

switch *action {
case "add":
reader := bufio.NewReader(os.Stdin)

if *user == "" {
fmt.Println("need -user")
return
}
for _, u := range users {
if u.User == *user {
fmt.Println("user exists")
return
}
}

pass := *passwd
if *passwd == "" {
fmt.Print("password: ")
p1, _ := reader.ReadString('\n')
fmt.Print("repeat password: ")
p2, _ := reader.ReadString('\n')

if p1 != p2 {
fmt.Println("\npassword mismatch")
return
}
pass = p1
}
bytes, _ := bcrypt.GenerateFromPassword([]byte(pass), 14)

users = append(users, &UserInfo{User: *user, Password: string(bytes)})
if err := write(users); err != nil {
fmt.Println(err.Error())
if *user == "" {
for _, user := range users {
fmt.Printf("%s\t%s\t%s\t%s\n", user.User, user.Callsign, user.Team, user.Role)
}
return
}

case "change":
pass := *passwd
if pass == "" {
reader := bufio.NewReader(os.Stdin)
fmt.Print("password: ")
p1, _ := reader.ReadString('\n')
fmt.Print("repeat password: ")
p2, _ := reader.ReadString('\n')

if *user == "" {
fmt.Println("need -user")
if p1 != p2 {
fmt.Println("\npassword mismatch")
return
}
pass = p1
}

var cu *UserInfo

for _, u := range users {
if u.User == *user {
cu = u
break
}
}
bpass, _ := bcrypt.GenerateFromPassword([]byte(pass), 14)

if cu == nil {
fmt.Println("user not found")
return
}
var found bool

pass := *passwd
if *passwd == "" {
fmt.Print("password: ")
p1, _ := reader.ReadString('\n')
fmt.Print("repeat password: ")
p2, _ := reader.ReadString('\n')

if p1 != p2 {
fmt.Println("\npassword mismatch")
return
}
pass = p1
}
bytes, _ := bcrypt.GenerateFromPassword([]byte(pass), 14)
cu.Password = string(bytes)
if err := write(users); err != nil {
fmt.Println(err.Error())
for _, u := range users {
if u.User == *user {
found = true
u.Password = string(bpass)
break
}
}

default:
for _, user := range users {
fmt.Printf("%s\t%s\t%s\t%s\n", user.User, user.Callsign, user.Team, user.Role)
}
if !found {
users = append(users, &UserInfo{User: *user, Password: string(bpass)})
}

if err := write(users); err != nil {
fmt.Println(err.Error())
}
}

0 comments on commit 944eefa

Please sign in to comment.