-
Notifications
You must be signed in to change notification settings - Fork 2
/
provider.go
159 lines (133 loc) · 4.02 KB
/
provider.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package terraform
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/spf13/viper"
)
type ProviderName struct {
namespace string
providerType string
version string
}
func NewProviderName(namespace, providerType, version string) *ProviderName {
return &ProviderName{
namespace: strings.ToLower(namespace),
providerType: strings.ToLower(providerType),
version: strings.ToLower(version),
}
}
func ParseProviderName(s string) (*ProviderName, error) {
parts := strings.Split(s, "/")
typeWithVersion := parts[len(parts)-1]
typeWithVersionParts := strings.Split(typeWithVersion, "@")
if len(parts) != 2 || len(typeWithVersionParts) > 2 {
return nil, errors.New("incorrect provider name format, expected '<namespace>/<type>[@<version>]'")
}
// Get Namespace
namespace := strings.TrimSpace(parts[0])
if namespace == "" {
return nil, errors.New("invalid provider namespace, expected non-empty value")
}
// Get Type
providerType := strings.TrimSpace(typeWithVersionParts[0])
if providerType == "" {
return nil, errors.New("invalid provider type, expected non-empty value")
}
// Get Version
version := "latest"
if len(typeWithVersionParts) > 1 {
version = strings.TrimSpace(typeWithVersionParts[1])
if version == "" {
return nil, errors.New("invalid provider version, expected non-empty value")
}
}
return NewProviderName(namespace, providerType, version), nil
}
type Provider struct {
name *ProviderName
operatingSystem string
architecture string
}
func NewProvider(name *ProviderName, os, arch string) *Provider {
return &Provider{
name: name,
operatingSystem: strings.ToLower(os),
architecture: strings.ToLower(arch),
}
}
func ParseProviderFromPath(path string) (*Provider, error) {
// Get parts
parts := strings.Split(path, string(os.PathSeparator))
// Verify path format
pattern := `^.*\/[^\/]+\/[^\/]+\/[^\/]+\/\d+\.\d+\.\d+\/[^_]+_[^\/]+$`
match, err := regexp.MatchString(pattern, path)
if err != nil {
return nil, err
}
if !match && runtime.GOOS != "windows" {
return nil, errors.New("invalid path format, expected something like '.../<namespace>/<type>/<version>/<os>_<arch>'")
}
// Get provider data
os := strings.Split(parts[len(parts)-1], "_")[0]
arch := strings.Split(parts[len(parts)-1], "_")[1]
version := parts[len(parts)-2]
providerType := parts[len(parts)-3]
namespace := parts[len(parts)-4]
providerName := NewProviderName(namespace, providerType, version)
return NewProvider(providerName, os, arch), nil
}
func (p *Provider) Namespace() string {
return p.name.namespace
}
func (p *Provider) ProviderType() string {
return p.name.providerType
}
func (p *Provider) Version() string {
return p.name.version
}
func (p *Provider) SetVersion(version string) {
p.name.version = version
}
func (p *Provider) OperatingSystem() string {
return p.operatingSystem
}
func (p *Provider) Architecture() string {
return p.architecture
}
func (p Provider) String() string {
return fmt.Sprintf("'%s/%s@%s' (%s/%s)", p.name.namespace, p.name.providerType, p.name.version, p.operatingSystem, p.architecture)
}
func (p Provider) InstallationPath() string {
return filepath.Join(
viper.GetString("terraform_plugin_cache_dir"),
viper.GetString("terraform_registry"),
p.name.namespace,
p.name.providerType,
p.name.version,
fmt.Sprintf("%s_%s", p.operatingSystem, p.architecture),
)
}
func (p *Provider) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Namespace string `json:"namespace"`
ProviderType string `json:"name"`
Version string `json:"version"`
OperatingSystem string `json:"os"`
Architecture string `json:"arch"`
}{
Namespace: p.Namespace(),
ProviderType: p.ProviderType(),
Version: p.Version(),
OperatingSystem: p.OperatingSystem(),
Architecture: p.Architecture(),
})
}
func (p *Provider) ToOutputRow() []string {
return []string{p.Namespace(), p.ProviderType(), p.Version(), p.OperatingSystem(), p.Architecture()}
}