From 7da97b1526b573106ec92300758440a746abc99a Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Wed, 29 Mar 2023 16:38:50 -0400 Subject: [PATCH] feat(backend): parse access level --- server/backend/access.go | 16 +++++++++++ server/backend/file/file.go | 53 +++++++++---------------------------- server/backend/server.go | 4 --- 3 files changed, 29 insertions(+), 44 deletions(-) diff --git a/server/backend/access.go b/server/backend/access.go index e13e20fcc..8ed8dbfc3 100644 --- a/server/backend/access.go +++ b/server/backend/access.go @@ -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) + } +} diff --git a/server/backend/file/file.go b/server/backend/file/file.go index f0a72f3b3..aa0465b3f 100644 --- a/server/backend/file/file.go +++ b/server/backend/file/file.go @@ -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 ( @@ -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) { @@ -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. diff --git a/server/backend/server.go b/server/backend/server.go index 2f0c54a49..72198c018 100644 --- a/server/backend/server.go +++ b/server/backend/server.go @@ -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 }