126 lines
2.3 KiB
Go
126 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
type Config struct {
|
|
Host string `yaml:"Host"`
|
|
Port int `yaml:"Port"`
|
|
}
|
|
|
|
type Result struct {
|
|
FolderName string
|
|
Host string
|
|
Port int
|
|
PrefixRoute map[string]bool
|
|
}
|
|
|
|
func GetZeroInfo(rootDir string) (results []*Result) {
|
|
entries, err := ioutil.ReadDir(rootDir)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
if entry.IsDir() {
|
|
|
|
result, err := findFoldersAndExtractInfo(rootDir, entry)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
results = append(results, result)
|
|
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func findFoldersAndExtractInfo(rootDir string, entry os.FileInfo) (*Result, error) {
|
|
var result *Result
|
|
|
|
folderName := entry.Name()
|
|
path := filepath.Join(rootDir, folderName)
|
|
|
|
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
relPath, err := filepath.Rel(path, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if strings.Contains(relPath, string(os.PathSeparator)) {
|
|
return filepath.SkipDir
|
|
}
|
|
|
|
configPath := filepath.Join(path, "etc", folderName+".yaml")
|
|
routesPath := filepath.Join(path, "internal", "handler", "routes.go")
|
|
|
|
configContent, err := ioutil.ReadFile(configPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var config Config
|
|
err = yaml.Unmarshal(configContent, &config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
routesContent, err := ioutil.ReadFile(routesPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
PrefixRoute := extractPrefixRouteValues(string(routesContent))
|
|
|
|
result = &Result{
|
|
FolderName: folderName,
|
|
Host: config.Host,
|
|
Port: config.Port,
|
|
PrefixRoute: PrefixRoute,
|
|
}
|
|
|
|
return filepath.SkipDir
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func extractPrefixRouteValues(content string) map[string]bool {
|
|
lines := strings.Split(content, "\n")
|
|
var prefixPath map[string]bool = make(map[string]bool)
|
|
|
|
for _, line := range lines {
|
|
if strings.Contains(line, "Path:") {
|
|
path := strings.TrimSpace(strings.TrimPrefix(line, "Path:"))
|
|
path1 := strings.Split(strings.Trim(path, `"`), "/")[1]
|
|
path1 = "/" + path1
|
|
if _, ok := prefixPath[path1]; !ok {
|
|
prefixPath[path1] = true
|
|
}
|
|
}
|
|
}
|
|
|
|
return prefixPath
|
|
}
|