summaryrefslogtreecommitdiff
path: root/store/user.go
diff options
context:
space:
mode:
Diffstat (limited to 'store/user.go')
-rw-r--r--store/user.go34
1 files changed, 12 insertions, 22 deletions
diff --git a/store/user.go b/store/user.go
index d6772c3..7f9eabd 100644
--- a/store/user.go
+++ b/store/user.go
@@ -2,6 +2,7 @@ package store
import (
"encoding/json"
+ "fmt"
log "github.com/sirupsen/logrus"
"os"
)
@@ -17,13 +18,11 @@ type User struct {
// user parses user metadata file.
func (s *Store) user(path string) User {
if payload, err := os.ReadFile(path); err != nil {
- log.Fatalf("Error while reading user metadata %s, %s", path, err)
- log.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error reading user metadata %s, %s", path, err))
} else {
var info User
if err = json.Unmarshal(payload, &info); err != nil {
- log.Fatalf("Error while parsing user metadata %s, %s", path, err)
- log.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error parsing user metadata %s, %s", path, err))
} else {
return info
}
@@ -46,8 +45,7 @@ func (s *Store) User(flake string) User {
func (s *Store) Users() []string {
var users []string
if entries, err := os.ReadDir(s.UsersDir()); err != nil {
- log.Fatalf("Error while reading users, %s", err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error reading users, %s", err))
} else {
for _, entry := range entries {
if entry.IsDir() {
@@ -61,12 +59,10 @@ func (s *Store) Users() []string {
// UserMetadata sets user metadata.
func (s *Store) UserMetadata(info User) {
if payload, err := json.Marshal(info); err != nil {
- log.Fatalf("Error while updating user %s metadata, %s", info.Snowflake, err)
- log.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error updating user %s metadata, %s", info.Snowflake, err))
} else {
if err = os.WriteFile(s.UserMetadataPath(info.Snowflake), payload, s.PermissionFile); err != nil {
- log.Fatalf("Error while writing user %s metadata, %s", info.Snowflake, err)
- log.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error writing user %s metadata, %s", info.Snowflake, err))
}
}
}
@@ -85,8 +81,7 @@ func (s *Store) UserAdd(username string, privileged bool, password string) User
}
// Create user directory and images
if err := os.MkdirAll(s.UserImagesPath(info.Snowflake), s.PermissionDir); err != nil {
- log.Fatalf("Error while creating user %s directory, %s", info.Snowflake, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error creating user %s directory, %s", info.Snowflake, err))
}
s.getLock(info.Snowflake).Lock()
defer s.getLock(info.Snowflake).Unlock()
@@ -196,8 +191,7 @@ func (s *Store) userUsernameAssociate(flake, username string) {
// userUsernameDisassociate disassociates specific username.
func (s *Store) userUsernameDisassociate(username string) {
if err := os.Remove(s.UsernamePath(username)); err != nil {
- log.Fatalf("Error disassociating username %s, %s", username, err)
- log.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error disassociating username %s, %s", username, err))
}
}
@@ -207,8 +201,7 @@ func (s *Store) userPassword(path string) string {
if os.IsNotExist(err) {
return ""
}
- log.Fatalf("Error reading password from user directory %s, %s", path, err)
- log.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error reading password from user directory %s, %s", path, err))
} else {
return string(payload)
}
@@ -221,8 +214,7 @@ func (s *Store) userPasswordUpdate(path, password string) bool {
return false
}
if err := os.WriteFile(path+"/passwd", []byte(password), s.PermissionFile); err != nil {
- log.Fatalf("Error setting password for user directory %s, %s", path, err)
- log.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error setting password for user directory %s, %s", path, err))
} else {
return true
}
@@ -295,8 +287,7 @@ func (s *Store) UserDestroy(flake string) {
s.SecretDisassociate(info.Snowflake)
// Remove user data directory
if err := os.RemoveAll(s.UserPath(flake)); err != nil {
- log.Fatalf("Error destroying user %s data directory, %s", flake, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error destroying user %s data directory, %s", flake, err))
}
log.Infof("User %s username %s destroyed.", info.Snowflake, info.Username)
}
@@ -312,8 +303,7 @@ func (s *Store) UserImages(flake string) []string {
var images []string
if entries, err := os.ReadDir(s.UserImagesPath(flake)); err != nil {
- log.Fatalf("Error while reading user %s images, %s", flake, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error reading user %s images, %s", flake, err))
} else {
for _, entry := range entries {
images = append(images, entry.Name())