-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreference.go
86 lines (74 loc) · 1.94 KB
/
reference.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
83
84
85
86
package main
import (
"fmt"
"net/url"
"path/filepath"
"strings"
)
const terraformModuleSourceGitPrefix = "git::"
type gitModuleReferenceKey string
type moduleReferenceMap map[gitModuleReferenceKey]moduleReference
type moduleReference struct {
Source string
Version *string
path string
}
type gitModuleReference struct {
Source string
refValue *string
path string
}
func (r gitModuleReference) Vendor() moduleReference {
moduleName := strings.TrimSuffix(filepath.Base(r.Source), ".git")
vendoredSource := "./" + filepath.Join("vendor", "modules", moduleName)
return moduleReference{
Source: vendoredSource,
path: r.path,
}
}
func (r gitModuleReference) TerrafileModule() module {
Version := "master"
if r.refValue != nil {
Version = *r.refValue
}
return module{
Source: r.Source,
Version: Version,
}
}
func (r *moduleReference) Git() (*gitModuleReference, bool) {
if !strings.HasPrefix(r.Source, terraformModuleSourceGitPrefix) {
return nil, false
}
out := gitModuleReference{
Source: strings.TrimPrefix(r.Source, terraformModuleSourceGitPrefix),
path: r.path,
}
sourceURL, err := url.Parse(out.Source)
if err != nil {
return &out, true
}
if refValue := sourceURL.Query().Get("ref"); refValue != "" {
out.refValue = &refValue
sourceURL.RawQuery = ""
out.Source = sourceURL.String()
}
return &out, true
}
func (r moduleReference) String() string {
return fmt.Sprintf("%q (in %s)", r.Source, r.path)
}
func (r *gitModuleReference) Key() gitModuleReferenceKey {
if r.refValue == nil {
return gitModuleReferenceKey(fmt.Sprintf("%s:master", r.Source))
}
return gitModuleReferenceKey(fmt.Sprintf("%s:%v", r.Source, r.refValue))
}
func groupReferencesByDir(references []moduleReference) map[string][]moduleReference {
terrafileDirs := make(map[string][]moduleReference)
for _, r := range references {
dir := filepath.Dir(r.path)
terrafileDirs[dir] = append(terrafileDirs[dir], r)
}
return terrafileDirs
}