Skip to content
This repository was archived by the owner on Jun 11, 2021. It is now read-only.

Commit f1c697e

Browse files
committed
Move getter out of install command
1 parent fd76f76 commit f1c697e

File tree

2 files changed

+120
-92
lines changed

2 files changed

+120
-92
lines changed

cmd/install.go

Lines changed: 2 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@
2121
package cmd
2222

2323
import (
24-
"net/url"
2524
"os"
2625
"path"
27-
"strings"
2826
"sync"
2927

3028
"github.com/otiai10/copy"
3129
"github.com/spf13/cobra"
3230
jww "github.com/spf13/jwalterweatherman"
3331

34-
cleanhttp "github.com/hashicorp/go-cleanhttp"
3532
getter "github.com/hashicorp/go-getter"
3633

3734
xt "github.com/devopsmakers/xterrafile/pkg/xterrafile"
@@ -83,9 +80,9 @@ func getModule(moduleName string, moduleMeta module, wg *sync.WaitGroup) {
8380
xt.CopyFile(moduleName, moduleSource, directory)
8481
case xt.IsRegistrySourceAddr(moduleSource):
8582
source, version := xt.GetRegistrySource(moduleName, moduleSource, moduleVersion, nil)
86-
getWithGoGetter(moduleName, source, version, directory)
83+
xt.GetWithGoGetter(moduleName, source, version, directory)
8784
default:
88-
getWithGoGetter(moduleName, moduleSource, moduleVersion, directory)
85+
xt.GetWithGoGetter(moduleName, moduleSource, moduleVersion, directory)
8986
}
9087

9188
// If we have a path specified, let's extract it (move and copy stuff).
@@ -103,90 +100,3 @@ func getModule(moduleName string, moduleMeta module, wg *sync.WaitGroup) {
103100
// Cleanup .git directory
104101
os.RemoveAll(path.Join(directory, ".git"))
105102
}
106-
107-
// Handle modules from other sources to reflect:
108-
// https://www.terraform.io/docs/modules/sources.html
109-
//
110-
// HEAVILY inpired by Terraform's internal getter / module_install code:
111-
// https://github.com/hashicorp/terraform/blob/master/internal/initwd/getter.go
112-
// https://github.com/hashicorp/terraform/blob/master/internal/initwd/module_install.go
113-
114-
var goGetterDetectors = []getter.Detector{
115-
new(getter.GitHubDetector),
116-
new(getter.BitBucketDetector),
117-
new(getter.GCSDetector),
118-
new(getter.S3Detector),
119-
new(getter.FileDetector),
120-
}
121-
122-
var goGetterNoDetectors = []getter.Detector{}
123-
124-
var goGetterDecompressors = map[string]getter.Decompressor{
125-
"bz2": new(getter.Bzip2Decompressor),
126-
"gz": new(getter.GzipDecompressor),
127-
"xz": new(getter.XzDecompressor),
128-
"zip": new(getter.ZipDecompressor),
129-
130-
"tar.bz2": new(getter.TarBzip2Decompressor),
131-
"tar.tbz2": new(getter.TarBzip2Decompressor),
132-
133-
"tar.gz": new(getter.TarGzipDecompressor),
134-
"tgz": new(getter.TarGzipDecompressor),
135-
136-
"tar.xz": new(getter.TarXzDecompressor),
137-
"txz": new(getter.TarXzDecompressor),
138-
}
139-
140-
var goGetterGetters = map[string]getter.Getter{
141-
"file": new(getter.FileGetter),
142-
"gcs": new(getter.GCSGetter),
143-
"git": new(getter.GitGetter),
144-
"hg": new(getter.HgGetter),
145-
"s3": new(getter.S3Getter),
146-
"http": getterHTTPGetter,
147-
"https": getterHTTPGetter,
148-
}
149-
150-
var getterHTTPClient = cleanhttp.DefaultClient()
151-
152-
var getterHTTPGetter = &getter.HttpGetter{
153-
Client: getterHTTPClient,
154-
Netrc: true,
155-
}
156-
157-
func getWithGoGetter(name string, source string, version string, directory string) {
158-
159-
// Fixup potential URLs for Github Detector
160-
if xt.IContains(source, ".git") {
161-
source = strings.Replace(source, "https://github.com/", "github.com/", 1)
162-
}
163-
164-
moduleSource, err := getter.Detect(source, directory, getter.Detectors)
165-
xt.CheckIfError(name, err)
166-
167-
jww.DEBUG.Printf("[%s] Detected real source: %s", name, moduleSource)
168-
169-
realModuleSource, err := url.Parse(moduleSource)
170-
xt.CheckIfError(name, err)
171-
172-
if len(version) > 0 {
173-
qParams := realModuleSource.Query()
174-
qParams.Set("ref", version)
175-
realModuleSource.RawQuery = qParams.Encode()
176-
}
177-
178-
jww.INFO.Printf("[%s] Fetching %s", name, realModuleSource.String())
179-
client := getter.Client{
180-
Src: realModuleSource.String(),
181-
Dst: directory,
182-
Pwd: directory,
183-
184-
Mode: getter.ClientModeDir,
185-
186-
Detectors: goGetterNoDetectors, // we already did detection above
187-
Decompressors: goGetterDecompressors,
188-
Getters: goGetterGetters,
189-
}
190-
err = client.Get()
191-
xt.CheckIfError(name, err)
192-
}

pkg/xterrafile/getter.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright © 2019 Tim Birkett <tim.birkett@devopsmakers.com>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package xterrafile
22+
23+
import (
24+
"net/url"
25+
"strings"
26+
27+
cleanhttp "github.com/hashicorp/go-cleanhttp"
28+
getter "github.com/hashicorp/go-getter"
29+
jww "github.com/spf13/jwalterweatherman"
30+
)
31+
32+
// Handle modules from other sources to reflect:
33+
// https://www.terraform.io/docs/modules/sources.html
34+
//
35+
// HEAVILY inpired by Terraform's internal getter / module_install code:
36+
// https://github.com/hashicorp/terraform/blob/master/internal/initwd/getter.go
37+
// https://github.com/hashicorp/terraform/blob/master/internal/initwd/module_install.go
38+
39+
var goGetterDetectors = []getter.Detector{
40+
new(getter.GitHubDetector),
41+
new(getter.BitBucketDetector),
42+
new(getter.GCSDetector),
43+
new(getter.S3Detector),
44+
new(getter.FileDetector),
45+
}
46+
47+
var goGetterNoDetectors = []getter.Detector{}
48+
49+
var goGetterDecompressors = map[string]getter.Decompressor{
50+
"bz2": new(getter.Bzip2Decompressor),
51+
"gz": new(getter.GzipDecompressor),
52+
"xz": new(getter.XzDecompressor),
53+
"zip": new(getter.ZipDecompressor),
54+
55+
"tar.bz2": new(getter.TarBzip2Decompressor),
56+
"tar.tbz2": new(getter.TarBzip2Decompressor),
57+
58+
"tar.gz": new(getter.TarGzipDecompressor),
59+
"tgz": new(getter.TarGzipDecompressor),
60+
61+
"tar.xz": new(getter.TarXzDecompressor),
62+
"txz": new(getter.TarXzDecompressor),
63+
}
64+
65+
var goGetterGetters = map[string]getter.Getter{
66+
"file": new(getter.FileGetter),
67+
"gcs": new(getter.GCSGetter),
68+
"git": new(getter.GitGetter),
69+
"hg": new(getter.HgGetter),
70+
"s3": new(getter.S3Getter),
71+
"http": getterHTTPGetter,
72+
"https": getterHTTPGetter,
73+
}
74+
75+
var getterHTTPClient = cleanhttp.DefaultClient()
76+
77+
var getterHTTPGetter = &getter.HttpGetter{
78+
Client: getterHTTPClient,
79+
Netrc: true,
80+
}
81+
82+
// GetWithGoGetter downloads objects using go-getter
83+
func GetWithGoGetter(name string, source string, version string, directory string) {
84+
85+
// Fixup potential URLs for Github Detector
86+
if IContains(source, ".git") {
87+
source = strings.Replace(source, "https://github.com/", "github.com/", 1)
88+
}
89+
90+
moduleSource, err := getter.Detect(source, directory, getter.Detectors)
91+
CheckIfError(name, err)
92+
93+
jww.DEBUG.Printf("[%s] Detected real source: %s", name, moduleSource)
94+
95+
realModuleSource, err := url.Parse(moduleSource)
96+
CheckIfError(name, err)
97+
98+
if len(version) > 0 {
99+
qParams := realModuleSource.Query()
100+
qParams.Set("ref", version)
101+
realModuleSource.RawQuery = qParams.Encode()
102+
}
103+
104+
jww.INFO.Printf("[%s] Fetching %s", name, realModuleSource.String())
105+
client := getter.Client{
106+
Src: realModuleSource.String(),
107+
Dst: directory,
108+
Pwd: directory,
109+
110+
Mode: getter.ClientModeDir,
111+
112+
Detectors: goGetterNoDetectors, // we already did detection above
113+
Decompressors: goGetterDecompressors,
114+
Getters: goGetterGetters,
115+
}
116+
err = client.Get()
117+
CheckIfError(name, err)
118+
}

0 commit comments

Comments
 (0)