-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloader.go
82 lines (60 loc) · 1.28 KB
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package samb
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/recoye/config"
"gopkg.in/yaml.v2"
)
// Load opens a project based on the filesystem
// path supplied.
func Load(path string) (*Project, error) {
// err will be used to check
// for errors on file load.
var err error
var env = &Project{}
var ext = filepath.Ext(path)
switch ext {
case ".yml":
err = LoadYAML(path, env)
default:
err = LoadSE(path, env)
}
if err != nil {
return nil, err
}
chDirRootOf(path)
env.ProcessImports()
return env, nil
}
// LoadSE loads .se file with SAMB directives.
func LoadSE(path string, p *Project) error {
var conf = config.New(path)
return conf.Unmarshal(p)
}
// LoadYAML loads a YAML file with SAMB
// directives.
func LoadYAML(path string, p *Project) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return yaml.Unmarshal(data, p)
}
// chDirRootOf will change the working directory to the directory
// of the specified SAMB
// source file path.
func chDirRootOf(file string) {
parts := strings.Split(file, "/")
file = strings.Replace(file, parts[len(parts)-1], "", -1)
if file == "" {
file = "./"
}
log.Println("Changing Directory to ", file)
err := os.Chdir(file)
if err != nil {
panic(err)
}
}