gpaste/users/userstore_memory.go
Torjus Håkestad cc4e61f981
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Add gofumpt linter
2022-01-27 11:36:16 +01:00

61 lines
966 B
Go

package users
import (
"fmt"
"sync"
)
var _ UserStore = &MemoryUserStore{}
type MemoryUserStore struct {
users map[string]*User
lock sync.Mutex
}
func NewMemoryUserStore() *MemoryUserStore {
return &MemoryUserStore{users: make(map[string]*User)}
}
func (s *MemoryUserStore) Get(username string) (*User, error) {
s.lock.Lock()
defer s.lock.Unlock()
user, ok := s.users[username]
if !ok {
return nil, fmt.Errorf("no such user: %s", username)
}
return user, nil
}
func (s *MemoryUserStore) Store(user *User) error {
s.lock.Lock()
defer s.lock.Unlock()
s.users[user.Username] = user
return nil
}
func (s *MemoryUserStore) Delete(username string) error {
s.lock.Lock()
defer s.lock.Unlock()
delete(s.users, username)
return nil
}
func (s *MemoryUserStore) List() ([]string, error) {
s.lock.Lock()
defer s.lock.Unlock()
ids := make([]string, 0, len(s.users))
for k := range s.users {
ids = append(ids, k)
}
return ids, nil
}