-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbedrock.go
More file actions
80 lines (74 loc) · 1.67 KB
/
bedrock.go
File metadata and controls
80 lines (74 loc) · 1.67 KB
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
package main
import (
"errors"
"github.com/codegangsta/cli"
"os"
"path/filepath"
)
type Bedrock struct {
Config Config
OutputDirPath string
Inputs []string
InputType InputType
}
func NewBedrock(configFilePath string, inputType string, outDir string, args cli.Args) (Bedrock, error) {
var b Bedrock
wd, err := os.Getwd()
if err != nil {
return b, err
}
b.InputType = StringToInputMode(inputType)
// create & register config
if configFilePath == "" {
return b, errors.New("config flag must be specified")
} else if !filepath.IsAbs(configFilePath) {
configFilePath = filepath.Join(wd, configFilePath)
}
config, err := NewConfig(configFilePath)
if err != nil {
return b, err
}
b.Config = config
// register out dir if specified
if outDir != "" && !filepath.IsAbs(outDir) {
outDir = filepath.Join(wd, outDir)
}
b.OutputDirPath = outDir
// register inputs
inputs, err := createInputs(args, b.InputType.extNames(), wd)
if err != nil {
return b, err
}
b.Inputs = inputs
return b, nil
}
func (c Bedrock) OutputsFiles() bool {
return c.OutputDirPath != ""
}
func createInputs(args cli.Args, allowExts []string, wd string) ([]string, error) {
res := []string{}
for _, arg := range args {
files, err := filepath.Glob(arg)
if err != nil {
return res, err
}
for _, file := range files {
info, err := os.Stat(file)
if err != nil {
return res, err
}
if !info.IsDir() && contains(allowExts, filepath.Ext(file)) {
res = append(res, filepath.Join(wd, file))
}
}
}
return res, nil
}
func contains(list []string, target string) bool {
for _, val := range list {
if val == target {
return true
}
}
return false
}