Skip to content

Commit

Permalink
feat(backend): parse access level
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2023
1 parent 37fbfa2 commit 7da97b1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 44 deletions.
16 changes: 16 additions & 0 deletions server/backend/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@ func (a AccessLevel) String() string {
return "unknown"
}
}

// ParseAccessLevel parses an access level string.
func ParseAccessLevel(s string) AccessLevel {
switch s {
case "no-access":
return NoAccess
case "read-only":
return ReadOnlyAccess
case "read-write":
return ReadWriteAccess
case "admin-access":
return AdminAccess
default:
return AccessLevel(-1)
}
}
53 changes: 13 additions & 40 deletions server/backend/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ import (

// sub file and directory names.
const (
anonAccess = "anon-access"
defaultBranch = "default-branch"
allowKeyless = "allow-keyless"
admins = "admins"
repos = "repos"
collabs = "collaborators"
description = "description"
exportOk = "git-daemon-export-ok"
private = "private"
settings = "settings"
anonAccess = "anon-access"
allowKeyless = "allow-keyless"
admins = "admins"
repos = "repos"
collabs = "collaborators"
description = "description"
exportOk = "git-daemon-export-ok"
private = "private"
settings = "settings"
)

var (
Expand Down Expand Up @@ -128,7 +127,7 @@ func NewFileBackend(path string) (*FileBackend, error) {
}
}

for _, file := range []string{admins, anonAccess, allowKeyless, defaultBranch} {
for _, file := range []string{admins, anonAccess, allowKeyless} {
fp := filepath.Join(fb.settingsPath(), file)
_, err := os.Stat(fp)
if errors.Is(err, fs.ErrNotExist) {
Expand Down Expand Up @@ -433,38 +432,12 @@ func (fb *FileBackend) AnonAccess() backend.AccessLevel {
return backend.NoAccess
}

switch line {
case backend.NoAccess.String():
return backend.NoAccess
case backend.ReadOnlyAccess.String():
return backend.ReadOnlyAccess
case backend.ReadWriteAccess.String():
return backend.ReadWriteAccess
case backend.AdminAccess.String():
return backend.AdminAccess
default:
al := backend.ParseAccessLevel(line)
if al < 0 {
return backend.NoAccess
}
}

// DefaultBranch returns the default branch for new repositories.
//
// It implements backend.Backend.
func (fb *FileBackend) DefaultBranch() string {
line, err := readOneLine(filepath.Join(fb.settingsPath(), defaultBranch))
if err != nil {
logger.Debug("failed to read default-branch file", "err", err)
return defaults[defaultBranch]
}

return line
}

// SetDefaultBranch sets the default branch for new repositories.
//
// It implements backend.Backend.
func (fb *FileBackend) SetDefaultBranch(branch string) error {
return os.WriteFile(filepath.Join(fb.settingsPath(), defaultBranch), []byte(branch), 0600)
return al
}

// Description returns the description of the given repo.
Expand Down
4 changes: 0 additions & 4 deletions server/backend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@ type ServerBackend interface {
AllowKeyless() bool
// SetAllowKeyless sets whether or not keyless access is allowed.
SetAllowKeyless(allow bool) error
// DefaultBranch returns the default branch for new repositories.
DefaultBranch() string
// SetDefaultBranch sets the default branch for new repositories.
SetDefaultBranch(branch string) error
}

0 comments on commit 7da97b1

Please sign in to comment.