Пример #1
0
func userListRun(cmd *Command, args ...string) {
	users := []model.User{}
	db, err := model.Gorm()
	if err != nil {
		log.Fatal(err)
	}
	err = db.Order("name").Order("username").Find(&users).Error
	if err != nil {
		log.Fatal(err)
	}
	idLen, nameLen, unLen := 2, 4, 8
	for _, u := range users {
		l := len(strconv.FormatInt(u.ID, 10))
		if l > idLen {
			idLen = l
		}
		l = len(u.Name)
		if l > nameLen {
			nameLen = l
		}
		l = len(u.Username)
		if l > unLen {
			unLen = l
		}
	}
	fmt.Printf("| %*s | %-*s | %-*s |\n", idLen, "id", unLen, "username", nameLen, "name")
	for _, u := range users {
		fmt.Printf("| %*d | %-*s | %-*s |\n", idLen, u.ID, unLen, u.Username, nameLen, u.Name)
	}
	fmt.Printf("%d user(s)\n", len(users))
}
Пример #2
0
func userDeleteRun(cmd *Command, args ...string) {
	userDeleteCmd.resetFlags()
	userDeleteCmd.checkFlags()
	db, err := model.Gorm()
	db.LogMode(true)
	if err != nil {
		log.Fatal(err)
	}
	user := &model.User{}
	if userDeleteCmd.id != nil {
		user.ID = *userDeleteCmd.id
	} else {
		user.Username = *userDeleteCmd.username
	}
	err = db.Where(user).First(user).Error
	if err != nil {
		log.Fatal(err)
	}
	db.Delete(user)
}
Пример #3
0
func userAddRun(cmd *Command, args ...string) {
	if *userAddCmd.username == "" {
		log.Fatal("A username must be specified")
	}
	passwd := createPassword()
	encPasswd, err := bcrypt.GenerateFromPassword([]byte(passwd), PasswordHashCost)
	if err != nil {
		log.Fatalf("Failed to bcrypt password: %v", err)
	}
	user := model.User{
		Username: *userAddCmd.username,
		Name:     *userAddCmd.name,
		Passhash: string(encPasswd),
	}
	db, err := model.Gorm()
	if err != nil {
		log.Fatal(err)
	}
	err = db.Create(&user).Error
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("created user '%s' (#%d) with initial password '%s'\n", user.Username, user.ID, passwd)
}
Пример #4
0
func demoRun(cmd *Command, args ...string) {
	config := config.LoadConfig()

	fmt.Printf("driver: %v, open: %v\n", config.DriverName, config.DataSourceName)

	db, err := model.Db()
	if err != nil {
		log.Fatalf("connect error: %v\n", err)
	}
	var now time.Time
	err = db.QueryRow("select now()").Scan(&now)
	if err != nil {
		log.Fatalf("query error: %v\n", err)
	}
	fmt.Printf("it's %v\n", now)

	gorm, err := model.Gorm()
	if err != nil {
		log.Fatalf("gorm open error: %v\n", err)
	}
	gorm.LogMode(true)

	tx := gorm.Begin()

	user := model.User{}
	fmt.Println(tx.FirstOrCreate(&user, model.User{
		Name:     "Barney Boisvert",
		Username: "******"}).Error)

	coffee := model.Tag{}
	fmt.Println(tx.FirstOrCreate(&coffee, model.Tag{
		UserID: user.ID,
		Tag:    "coffee"}).Error)
	desk := model.Tag{}
	fmt.Println(tx.FirstOrCreate(&desk, model.Tag{
		UserID: user.ID,
		Tag:    "desk"}).Error)
	for _, t := range []model.Tag{coffee, desk} {
		fmt.Printf("tag %v has id %v\n", t.Tag, t.ID)
	}

	event := model.Event{
		UserID:    user.ID,
		Timestamp: time.Now(),
		Notes:     "i am some notes, yo!"}
	fmt.Println(tx.Create(&event).Error)
	fmt.Printf("event %v\n", event.ID)

	tagRefs := make([]model.EventTag, 2)
	tagRefs[0] = model.EventTag{
		EventID: event.ID,
		TagID:   coffee.ID,
		Number:  2}
	tagRefs[1] = model.EventTag{
		EventID: event.ID,
		TagID:   desk.ID,
		Number:  1}
	for _, tr := range tagRefs {
		fmt.Println(tx.Create(&tr).Error)
		fmt.Printf("event tag %v:%v\n", tr.EventID, tr.TagID)
	}

	events := []model.Event{}
	fmt.Println(tx.Preload("Tags").Preload("Tags.Tag").Find(&events).Error)
	for _, e := range events {
		fmt.Printf("event %v: %v %v\n", e.ID, e.Timestamp, e.Tags)
	}
	tags := events[0].Tags
	fmt.Printf("event %v, &tags: %p, &tags[0]: %p, &tags[1]: %p", events[0].ID, &tags, &(tags[0]), &(tags[1]))
	tx.Commit()
}