2024-01-17 06:34:08 +00:00
|
|
|
package navi
|
2023-11-16 15:06:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-01-25 16:11:44 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2023-12-10 03:30:31 +00:00
|
|
|
|
2024-01-24 16:09:39 +00:00
|
|
|
"github.com/pelletier/go-toml/v2"
|
2023-11-16 15:06:59 +00:00
|
|
|
)
|
|
|
|
|
2024-01-24 16:09:39 +00:00
|
|
|
var R *RoadApp
|
2023-11-16 15:06:59 +00:00
|
|
|
|
|
|
|
func ReadInConfig(root string) error {
|
2023-12-12 13:07:05 +00:00
|
|
|
instance := &RoadApp{
|
2024-01-24 16:09:39 +00:00
|
|
|
Regions: make([]*Region, 0),
|
2024-01-25 16:11:44 +00:00
|
|
|
Traces: make([]RoadTrace, 0, viper.GetInt("performance.traces_limit")),
|
2023-11-16 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 16:09:39 +00:00
|
|
|
if err := filepath.Walk(root, func(fp string, info os.FileInfo, _ error) error {
|
|
|
|
var region Region
|
2023-11-16 15:06:59 +00:00
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
2024-01-25 16:11:44 +00:00
|
|
|
} else if !strings.HasSuffix(info.Name(), ".toml") {
|
|
|
|
return nil
|
2023-11-16 15:06:59 +00:00
|
|
|
} else if file, err := os.OpenFile(fp, os.O_RDONLY, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
} else if data, err := io.ReadAll(file); err != nil {
|
|
|
|
return err
|
2024-01-24 16:09:39 +00:00
|
|
|
} else if err := toml.Unmarshal(data, ®ion); err != nil {
|
2023-11-16 15:06:59 +00:00
|
|
|
return err
|
|
|
|
} else {
|
2023-12-10 10:55:13 +00:00
|
|
|
defer file.Close()
|
|
|
|
|
2024-01-24 16:09:39 +00:00
|
|
|
if region.Disabled {
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-25 16:11:44 +00:00
|
|
|
|
2024-01-24 16:09:39 +00:00
|
|
|
instance.Regions = append(instance.Regions, ®ion)
|
2023-11-16 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-24 16:09:39 +00:00
|
|
|
R = instance
|
2023-11-16 15:06:59 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|