2018-10-01 15:43:18 +00:00
|
|
|
package config
|
2018-10-01 10:29:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.xdrm.io/go/aicra/driver"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
// InferFromFolder fills the 'Map' by browsing recursively the
|
|
|
|
// 'Folder' field
|
|
|
|
func (b *builder) InferFromFolder(_root string, _driver driver.Driver) {
|
2018-10-01 10:29:05 +00:00
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
// init map
|
2018-10-01 10:29:05 +00:00
|
|
|
if b.Map == nil {
|
|
|
|
b.Map = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. ignore if no Folder
|
|
|
|
if len(b.Folder) < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. If relative Folder, join to root
|
|
|
|
rootpath := filepath.Join(_root, b.Folder)
|
|
|
|
|
|
|
|
// 3. Walk
|
|
|
|
filepath.Walk(rootpath, func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
|
|
|
// ignore dir
|
|
|
|
if err != nil || info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// format path
|
|
|
|
path, err = filepath.Rel(rootpath, path)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// extract universal path from the driver
|
|
|
|
upath := _driver.Path(_root, b.Folder, path)
|
|
|
|
|
|
|
|
// format name
|
|
|
|
name := upath
|
2018-10-01 17:27:38 +00:00
|
|
|
if name == "/" {
|
2018-10-01 10:29:05 +00:00
|
|
|
name = ""
|
|
|
|
}
|
2018-10-01 17:27:38 +00:00
|
|
|
name = fmt.Sprintf("%s", name)
|
2018-10-01 10:29:05 +00:00
|
|
|
|
|
|
|
// add to map
|
|
|
|
b.Map[name] = upath
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|