This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_release.go
121 lines (96 loc) · 2.81 KB
/
upload_release.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
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"
boshrel "github.com/cloudfoundry/bosh-cli/release"
boshreldir "github.com/cloudfoundry/bosh-cli/releasedir"
boshui "github.com/cloudfoundry/bosh-cli/ui"
)
type UploadReleaseCmd struct {
releaseReader boshrel.Reader
releaseArchiveWriter boshrel.Writer
releaseDir boshreldir.ReleaseDir
director boshdir.Director
releaseArchiveFactory func(string) boshdir.ReleaseArchive
ui boshui.UI
}
func NewUploadReleaseCmd(
releaseReader boshrel.Reader,
releaseArchiveWriter boshrel.Writer,
releaseDir boshreldir.ReleaseDir,
director boshdir.Director,
releaseArchiveFactory func(string) boshdir.ReleaseArchive,
ui boshui.UI,
) UploadReleaseCmd {
return UploadReleaseCmd{
releaseReader: releaseReader,
releaseArchiveWriter: releaseArchiveWriter,
releaseDir: releaseDir,
director: director,
releaseArchiveFactory: releaseArchiveFactory,
ui: ui,
}
}
func (c UploadReleaseCmd) Run(opts UploadReleaseOpts) error {
if opts.Args.URL.IsRemote() {
return c.uploadRemote(string(opts.Args.URL), opts)
}
if c.releaseReader == nil {
return bosherr.Errorf("Cannot upload non-remote release '%s'", opts.Args.URL)
}
return c.uploadFile(opts.Args.URL.FilePath(), opts.Rebase, opts.Fix)
}
func (c UploadReleaseCmd) uploadRemote(url string, opts UploadReleaseOpts) 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.UploadReleaseURL(url, opts.SHA1, opts.Rebase, opts.Fix)
}
func (c UploadReleaseCmd) uploadFile(path string, rebase, fix bool) error {
var release boshrel.Release
var err error
if len(path) > 0 {
release, err = c.releaseReader.Read(path)
if err != nil {
return err
}
} else {
release, err = c.releaseDir.LastRelease()
if err != nil {
return err
}
}
var pkgFpsToSkip []string
if !fix {
pkgFpsToSkip, err = c.director.MatchPackages(release.Manifest(), release.IsCompiled())
if err != nil {
return err
}
}
path, err = c.releaseArchiveWriter.Write(release, pkgFpsToSkip)
if err != nil {
return err
}
file, err := c.releaseArchiveFactory(path).File()
if err != nil {
return bosherr.WrapErrorf(err, "Opening release")
}
return c.director.UploadReleaseFile(file, rebase, fix)
}
func (c UploadReleaseCmd) needToUpload(name, version string, fix bool) (bool, error) {
if fix {
return true, nil
}
found, err := c.director.HasRelease(name, version)
if err != nil {
return true, err
}
if found {
c.ui.PrintLinef("Release '%s/%s' already exists.", name, version)
return false, nil
}
return true, nil
}