forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_stemcell.go
86 lines (67 loc) · 1.97 KB
/
upload_stemcell.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
package cmd
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
semver "github.com/cppforlife/go-semi-semantic/version"
boshdir "github.com/cloudfoundry/bosh-cli/director"
biui "github.com/cloudfoundry/bosh-cli/ui"
)
type UploadStemcellCmd struct {
director boshdir.Director
stemcellArchiveFactory func(string) boshdir.StemcellArchive
ui biui.UI
}
func NewUploadStemcellCmd(
director boshdir.Director,
stemcellArchiveFactory func(string) boshdir.StemcellArchive,
ui biui.UI,
) UploadStemcellCmd {
return UploadStemcellCmd{
director: director,
stemcellArchiveFactory: stemcellArchiveFactory,
ui: ui,
}
}
func (c UploadStemcellCmd) Run(opts UploadStemcellOpts) error {
if opts.Args.URL.IsRemote() {
return c.uploadRemote(string(opts.Args.URL), opts)
}
return c.uploadFile(opts.Args.URL.FilePath(), opts.Fix)
}
func (c UploadStemcellCmd) uploadRemote(url string, opts UploadStemcellOpts) error {
version := semver.Version(opts.Version)
necessary, err := c.needToUpload(opts.Name, version.AsString(), opts.Fix)
if err != nil || !necessary {
return err
}
return c.director.UploadStemcellURL(url, opts.SHA1, opts.Fix)
}
func (c UploadStemcellCmd) uploadFile(path string, fix bool) error {
archive := c.stemcellArchiveFactory(path)
name, version, err := archive.Info()
if err != nil {
return bosherr.WrapErrorf(err, "Retrieving stemcell info")
}
necessary, err := c.needToUpload(name, version, fix)
if err != nil || !necessary {
return err
}
file, err := archive.File()
if err != nil {
return bosherr.WrapErrorf(err, "Opening stemcell")
}
return c.director.UploadStemcellFile(file, fix)
}
func (c UploadStemcellCmd) needToUpload(name, version string, fix bool) (bool, error) {
if fix {
return true, nil
}
found, err := c.director.HasStemcell(name, version)
if err != nil {
return true, err
}
if found {
c.ui.PrintLinef("Stemcell '%s/%s' already exists.", name, version)
return false, nil
}
return true, nil
}