當前位置: 首頁>>代碼示例>>Golang>>正文


Golang db.Conn函數代碼示例

本文整理匯總了Golang中github.com/tsuru/gandalf/db.Conn函數的典型用法代碼示例。如果您正苦於以下問題:Golang Conn函數的具體用法?Golang Conn怎麽用?Golang Conn使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Conn函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TearDownSuite

func (s *S) TearDownSuite(c *gocheck.C) {
    commandmocker.Remove(s.tmpdir)
    conn, err := db.Conn()
    c.Assert(err, gocheck.IsNil)
    defer conn.Close()
    conn.User().Database.DropDatabase()
}
開發者ID:heynemann,項目名稱:gandalf,代碼行數:7,代碼來源:utils_test.go

示例2: handleAssociatedRepositories

func (u *User) handleAssociatedRepositories() error {
    var repos []repository.Repository
    conn, err := db.Conn()
    if err != nil {
        return err
    }
    defer conn.Close()
    if err := conn.Repository().Find(bson.M{"users": u.Name}).All(&repos); err != nil {
        return err
    }
    for _, r := range repos {
        if len(r.Users) == 1 {
            return errors.New("Could not remove user: user is the only one with access to at least one of it's repositories")
        }
    }
    for _, r := range repos {
        for i, v := range r.Users {
            if v == u.Name {
                r.Users[i], r.Users = r.Users[len(r.Users)-1], r.Users[:len(r.Users)-1]
                if err := conn.Repository().Update(bson.M{"_id": r.Name}, r); err != nil {
                    return err
                }
                break
            }
        }
    }
    return nil
}
開發者ID:heynemann,項目名稱:gandalf,代碼行數:28,代碼來源:user.go

示例3: updateKey

func updateKey(name, body, username string) error {
    newK, err := newKey(name, username, body)
    if err != nil {
        return err
    }
    var oldK Key
    conn, err := db.Conn()
    if err != nil {
        return err
    }
    defer conn.Close()
    err = conn.Key().Find(bson.M{"name": name, "username": username}).One(&oldK)
    if err != nil {
        return ErrKeyNotFound
    }
    err = remove(&oldK)
    if err != nil {
        return err
    }
    err = writeKey(newK)
    if err != nil {
        writeKey(&oldK)
        return err
    }
    return conn.Key().Update(bson.M{"name": name, "username": username}, newK)
}
開發者ID:simonoff,項目名稱:gandalf,代碼行數:26,代碼來源:key.go

示例4: Rename

// Rename renames a repository.
func Rename(oldName, newName string) error {
    log.Debugf("Renaming repository %q to %q", oldName, newName)
    repo, err := Get(oldName)
    if err != nil {
        log.Errorf("repository.Rename: Repository %q not found: %s", oldName, err)
        return err
    }
    newRepo := repo
    newRepo.Name = newName
    conn, err := db.Conn()
    if err != nil {
        return err
    }
    defer conn.Close()
    err = conn.Repository().Insert(newRepo)
    if err != nil {
        log.Errorf("repository.Rename: Error adding new repository %q: %s", newName, err)
        return err
    }
    err = conn.Repository().RemoveId(oldName)
    if err != nil {
        log.Errorf("repository.Rename: Error removing old repository %q: %s", oldName, err)
        return err
    }
    return fs.Filesystem().Rename(barePath(oldName), barePath(newName))
}
開發者ID:rochacon,項目名稱:gandalf,代碼行數:27,代碼來源:repository.go

示例5: TestGrantAccessShouldAddUserToListOfRepositories

func (s *S) TestGrantAccessShouldAddUserToListOfRepositories(c *gocheck.C) {
    tmpdir, err := commandmocker.Add("git", "$*")
    c.Assert(err, gocheck.IsNil)
    defer commandmocker.Remove(tmpdir)
    r, err := New("proj1", []string{"someuser"}, true)
    c.Assert(err, gocheck.IsNil)
    conn, err := db.Conn()
    c.Assert(err, gocheck.IsNil)
    defer conn.Close()
    defer conn.Repository().RemoveId(r.Name)
    r2, err := New("proj2", []string{"otheruser"}, true)
    c.Assert(err, gocheck.IsNil)
    defer conn.Repository().RemoveId(r2.Name)
    u := struct {
        Name string `bson:"_id"`
    }{Name: "lolcat"}
    err = conn.User().Insert(&u)
    c.Assert(err, gocheck.IsNil)
    defer conn.User().RemoveId(u.Name)
    err = GrantAccess([]string{r.Name, r2.Name}, []string{u.Name})
    c.Assert(err, gocheck.IsNil)
    err = conn.Repository().FindId(r.Name).One(&r)
    c.Assert(err, gocheck.IsNil)
    err = conn.Repository().FindId(r2.Name).One(&r2)
    c.Assert(err, gocheck.IsNil)
    c.Assert(r.Users, gocheck.DeepEquals, []string{"someuser", u.Name})
    c.Assert(r2.Users, gocheck.DeepEquals, []string{"otheruser", u.Name})
}
開發者ID:kenpu,項目名稱:gandalf,代碼行數:28,代碼來源:repository_test.go

示例6: New

// New creates a representation of a git repository. It creates a Git
// repository using the "bare-dir" setting and saves repository's meta data in
// the database.
func New(name string, users, readOnlyUsers []string, isPublic bool) (*Repository, error) {
    log.Debugf("Creating repository %q", name)
    r := &Repository{Name: name, Users: users, ReadOnlyUsers: readOnlyUsers, IsPublic: isPublic}
    if v, err := r.isValid(); !v {
        log.Errorf("repository.New: Invalid repository %q: %s", name, err)
        return r, err
    }
    conn, err := db.Conn()
    if err != nil {
        return nil, err
    }
    defer conn.Close()
    err = conn.Repository().Insert(r)
    if err != nil {
        if mgo.IsDup(err) {
            return nil, ErrRepositoryAlreadyExists
        }
        return nil, err
    }
    if err = newBare(name); err != nil {
        log.Errorf("repository.New: Error creating bare repository for %q: %s", name, err)
        conn.Repository().Remove(bson.M{"_id": r.Name})
        return r, err
    }
    barePath := barePath(name)
    if barePath != "" && isPublic {
        if f, createErr := fs.Filesystem().Create(barePath + "/git-daemon-export-ok"); createErr == nil {
            f.Close()
        }
    }
    return r, err
}
開發者ID:tsuru,項目名稱:gandalf,代碼行數:35,代碼來源:repository.go

示例7: TestGrantAccessUpdatesReposDocument

func (s *S) TestGrantAccessUpdatesReposDocument(c *gocheck.C) {
    u, err := user.New("pippin", map[string]string{})
    conn, err := db.Conn()
    c.Assert(err, gocheck.IsNil)
    defer conn.Close()
    defer conn.User().Remove(bson.M{"_id": "pippin"})
    c.Assert(err, gocheck.IsNil)
    r := repository.Repository{Name: "onerepo"}
    err = conn.Repository().Insert(&r)
    c.Assert(err, gocheck.IsNil)
    defer conn.Repository().Remove(bson.M{"_id": r.Name})
    r2 := repository.Repository{Name: "otherepo"}
    err = conn.Repository().Insert(&r2)
    c.Assert(err, gocheck.IsNil)
    defer conn.Repository().Remove(bson.M{"_id": r2.Name})
    b := bytes.NewBufferString(fmt.Sprintf(`{"repositories": ["%s", "%s"], "users": ["%s"]}`, r.Name, r2.Name, u.Name))
    rec, req := del("/repository/grant", b, c)
    GrantAccess(rec, req)
    var repos []repository.Repository
    err = conn.Repository().Find(bson.M{"_id": bson.M{"$in": []string{r.Name, r2.Name}}}).All(&repos)
    c.Assert(err, gocheck.IsNil)
    c.Assert(rec.Code, gocheck.Equals, 200)
    for _, repo := range repos {
        c.Assert(repo.Users, gocheck.DeepEquals, []string{u.Name})
    }
}
開發者ID:rochacon,項目名稱:gandalf,代碼行數:26,代碼來源:handler_test.go

示例8: TearDownSuite

func (s *S) TearDownSuite(c *gocheck.C) {
    fs.Fsystem = nil
    conn, err := db.Conn()
    c.Assert(err, gocheck.IsNil)
    defer conn.Close()
    conn.User().Database.DropDatabase()
}
開發者ID:rfloriano,項目名稱:gandalf,代碼行數:7,代碼來源:user_test.go

示例9: New

// New creates a representation of a git repository. It creates a Git
// repository using the "bare-dir" setting and saves repository's meta data in
// the database.
func New(name string, users []string, isPublic bool) (*Repository, error) {
    log.Debugf("Creating repository %q", name)
    r := &Repository{Name: name, Users: users, IsPublic: isPublic}
    if v, err := r.isValid(); !v {
        log.Errorf("repository.New: Invalid repository %q: %s", name, err)
        return r, err
    }
    if err := newBare(name); err != nil {
        log.Errorf("repository.New: Error creating bare repository for %q: %s", name, err)
        return r, err
    }
    barePath := barePath(name)
    if barePath != "" && isPublic {
        ioutil.WriteFile(barePath+"/git-daemon-export-ok", []byte(""), 0644)
        if f, err := fs.Filesystem().Create(barePath + "/git-daemon-export-ok"); err == nil {
            f.Close()
        }
    }
    conn, err := db.Conn()
    if err != nil {
        return nil, err
    }
    defer conn.Close()
    err = conn.Repository().Insert(&r)
    if mgo.IsDup(err) {
        log.Errorf("repository.New: Duplicate repository %q", name)
        return r, fmt.Errorf("A repository with this name already exists.")
    }
    return r, err
}
開發者ID:rochacon,項目名稱:gandalf,代碼行數:33,代碼來源:repository.go

示例10: retrieveRepos

func (s *S) retrieveRepos(r, r2 *repository.Repository, c *gocheck.C) {
    conn, err := db.Conn()
    c.Assert(err, gocheck.IsNil)
    err = conn.Repository().FindId(r.Name).One(&r)
    c.Assert(err, gocheck.IsNil)
    err = conn.Repository().FindId(r2.Name).One(&r2)
    c.Assert(err, gocheck.IsNil)
}
開發者ID:rfloriano,項目名稱:gandalf,代碼行數:8,代碼來源:user_test.go

示例11: createRepo

func (s *S) createRepo(name string, users []string, c *gocheck.C) repository.Repository {
    r := repository.Repository{Name: name, Users: users}
    conn, err := db.Conn()
    c.Assert(err, gocheck.IsNil)
    err = conn.Repository().Insert(&r)
    c.Assert(err, gocheck.IsNil)
    return r
}
開發者ID:rfloriano,項目名稱:gandalf,代碼行數:8,代碼來源:user_test.go

示例12: TestUserUpdateKeyNotFound

func (s *S) TestUserUpdateKeyNotFound(c *check.C) {
    newKey := Key{Name: "somekey", Body: otherKey}
    u, err := New("umi", map[string]string{})
    conn, err := db.Conn()
    c.Assert(err, check.IsNil)
    defer conn.User().RemoveId(u.Name)
    err = UpdateKey("umi", newKey)
    c.Assert(err, check.Equals, ErrKeyNotFound)
}
開發者ID:tsuru,項目名稱:gandalf,代碼行數:9,代碼來源:user_test.go

示例13: GrantAccess

// GrantAccess gives write permission for users in all specified repositories.
// If any of the repositories/users do not exists, GrantAccess just skips it.
func GrantAccess(rNames, uNames []string) error {
    conn, err := db.Conn()
    if err != nil {
        return err
    }
    defer conn.Close()
    _, err = conn.Repository().UpdateAll(bson.M{"_id": bson.M{"$in": rNames}}, bson.M{"$addToSet": bson.M{"users": bson.M{"$each": uNames}}})
    return err
}
開發者ID:rochacon,項目名稱:gandalf,代碼行數:11,代碼來源:repository.go

示例14: RevokeAccess

// RevokeAccess revokes write permission from users in all specified
// repositories.
func RevokeAccess(rNames, uNames []string) error {
    conn, err := db.Conn()
    if err != nil {
        return err
    }
    defer conn.Close()
    _, err = conn.Repository().UpdateAll(bson.M{"_id": bson.M{"$in": rNames}}, bson.M{"$pullAll": bson.M{"users": uNames}})
    return err
}
開發者ID:rochacon,項目名稱:gandalf,代碼行數:11,代碼來源:repository.go

示例15: TestNewUserWihoutKeys

func (s *S) TestNewUserWihoutKeys(c *gocheck.C) {
    b := strings.NewReader(`{"name": "brain"}`)
    recorder, request := post("/user", b, c)
    NewUser(recorder, request)
    conn, err := db.Conn()
    c.Assert(err, gocheck.IsNil)
    defer conn.Close()
    defer conn.User().Remove(bson.M{"_id": "brain"})
    c.Assert(recorder.Code, gocheck.Equals, 200)
}
開發者ID:rochacon,項目名稱:gandalf,代碼行數:10,代碼來源:handler_test.go


注:本文中的github.com/tsuru/gandalf/db.Conn函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。