generated from origadmin/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
150 lines (132 loc) · 3.73 KB
/
load.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (c) 2024 OrigAdmin. All rights reserved.
*/
package bootstrap
import (
"os"
"path/filepath"
"github.com/origadmin/toolkits/codec"
"github.com/origadmin/toolkits/errors"
"github.com/origadmin/runtime"
"github.com/origadmin/runtime/config"
configv1 "github.com/origadmin/runtime/gen/go/config/v1"
)
type decoder = func(string, any) error
// loadSourceConfig loads the config file from the given path
func loadSourceConfig(si os.FileInfo, path string) (*configv1.SourceConfig, error) {
// Check if the file or directory exists
if si == nil {
return nil, errors.New("load config file target is not exist")
}
var cfg configv1.SourceConfig
err := loadCustomizeConfig(si, path, &cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}
// loadCustomizeConfig loads the user config file from the given path
func loadCustomizeConfig(si os.FileInfo, path string, cfg any) error {
// Check if the path is a directory
decode := decodeFile
if si.IsDir() {
decode = decodeDir
}
err := decode(path, cfg)
if err != nil {
return err
}
return nil
}
// decodeFile loads the config file from the given path
func decodeFile(path string, cfg any) error {
// Decode the file into the config struct
if err := codec.DecodeFromFile(path, cfg); err != nil {
return errors.Wrapf(err, "failed to parse config file %s", path)
}
return nil
}
// decodeDir loads the config file from the given directory
func decodeDir(path string, cfg any) error {
found := false
// Walk through the directory and load each file
err := filepath.WalkDir(path, func(walkpath string, d os.DirEntry, err error) error {
if err != nil {
return errors.Wrapf(err, "failed to get config file %s", walkpath)
}
// Check if the path is a directory
if d.IsDir() {
return nil
}
// Decode the file into the config struct
if err := decodeFile(walkpath, cfg); err != nil {
return err
}
found = true
return nil
})
if err != nil {
return errors.Wrap(err, "load config error")
}
if !found {
return errors.New("no config file found in " + path)
}
return nil
}
// LoadSourceConfig loads the config file from the given path
func LoadSourceConfig(bootstrap *Bootstrap) (*configv1.SourceConfig, error) {
// Get the path from the bootstrap
path := bootstrap.WorkPath()
// Get the file info from the path
stat, err := os.Stat(path)
if err != nil {
return nil, errors.Wrap(err, "load config stat error")
}
// Load the config file
return loadSourceConfig(stat, path)
}
// LoadPathSourceConfig loads the config file from the given path
func LoadPathSourceConfig(path string) (*configv1.SourceConfig, error) {
// Get the file info from the path
stat, err := os.Stat(path)
if err != nil {
return nil, errors.Wrap(err, "load config stat error")
}
// Load the config file
return loadSourceConfig(stat, path)
}
// LoadRemoteConfig loads the config file from the given path
func LoadRemoteConfig(bootstrap *Bootstrap, v any, ss ...config.SourceOptionSetting) error {
sourceConfig, err := LoadSourceConfig(bootstrap)
if err != nil {
return err
}
runtimeConfig, err := runtime.NewConfig(sourceConfig, ss...)
if err != nil {
return err
}
if err := runtimeConfig.Load(); err != nil {
return err
}
if err := runtimeConfig.Scan(v); err != nil {
return err
}
return nil
}
// LoadLocalConfig loads the config file from the given path
func LoadLocalConfig(bs *Bootstrap, v any) error {
source, err := LoadSourceConfig(bs)
if err != nil {
return err
}
if source.GetType() != "file" {
return errors.New("local config type must be file")
}
path := source.GetFile().GetPath()
// Get the file info from the path
stat, err := os.Stat(path)
if err != nil {
return errors.Wrap(err, "load config stat error")
}
return loadCustomizeConfig(stat, path, v)
}