-
Notifications
You must be signed in to change notification settings - Fork 1
/
folder.go
57 lines (43 loc) · 1.34 KB
/
folder.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
package folder
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/alexfalkowski/go-service/meta"
"github.com/alexfalkowski/konfig/source/configurator/errors"
)
// NewConfigurator for folder.
func NewConfigurator(cfg Config) *Configurator {
return &Configurator{cfg: cfg}
}
// Configurator for folder.
type Configurator struct {
cfg Config
}
// GetConfig for folder.
func (c *Configurator) GetConfig(ctx context.Context, app, ver, env, continent, country, cmd string) ([]byte, error) {
if _, err := os.Stat(c.cfg.Dir); os.IsNotExist(err) {
meta.WithAttribute(ctx, "folder.dir_error", err.Error())
return nil, err
}
path := filepath.Join(c.cfg.Dir, c.path(app, ver, env, continent, country, cmd))
data, err := os.ReadFile(filepath.Clean(path))
if err != nil {
meta.WithAttribute(ctx, "folder.file_error", err.Error())
if os.IsNotExist(err) {
return nil, errors.ErrNotFound
}
return nil, err
}
return data, nil
}
func (c *Configurator) path(app, ver, env, continent, country, cmd string) string {
if continent == "*" && country == "*" {
return fmt.Sprintf("%s/%s/%s/%s.config.yml", app, ver, env, cmd)
}
if continent != "*" && country == "*" {
return fmt.Sprintf("%s/%s/%s/%s/%s.config.yml", app, ver, env, continent, cmd)
}
return fmt.Sprintf("%s/%s/%s/%s/%s/%s.config.yml", app, ver, env, continent, country, cmd)
}