-
Notifications
You must be signed in to change notification settings - Fork 33
/
terragruntretriever.go
150 lines (126 loc) · 3.94 KB
/
terragruntretriever.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
/*
*
* Copyright 2024 tofuutils authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package terragruntretriever
import (
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/hashicorp/go-hclog"
"github.com/tofuutils/tenv/config"
"github.com/tofuutils/tenv/pkg/apimsg"
sha256check "github.com/tofuutils/tenv/pkg/check/sha256"
"github.com/tofuutils/tenv/pkg/download"
"github.com/tofuutils/tenv/pkg/github"
htmlretriever "github.com/tofuutils/tenv/versionmanager/retriever/html"
)
const (
baseFileName = "terragrunt_"
gruntworkName = "gruntwork-io"
)
type TerragruntRetriever struct {
conf *config.Config
}
func Make(conf *config.Config) TerragruntRetriever {
return TerragruntRetriever{conf: conf}
}
func (r TerragruntRetriever) InstallRelease(versionStr string, targetPath string) error {
err := r.conf.InitRemoteConf()
if err != nil {
return err
}
tag := versionStr
// assume that terragrunt tags start with a 'v'
if tag[0] != 'v' {
tag = "v" + versionStr
}
var assetURLs []string
fileName, shaFileName := buildAssetNames(r.conf.Arch)
if r.conf.Displayer.IsDebug() {
r.conf.Displayer.Log(hclog.Debug, apimsg.MsgSearch, apimsg.AssetsName, []string{fileName, shaFileName})
}
switch r.conf.Tg.GetInstallMode() {
case config.InstallModeDirect:
baseAssetURL, err2 := url.JoinPath(r.conf.Tg.GetRemoteURL(), gruntworkName, config.TerragruntName, github.Releases, github.Download, tag) //nolint
if err2 != nil {
return err2
}
assetURLs, err = htmlretriever.BuildAssetURLs(baseAssetURL, fileName, shaFileName)
case config.ModeAPI:
assetURLs, err = github.AssetDownloadURL(tag, []string{fileName, shaFileName}, r.conf.Tg.GetRemoteURL(), r.conf.GithubToken, r.conf.Displayer.Display)
default:
return config.ErrInstallMode
}
if err != nil {
return err
}
urlTranformer := download.UrlTranformer(r.conf.Tg.GetRewriteRule())
assetURLs, err = download.ApplyUrlTranformer(urlTranformer, assetURLs...)
if err != nil {
return err
}
data, err := download.Bytes(assetURLs[0], r.conf.Displayer.Display)
if err != nil {
return err
}
dataSums, err := download.Bytes(assetURLs[1], r.conf.Displayer.Display)
if err != nil {
return err
}
if err = sha256check.Check(data, dataSums, fileName); err != nil {
return err
}
err = os.MkdirAll(targetPath, 0755)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(targetPath, config.TerragruntName), data, 0755)
}
func (r TerragruntRetriever) ListReleases() ([]string, error) {
err := r.conf.InitRemoteConf()
if err != nil {
return nil, err
}
listURL := r.conf.Tg.GetListURL()
switch r.conf.Tg.GetListMode() {
case config.ListModeHTML:
baseURL, err := url.JoinPath(listURL, gruntworkName, config.TerragruntName, github.Releases, github.Download) //nolint
if err != nil {
return nil, err
}
r.conf.Displayer.Display(apimsg.MsgFetchAllReleases + baseURL)
return htmlretriever.ListReleases(baseURL, r.conf.Tg.Data)
case config.ModeAPI:
r.conf.Displayer.Display(apimsg.MsgFetchAllReleases + listURL)
return github.ListReleases(listURL, r.conf.GithubToken)
default:
return nil, config.ErrListMode
}
}
func buildAssetNames(arch string) (string, string) {
var nameBuilder strings.Builder
nameBuilder.WriteString(baseFileName)
nameBuilder.WriteString(runtime.GOOS)
nameBuilder.WriteByte('_')
nameBuilder.WriteString(arch)
if runtime.GOOS == "windows" {
nameBuilder.WriteString(".exe")
}
return nameBuilder.String(), "SHA256SUMS"
}