|
| 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