-
Notifications
You must be signed in to change notification settings - Fork 52
/
module.go
81 lines (69 loc) · 2.15 KB
/
module.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
package module
import (
"crypto/md5"
"encoding/base64"
"io"
"log"
"path"
"regexp"
"sync"
"github.com/commitdev/zero/internal/config"
"github.com/commitdev/zero/internal/config/moduleconfig"
"github.com/commitdev/zero/internal/constants"
"github.com/commitdev/zero/internal/util"
"github.com/commitdev/zero/pkg/util/exit"
"github.com/hashicorp/go-getter"
)
// TemplateModule merges a module instance params with the static configs
type TemplateModule struct {
config.ModuleInstance // @TODO Move this
Config moduleconfig.ModuleConfig
}
// FetchModule downloads the remote module source if necessary. Meant to be run in a goroutine.
func FetchModule(source string, wg *sync.WaitGroup) {
defer wg.Done()
localPath := GetSourceDir(source)
if !isLocal(source) {
err := getter.Get(localPath, source)
if err != nil {
exit.Fatal("Failed to fetch remote module from %s: %v\n", source, err)
}
}
return
}
// ParseModuleConfig loads the local config file for a module and parses the yaml
func ParseModuleConfig(source string) (moduleconfig.ModuleConfig, error) {
localPath := GetSourceDir(source)
config := moduleconfig.ModuleConfig{}
configPath := path.Join(localPath, constants.ZeroModuleYml)
config, err := moduleconfig.LoadModuleConfig(configPath)
return config, err
}
// GetSourcePath gets a unique local source directory name. For local modules, it use the local directory
func GetSourceDir(source string) string {
if !isLocal(source) {
h := md5.New()
io.WriteString(h, source)
source = base64.StdEncoding.EncodeToString(h.Sum(nil))
return path.Join(constants.TemplatesDir, source)
} else {
return source
}
}
// IsLocal uses the go-getter FileDetector to check if source is a file
func isLocal(source string) bool {
pwd := util.GetCwd()
// ref: https://github.com/hashicorp/go-getter/blob/master/detect_test.go
out, err := getter.Detect(source, pwd, getter.Detectors)
match, err := regexp.MatchString("^file://.*", out)
if err != nil {
log.Panicf("invalid source format %s", err)
}
return match
}
func withPWD(pwd string) func(*getter.Client) error {
return func(c *getter.Client) error {
c.Pwd = pwd
return nil
}
}