forked from urdt/ussd
setup account file interface
This commit is contained in:
parent
23bfdf4483
commit
00d582c017
@ -1,9 +1,6 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
)
|
||||
|
||||
|
||||
|
||||
type AccountFileHandlerInterface interface {
|
||||
@ -14,37 +11,3 @@ type AccountFileHandlerInterface interface {
|
||||
|
||||
|
||||
|
||||
type AccountFileHandler2 struct {
|
||||
FilePath string
|
||||
}
|
||||
|
||||
// Implement the methods required by AccountFileHandlerInterface.
|
||||
func (afh *AccountFileHandler2) EnsureFileExists() error {
|
||||
f, err := os.OpenFile(afh.FilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return f.Close()
|
||||
}
|
||||
func (afh *AccountFileHandler2) ReadAccountData() (map[string]string, error) {
|
||||
jsonData, err := os.ReadFile(afh.FilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var accountData map[string]string
|
||||
err = json.Unmarshal(jsonData, &accountData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return accountData, nil
|
||||
}
|
||||
func (afh *AccountFileHandler2) WriteAccountData(data map[string]string) error {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(afh.FilePath, jsonData, 0644)
|
||||
}
|
Loading…
Reference in New Issue
Block a user