This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
87 lines (76 loc) · 2.41 KB
/
main.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
package main
import (
"encoding/json"
"os"
"github.com/pivotalservices/file-downloader-resource/config"
"github.com/pivotalservices/file-downloader-resource/file"
"github.com/pivotalservices/file-downloader-resource/types"
)
var VERSION = "0.0.0-dev"
func main() {
if len(os.Args) < 2 {
println("version: " + VERSION)
println("usage: " + os.Args[0] + " <destination>")
os.Exit(1)
}
destination := os.Args[1]
err := os.MkdirAll(destination, 0755)
if err != nil {
fatal("creating destination", err)
}
var request types.InRequest
err = json.NewDecoder(os.Stdin).Decode(&request)
if err != nil {
fatal("reading request", err)
}
configProvider, err := config.FromSource(request.Source)
if err != nil {
fatal("constructing config provider", err)
}
versionInfo, err := configProvider.GetVersionInfo(request.Version.Ref, request.Params.Product)
if err != nil {
fatal("getting version info", err)
}
fileProvider, err := file.FromSource(request.Source)
if err != nil {
fatal("constructing file provider", err)
}
if request.Params.Stemcell {
err = fileProvider.DownloadFile(destination, versionInfo.StemcellProductPath(), versionInfo.StemcellVersion, versionInfo.StemcellFilePattern, request.Params.Unpack)
if err != nil {
fatal("downloading stemcell file", err)
}
} else {
err = fileProvider.DownloadFile(destination, versionInfo.PivotalProduct, versionInfo.Version, versionInfo.FilePattern, request.Params.Unpack)
if err != nil {
fatal("downloading file", err)
}
}
if request.Params.Stemcell {
json.NewEncoder(os.Stdout).Encode(types.InResponse{
Version: request.Version,
Metadata: types.Metadata{
{Name: "resource_version", Value: VERSION},
{Name: "ref", Value: request.Version.Ref},
{Name: "product", Value: "stemcells"},
{Name: "product_version", Value: versionInfo.StemcellVersion},
{Name: "file_pattern", Value: versionInfo.StemcellFilePattern},
},
})
} else {
json.NewEncoder(os.Stdout).Encode(types.InResponse{
Version: request.Version,
Metadata: types.Metadata{
{Name: "resource_version", Value: VERSION},
{Name: "ref", Value: request.Version.Ref},
{Name: "product", Value: versionInfo.PivotalProduct},
{Name: "product_version", Value: versionInfo.Version},
{Name: "file_pattern", Value: versionInfo.FilePattern},
},
})
}
}
func fatal(doing string, err error) {
println("error " + doing + ": " + err.Error())
os.Exit(1)
}