7da97b1526b573106ec92300758440a746abc99a

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

feat(backend): parse access level

Diff

  1diff --git a/server/backend/access.go b/server/backend/access.go
  2index e13e20fcc5e175ca68f633339a36b293ce39adb9..8ed8dbfc339d489a5464feeaf975bc762f10d2fa 100644
  3--- a/server/backend/access.go
  4+++ b/server/backend/access.go
  5@@ -32,3 +32,19 @@ func (a AccessLevel) String() string {
  6 		return "unknown"
  7 	}
  8 }
  9+
 10+// ParseAccessLevel parses an access level string.
 11+func ParseAccessLevel(s string) AccessLevel {
 12+	switch s {
 13+	case "no-access":
 14+		return NoAccess
 15+	case "read-only":
 16+		return ReadOnlyAccess
 17+	case "read-write":
 18+		return ReadWriteAccess
 19+	case "admin-access":
 20+		return AdminAccess
 21+	default:
 22+		return AccessLevel(-1)
 23+	}
 24+}
 25diff --git a/server/backend/file/file.go b/server/backend/file/file.go
 26index f0a72f3b38a43d4165d5bee40eb5da08c21f9b18..aa0465b3f44b64c8d7f04f63ccab5ab8cfcf1e55 100644
 27--- a/server/backend/file/file.go
 28+++ b/server/backend/file/file.go
 29@@ -39,16 +39,15 @@ import (
 30 
 31 // sub file and directory names.
 32 const (
 33-	anonAccess    = "anon-access"
 34-	defaultBranch = "default-branch"
 35-	allowKeyless  = "allow-keyless"
 36-	admins        = "admins"
 37-	repos         = "repos"
 38-	collabs       = "collaborators"
 39-	description   = "description"
 40-	exportOk      = "git-daemon-export-ok"
 41-	private       = "private"
 42-	settings      = "settings"
 43+	anonAccess   = "anon-access"
 44+	allowKeyless = "allow-keyless"
 45+	admins       = "admins"
 46+	repos        = "repos"
 47+	collabs      = "collaborators"
 48+	description  = "description"
 49+	exportOk     = "git-daemon-export-ok"
 50+	private      = "private"
 51+	settings     = "settings"
 52 )
 53 
 54 var (
 55@@ -128,7 +127,7 @@ func NewFileBackend(path string) (*FileBackend, error) {
 56 		}
 57 	}
 58 
 59-	for _, file := range []string{admins, anonAccess, allowKeyless, defaultBranch} {
 60+	for _, file := range []string{admins, anonAccess, allowKeyless} {
 61 		fp := filepath.Join(fb.settingsPath(), file)
 62 		_, err := os.Stat(fp)
 63 		if errors.Is(err, fs.ErrNotExist) {
 64@@ -433,38 +432,12 @@ func (fb *FileBackend) AnonAccess() backend.AccessLevel {
 65 		return backend.NoAccess
 66 	}
 67 
 68-	switch line {
 69-	case backend.NoAccess.String():
 70-		return backend.NoAccess
 71-	case backend.ReadOnlyAccess.String():
 72-		return backend.ReadOnlyAccess
 73-	case backend.ReadWriteAccess.String():
 74-		return backend.ReadWriteAccess
 75-	case backend.AdminAccess.String():
 76-		return backend.AdminAccess
 77-	default:
 78+	al := backend.ParseAccessLevel(line)
 79+	if al < 0 {
 80 		return backend.NoAccess
 81 	}
 82-}
 83 
 84-// DefaultBranch returns the default branch for new repositories.
 85-//
 86-// It implements backend.Backend.
 87-func (fb *FileBackend) DefaultBranch() string {
 88-	line, err := readOneLine(filepath.Join(fb.settingsPath(), defaultBranch))
 89-	if err != nil {
 90-		logger.Debug("failed to read default-branch file", "err", err)
 91-		return defaults[defaultBranch]
 92-	}
 93-
 94-	return line
 95-}
 96-
 97-// SetDefaultBranch sets the default branch for new repositories.
 98-//
 99-// It implements backend.Backend.
100-func (fb *FileBackend) SetDefaultBranch(branch string) error {
101-	return os.WriteFile(filepath.Join(fb.settingsPath(), defaultBranch), []byte(branch), 0600)
102+	return al
103 }
104 
105 // Description returns the description of the given repo.
106diff --git a/server/backend/server.go b/server/backend/server.go
107index 2f0c54a497a3aefce6167f11ba75c37a961931df..72198c018d445afabc5b2a5e049e742894c1fb5c 100644
108--- a/server/backend/server.go
109+++ b/server/backend/server.go
110@@ -10,8 +10,4 @@ type ServerBackend interface {
111 	AllowKeyless() bool
112 	// SetAllowKeyless sets whether or not keyless access is allowed.
113 	SetAllowKeyless(allow bool) error
114-	// DefaultBranch returns the default branch for new repositories.
115-	DefaultBranch() string
116-	// SetDefaultBranch sets the default branch for new repositories.
117-	SetDefaultBranch(branch string) error
118 }