user.go (655B)
1 package store 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 8 "code.dwrz.net/src/pkg/dqs/user" 9 ) 10 11 const userFile = "user.json" 12 13 func (s *Store) GetUser() (*user.User, error) { 14 name := fmt.Sprintf("%s/%s", s.Dir, userFile) 15 16 data, err := os.ReadFile(name) 17 if err != nil { 18 return nil, err 19 } 20 21 var u = &user.User{} 22 if err := json.Unmarshal(data, u); err != nil { 23 return nil, err 24 } 25 26 return u, nil 27 } 28 29 func (s *Store) UpdateUser(u *user.User) error { 30 data, err := json.Marshal(u) 31 if err != nil { 32 return err 33 } 34 35 name := fmt.Sprintf("%s/%s", s.Dir, userFile) 36 37 if err := os.WriteFile(name, data, permissions); err != nil { 38 return err 39 } 40 41 return nil 42 }