-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
109 lines (91 loc) · 2.37 KB
/
helper.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package core
import (
"fmt"
routing "github.com/qiangxue/fasthttp-routing"
"os"
"path/filepath"
)
type Handler []routing.Handler
var (
MethodGet = []string{"GET"}
MethodPut = []string{"PUT"}
MethodPost = []string{"POST"}
MethodDelete = []string{"DELETE"}
)
func EmptyJson() map[string]interface{} {
return map[string]interface{}{}
}
func (c *Config) Methods() []string {
return []string{"GET,PUT,POST,DELETE"}
}
func (c *Config) Method(method string) []string {
return []string{method}
}
func (c *Config) M(method string) []string {
return c.Method(method)
}
func findFileInDirectory(directoryPath, filename string) (string, error) {
// Check if the directory exists
fileInfo, err := os.Stat(directoryPath)
if err != nil {
return "", err
}
if !fileInfo.IsDir() {
return "", fmt.Errorf("%s is not a directory", directoryPath)
}
// Walk through the directory
var targetFile string
err = filepath.Walk(directoryPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && info.Name() == filename {
targetFile = path
}
return nil
})
if err != nil {
return "", err
}
if targetFile == "" {
return "", fmt.Errorf("file %s not found in directory %s", filename, directoryPath)
}
return targetFile, nil
}
// configLocations returns a slice of possible locations for the configuration file
func configLocations() []string {
var locations []string
// Add current directory
locations = append(locations, ".")
// Add home directory
homeDir, err := os.UserHomeDir()
if err == nil {
locations = append(locations, homeDir)
}
// Add common system-wide locations
commonLocations := []string{
"/etc",
"/usr/local/etc",
}
for _, loc := range commonLocations {
locations = append(locations, loc)
}
// Add executable directory
exePath, err := os.Executable()
if err == nil {
exeDir := filepath.Dir(exePath)
locations = append(locations, exeDir)
}
return locations
}
// findConfigFile searches for the configuration file in the specified locations
func findConfigFile(locations []string, configFilename string) (string, error) {
for _, loc := range locations {
configFile := filepath.Join(loc, configFilename)
_, err := os.Stat(configFile)
if err == nil {
return configFile, nil
}
}
return "", fmt.Errorf("configuration file '%s' not found in any of the locations", configFilename)
}