示例#1
0
func delette(args []string) {
	id := checkOneArg("delete", args)

	problems, err := db.GetDB("problems")
	if err != nil {
		_errx("Cannot get db 'problems': %s\n", err)
	}
	var P eval.Problem
	rev, err := problems.Get(id, &P)
	if err != nil {
		_errx("Couldn't get problem '%s': %s\n", id, err)
	}

	// Store in 'problems-deleted'
	delproblems, err := db.GetOrCreateDB("problems-deleted")
	if err != nil {
		_errx("Cannot get db 'problems-deleted'")
	}
	salt := db.RandString(8)
	err = delproblems.Put(id+"-"+salt, &P)
	if err != nil {
		_errx("Cannot backup deleted problem '%s': %s\n", id, err)
	}

	// Delete
	err = problems.Delete(id, rev)
	if err != nil {
		_errx("Couldn't delete problem '%s': %s\n", id, err)
	}

	fmt.Printf("Problem '%s' deleted\n", id)
}
示例#2
0
func adduser(args []string) {
	login := checkOneArg("adduser", args)

	// Get DB
	users, err := db.GetOrCreateDB("users")
	if err != nil {
		_errx("Cannot connect to 'users' database: %s\n", err)
	}

	// Check if user exists
	rev, err := users.Rev(login)
	if err != nil {
		_errx("Cannot get rev for user '%s'", login, err)
	}
	if rev != "" {
		_errx("User '%s' is already in the database", login)
	}

	// Disable terminal echo
	ToggleEcho()
	defer ToggleEcho()

	// Get password (two times)
	var passwd [2]string
	for i := range passwd {
		if i == 1 {
			fmt.Printf("(repeat) ")
		}
		fmt.Printf("Password: "******"%s", &passwd[i])
		fmt.Printf("\n") // no echo -> no endl
	}
	if passwd[0] != passwd[1] {
		_errx("Passwords do not match")
	}

	hash, err := bcrypt.GenerateFromPassword([]byte(passwd[0]), 10)
	if err != nil {
		_errx("Cannot hash password: %s\n", err)
	}

	err = users.Put(login, &db.User{
		Login:   login,
		Hpasswd: string(hash),
	})
	if err != nil {
		_errx("Cannot save user '%s': %s\n", login, err)
	}

	fmt.Printf("Ok\n")
}
示例#3
0
func _addupdate(dir string) error {
	id, Problem, err := eval.ReadFromDir(dir)
	if err != nil {
		return fmt.Errorf("Cannot read problem at '%s': %s\n", dir, err)
	}

	// Store in the database
	problems, err := db.GetOrCreateDB("problems")
	if err != nil {
		_errx("Cannot get db 'problems': %s\n", err)
	}
	if err := storeFunc(problems, id, Problem); err != nil {
		return err
	}
	return nil
}
示例#4
0
func TestStoreProblem(t *testing.T) {
	const dbname = "this-database-shouldn-exist-at-all-in-the-face-of-the-earth-42"

	D, err := db.GetOrCreateDB(dbname)
	if err != nil {
		t.Errorf("Cannot get or create database: %s\n", err)
		return
	}

	const pid = "Cpp.Intro.SumaEnteros"

	// Put
	rev, err := D.Rev(pid)
	if rev != "" {
		t.Fatalf("Un-f****n'-believable: this problem already existed!")
	}
	err = D.Put(pid, filesProb)
	if err != nil {
		t.Errorf("Cannot put: %s\n", err)
	}

	// Get
	var obj eval.Problem
	rev, err = D.Get(pid, &obj)
	if err != nil {
		t.Errorf("Cannot get: %s\n", err)
	}
	if !reflect.DeepEqual(filesProb, &obj) {
		fmt.Printf("%#v\n", filesProb)
		fmt.Printf("%#v\n", obj)
		t.Errorf("Different data\n")
	}

	// Delete
	if err := D.Delete(pid, rev); err != nil {
		t.Errorf("Cannot delete '%s': %s\n", pid, err)
	}

	db.DeleteDB(D)
}