diff --git a/cmd/userctl/main.go b/cmd/userctl/main.go index 1512290..3a9bedc 100644 --- a/cmd/userctl/main.go +++ b/cmd/userctl/main.go @@ -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()) + } }