gochart/chart_parser.go

66 lines
1.1 KiB
Go
Raw Normal View History

2014-12-26 20:25:03 +08:00
package main
import (
"errors"
2014-12-26 20:25:03 +08:00
"github.com/zieckey/goini"
"os"
"path/filepath"
2017-03-10 19:27:36 +08:00
"log"
2014-12-26 20:25:03 +08:00
)
const DataPrefix = "Data|"
type ChartIf interface {
Parse(ini *goini.INI) (map[string]string, error)
Template() string
}
type TemplateArgs struct {
args map[string]string
tmpl string
}
func Parse(file string) (tt TemplateArgs, err error) {
2014-12-26 20:25:03 +08:00
ini := goini.New()
err = ini.ParseFile(file)
if err != nil {
return tt, err
2014-12-26 20:25:03 +08:00
}
t, _ := ini.Get("ChartType")
if f, ok := ChartHandlers[t]; ok {
tt.args, err = f.Parse(ini)
tt.tmpl = f.Template()
2014-12-26 20:25:03 +08:00
}
return tt, err
2014-12-26 20:25:03 +08:00
}
func LookupChartFiles(dir string) ([]string, error) {
var files []string = make([]string, 0, 5)
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if f.IsDir() {
return nil
}
if ok, err := filepath.Match("*.chart", f.Name()); err != nil {
return err
} else if ok {
2017-03-10 19:27:36 +08:00
log.Printf("Add chart file %v", path)
2014-12-26 20:25:03 +08:00
files = append(files, path)
}
return nil
})
if len(files) == 0 {
return files, errors.New("Not found any *.chart files")
}
2014-12-26 20:25:03 +08:00
return files, err
}