-
Notifications
You must be signed in to change notification settings - Fork 10
/
repository.go
80 lines (67 loc) · 2.14 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package repository
import (
"os"
"path/filepath"
"github.com/mirzakhany/chapar/internal/domain"
"gopkg.in/yaml.v2"
)
type Repository interface {
LoadCollections() ([]*domain.Collection, error)
GetCollectionsDir() (string, error)
UpdateCollection(collection *domain.Collection) error
DeleteCollection(collection *domain.Collection) error
GetNewCollectionDir(name string) (*FilePath, error)
GetCollectionRequestNewFilePath(collection *domain.Collection, name string) (*FilePath, error)
LoadEnvironments() ([]*domain.Environment, error)
GetEnvironment(filepath string) (*domain.Environment, error)
GetEnvironmentDir() (string, error)
UpdateEnvironment(env *domain.Environment) error
DeleteEnvironment(env *domain.Environment) error
GetNewEnvironmentFilePath(name string) (*FilePath, error)
ReadPreferencesData() (*domain.Preferences, error)
UpdatePreferences(pref *domain.Preferences) error
LoadRequests() ([]*domain.Request, error)
GetRequest(filepath string) (*domain.Request, error)
GetRequestsDir() (string, error)
UpdateRequest(request *domain.Request) error
DeleteRequest(request *domain.Request) error
GetNewRequestFilePath(name string) (*FilePath, error)
}
type FilePath struct {
Path string
NewName string
}
func LoadFromYaml[T any](filename string) (*T, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
env := new(T)
if err := yaml.Unmarshal(data, env); err != nil {
return nil, err
}
return env, nil
}
func SaveToYaml[T any](filename string, data *T) error {
out, err := yaml.Marshal(data)
if err != nil {
return err
}
if err := os.WriteFile(filename, out, 0644); err != nil {
return err
}
return nil
}
// AddSuffixBeforeExt to add a suffix before the file extension
func AddSuffixBeforeExt(filePath, suffix string) string {
dir, file := filepath.Split(filePath)
extension := filepath.Ext(file)
baseName := file[:len(file)-len(extension)]
newBaseName := baseName + suffix + extension
return filepath.Join(dir, newBaseName)
}
func GetFileNameWithoutExt(filePath string) string {
_, file := filepath.Split(filePath)
extension := filepath.Ext(file)
return file[:len(file)-len(extension)]
}