@@ -22,13 +22,19 @@ import (
22
22
"github.com/spf13/viper"
23
23
)
24
24
25
+ type LocalAppCopy interface {
26
+ GetType () string
27
+ GetLocalPath () string
28
+ Remove (FS afero.Afero ) error
29
+ }
30
+
25
31
type Inspector interface {
26
32
// DetermineApplicationType loads and application from upstream,
27
33
// returning the app type and the local path where its been downloaded (when applicable),
28
34
DetermineApplicationType (
29
35
ctx context.Context ,
30
36
upstream string ,
31
- ) (appType string , localPath string , err error )
37
+ ) (app LocalAppCopy , err error )
32
38
}
33
39
34
40
func NewInspector (
@@ -59,18 +65,18 @@ type FileFetcher interface {
59
65
GetFiles (ctx context.Context , upstream , savePath string ) (string , error )
60
66
}
61
67
62
- func (r * inspector ) DetermineApplicationType (ctx context.Context , upstream string ) (appType string , localPath string , err error ) {
68
+ func (r * inspector ) DetermineApplicationType (ctx context.Context , upstream string ) (app LocalAppCopy , err error ) {
63
69
// hack hack hack
64
70
isReplicatedApp := strings .HasPrefix (upstream , "replicated.app" ) ||
65
71
strings .HasPrefix (upstream , "staging.replicated.app" ) ||
66
72
strings .HasPrefix (upstream , "local.replicated.app" )
67
73
if isReplicatedApp {
68
- return "replicated.app" , "" , nil
74
+ return & localAppCopy { AppType : "replicated.app" } , nil
69
75
}
70
76
71
77
parts := strings .SplitN (upstream , "?" , 2 )
72
78
if _ , err := os .Stat (parts [0 ]); err == nil && gogetter .IsShipYaml (parts [0 ]) {
73
- return "runbook.replicated.app" , parts [0 ], nil
79
+ return & localAppCopy { AppType : "runbook.replicated.app" , LocalPath : parts [0 ]} , nil
74
80
}
75
81
76
82
r .ui .Info (fmt .Sprintf ("Attempting to retrieve upstream %s ..." , upstream ))
@@ -90,19 +96,15 @@ func (r *inspector) DetermineApplicationType(ctx context.Context, upstream strin
90
96
return r .determineTypeFromContents (ctx , upstream , & fetcher )
91
97
}
92
98
93
- return "" , "" , errors .New (fmt .Sprintf ("upstream %s is not a replicated app, a github repo, or compatible with go-getter" , upstream ))
99
+ return nil , errors .New (fmt .Sprintf ("upstream %s is not a replicated app, a github repo, or compatible with go-getter" , upstream ))
94
100
}
95
101
96
- func (r * inspector ) determineTypeFromContents (ctx context.Context , upstream string , fetcher FileFetcher ) (
97
- applicationType string ,
98
- checkoutPath string ,
99
- err error ,
100
- ) {
102
+ func (r * inspector ) determineTypeFromContents (ctx context.Context , upstream string , fetcher FileFetcher ) (app LocalAppCopy , err error ) {
101
103
debug := level .Debug (r .logger )
102
104
103
105
repoSavePath , err := r .fs .TempDir (constants .ShipPathInternalTmp , "repo" )
104
106
if err != nil {
105
- return "" , "" , errors .Wrap (err , "create tmp dir" )
107
+ return nil , errors .Wrap (err , "create tmp dir" )
106
108
}
107
109
108
110
finalPath , err := fetcher .GetFiles (ctx , upstream , repoSavePath )
@@ -129,10 +131,10 @@ func (r *inspector) determineTypeFromContents(ctx context.Context, upstream stri
129
131
}
130
132
131
133
if ! hasSucceeded {
132
- return "" , "" , retryError
134
+ return nil , retryError
133
135
}
134
136
} else {
135
- return "" , "" , err
137
+ return nil , err
136
138
}
137
139
}
138
140
@@ -141,10 +143,10 @@ func (r *inspector) determineTypeFromContents(ctx context.Context, upstream stri
141
143
for _ , filename := range []string {"ship.yaml" , "ship.yml" } {
142
144
isReplicatedApp , err = r .fs .Exists (path .Join (finalPath , filename ))
143
145
if err != nil {
144
- return "" , "" , errors .Wrapf (err , "check for %s" , filename )
146
+ return nil , errors .Wrapf (err , "check for %s" , filename )
145
147
}
146
148
if isReplicatedApp {
147
- return "inline.replicated.app" , finalPath , nil
149
+ return & localAppCopy { AppType : "inline.replicated.app" , LocalPath : finalPath , rootTempDir : repoSavePath } , nil
148
150
}
149
151
}
150
152
@@ -156,8 +158,41 @@ func (r *inspector) determineTypeFromContents(ctx context.Context, upstream stri
156
158
debug .Log ("event" , "isChart.check" , "isChart" , isChart )
157
159
158
160
if isChart {
159
- return "helm" , finalPath , nil
161
+ return & localAppCopy { AppType : "helm" , LocalPath : finalPath , rootTempDir : repoSavePath } , nil
160
162
}
161
163
162
- return "k8s" , finalPath , nil
164
+ return & localAppCopy {AppType : "k8s" , LocalPath : finalPath , rootTempDir : repoSavePath }, nil
165
+ }
166
+
167
+ func NewLocalAppCopy (
168
+ appType string ,
169
+ localPath string ,
170
+ rootTempDir string ,
171
+ ) LocalAppCopy {
172
+ return & localAppCopy {
173
+ AppType : appType ,
174
+ LocalPath : localPath ,
175
+ rootTempDir : rootTempDir ,
176
+ }
177
+ }
178
+
179
+ type localAppCopy struct {
180
+ AppType string
181
+ LocalPath string
182
+ rootTempDir string
183
+ }
184
+
185
+ func (c * localAppCopy ) GetType () string {
186
+ return c .AppType
187
+ }
188
+
189
+ func (c * localAppCopy ) GetLocalPath () string {
190
+ return c .LocalPath
191
+ }
192
+
193
+ func (c * localAppCopy ) Remove (fs afero.Afero ) error {
194
+ if c .rootTempDir == "" {
195
+ return nil
196
+ }
197
+ return fs .RemoveAll (c .rootTempDir )
163
198
}
0 commit comments