Skip to content

Commit f31958a

Browse files
committed
Implemented http/https remote archive fetching
1 parent cd50de4 commit f31958a

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

RunSettings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func FindRunSettings() (RunSettings, error) {
5555

5656
flag.StringVar(&parameterGroup, "p", "", "Semicolon-separated list of parameters in k=v form.")
5757
flag.BoolVar(&ret.inspectionRun, "i", false, "Whether or not to show a list of available parameters for the skeleton")
58-
flag.BoolVar(&ret.addRegistry, "a", false, "Whether or not to register the template at the given path")
58+
flag.BoolVar(&ret.addRegistry, "a", false, "Whether or not to register the template at the given path (can be http/https URLs)")
5959
flag.BoolVar(&ret.showRegistry, "l", false, "Whether or not to show a list of all available registered templates")
6060
flag.BoolVar(&forceGenerate, "f", false, "Whether or not to overwrite existing files during generation")
6161
flag.BoolVar(&forceDelete, "d", false, "Whether or not to delete every existing file in the output path first (only valid when -f is specified)")

TemplateRegistry.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import (
66
"github.com/jhoonb/archivex"
77
"github.com/mitchellh/go-homedir"
88
"io/ioutil"
9+
"net/http"
910
"os"
11+
"net/url"
12+
"io"
1013
"path/filepath"
1114
"strings"
1215
)
@@ -112,6 +115,15 @@ func (this *TemplateRegistry) RegisterTemplate(path string) (string, error) {
112115
var targetPath, name string
113116
var err error
114117

118+
// if this is a remote path, download as a zip to a temporary directory before trying to register.
119+
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
120+
121+
path, err = downloadArchive(path)
122+
if(err != nil) {
123+
return "", err
124+
}
125+
}
126+
115127
// if the given path is a directory (not a zip file),
116128
// archive it and prepare for registration.
117129
if !strings.HasSuffix(path, archiveMarker) {
@@ -129,6 +141,57 @@ func (this *TemplateRegistry) RegisterTemplate(path string) (string, error) {
129141
return name, err
130142
}
131143

144+
/*
145+
Downloads the given [url] to a temporary directory (as a .zip).
146+
Returns the temporary path to the downloaded archive, or an error if it encountered one.
147+
*/
148+
func downloadArchive(targetURL string) (string, error) {
149+
150+
var response *http.Response
151+
var outputFile *os.File
152+
var parsedURL *url.URL
153+
var outputPath string
154+
var baseName string
155+
var err error
156+
157+
outputPath, err = ioutil.TempDir("", "wiskRemoteArchive")
158+
if(err != nil) {
159+
return "", err
160+
}
161+
162+
parsedURL, err = url.Parse(targetURL)
163+
if(err != nil) {
164+
return "", err
165+
}
166+
167+
baseName = filepath.Base(parsedURL.Path)
168+
baseName = strings.TrimSuffix(baseName, filepath.Ext(baseName))
169+
outputPath = filepath.Join(outputPath, baseName + ".zip")
170+
outputPath, _ = filepath.Abs(outputPath)
171+
172+
outputFile, err = os.Create(outputPath)
173+
if(err != nil) {
174+
return "", err
175+
}
176+
defer outputFile.Close()
177+
178+
response, err = http.Get(targetURL)
179+
if(err != nil) {
180+
return "", err
181+
}
182+
183+
if(response.Status != "200") {
184+
errorMsg := fmt.Sprintf("Unable to download remote archive: HTTP %s\n", response.Status)
185+
return "", errors.New(errorMsg)
186+
}
187+
188+
_, err = io.Copy(outputFile, response.Body)
189+
if(err != nil) {
190+
return "", err
191+
}
192+
return outputPath, nil
193+
}
194+
132195
func archivePath(path string) (string, error) {
133196

134197
var zip archivex.ZipFile

docs/wisk.7

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Causes the program to list all registered templates by name, then immediately ex
2222
.IP -i
2323
Causes the program to inspect the given skeleton (by positional argument 1), print out all of that skeleton's possible parameters, then exit.
2424
.IP -a
25-
Causes the program to register the given path (by positional argument 1) as a template. If the path is a *.zip file, it is added to the user's skeleton registry as-is. If the path is a directory, that directory is zipped and added to the user's skeleton registry. In no case will this command ever modify the given directory.
25+
Causes the program to register the given path (by positional argument 1) as a template. If the path is a *.zip file, it is added to the user's skeleton registry as-is. If the path is a directory, that directory is zipped and added to the user's skeleton registry. If the path is prefixed with 'http://' or 'https://', the remote archive will be downloaded and registered. In no case will this command ever modify the given directory.
2626
.IP -p
2727
Specifies parameters to be used when populating the target project. See "Parameters" for details.
2828
.IP -f

0 commit comments

Comments
 (0)