forked from switchupcb/copygen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yml.go
57 lines (46 loc) · 1.57 KB
/
yml.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
// Package config loads configuration data from an external file.
package config
import (
"fmt"
"os"
"path/filepath"
"github.com/reedom/copygen/cli/models"
"gopkg.in/yaml.v3"
)
// LoadYML loads a .yml configuration file into a Generator.
func LoadYML(relativepath string) (*models.Generator, error) {
file, err := os.ReadFile(relativepath)
if err != nil {
return nil, fmt.Errorf("the specified .yml filepath doesn't exist: %v\n%w", relativepath, err)
}
var yml YML
if err := yaml.Unmarshal(file, &yml); err != nil {
return nil, fmt.Errorf("an error occurred unmarshalling the .yml file\n%w", err)
}
gen := ParseYML(yml)
// determine the actual filepath of the loader.
absloadpath, err := filepath.Abs(relativepath)
if err != nil {
return nil, fmt.Errorf("an error occurred while determining the absolute file path of the loader file\n%v", relativepath)
}
// determine the actual filepath of the setup.go file.
gen.Setpath = filepath.Join(filepath.Dir(absloadpath), gen.Setpath)
// determine the actual filepath of the template file (if provided).
if gen.Tempath != "" {
gen.Tempath = filepath.Join(filepath.Dir(absloadpath), gen.Tempath)
}
// determine the actual filepath of the output file.
gen.Outpath = filepath.Join(filepath.Dir(absloadpath), gen.Outpath)
return gen, nil
}
// ParseYML parses a YML into a Generator.
func ParseYML(yml YML) *models.Generator {
return &models.Generator{
Setpath: yml.Generated.Setup,
Outpath: yml.Generated.Output,
Tempath: yml.Generated.Template,
Options: models.GeneratorOptions{
Custom: yml.Options,
},
}
}