ListPath enumerates .dat files in a given folder and returns the same folder as an absolute path and a map of files.
(dataPath string)
| 18 | // ListPath enumerates .dat files in a given folder and returns the |
| 19 | // same folder as an absolute path and a map of files. |
| 20 | func ListPath(dataPath string) (string, map[string]string, error) { |
| 21 | dataPath, _ = filepath.Abs(dataPath) |
| 22 | if info, err := os.Stat(dataPath); err != nil { |
| 23 | return "", nil, err |
| 24 | } else if !info.IsDir() { |
| 25 | return "", nil, fmt.Errorf("%s is not a folder", dataPath) |
| 26 | } |
| 27 | |
| 28 | files, err := ioutil.ReadDir(dataPath) |
| 29 | if err != nil { |
| 30 | return "", nil, err |
| 31 | } |
| 32 | |
| 33 | loadable := make(map[string]string) |
| 34 | for _, file := range files { |
| 35 | fileName := file.Name() |
| 36 | fileExt := filepath.Ext(fileName) |
| 37 | if fileExt != DatFileExt { |
| 38 | continue |
| 39 | } |
| 40 | |
| 41 | fileID := strings.Replace(fileName, DatFileExt, "", -1) |
| 42 | loadable[fileID] = filepath.Join(dataPath, fileName) |
| 43 | } |
| 44 | |
| 45 | return dataPath, loadable, nil |
| 46 | } |
| 47 | |
| 48 | // Load reads and deserializes a file into a generic protobuf message. |
| 49 | func Load(fileName string, m proto.Message) error { |