forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_release.go
110 lines (88 loc) · 2.75 KB
/
create_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
package cmd
import (
semver "github.com/cppforlife/go-semi-semantic/version"
boshrel "github.com/cloudfoundry/bosh-cli/release"
boshreldir "github.com/cloudfoundry/bosh-cli/releasedir"
boshui "github.com/cloudfoundry/bosh-cli/ui"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshfu "github.com/cloudfoundry/bosh-utils/fileutil"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type CreateReleaseCmd struct {
releaseDirFactory func(DirOrCWDArg) (boshrel.Reader, boshreldir.ReleaseDir)
releaseWriter boshrel.Writer
fs boshsys.FileSystem
ui boshui.UI
}
func NewCreateReleaseCmd(
releaseDirFactory func(DirOrCWDArg) (boshrel.Reader, boshreldir.ReleaseDir),
releaseWriter boshrel.Writer,
fs boshsys.FileSystem,
ui boshui.UI,
) CreateReleaseCmd {
return CreateReleaseCmd{releaseDirFactory, releaseWriter, fs, ui}
}
func (c CreateReleaseCmd) Run(opts CreateReleaseOpts) (boshrel.Release, error) {
releaseManifestReader, releaseDir := c.releaseDirFactory(opts.Directory)
manifestGiven := len(opts.Args.Manifest.Path) > 0
var release boshrel.Release
var err error
if manifestGiven {
release, err = releaseManifestReader.Read(opts.Args.Manifest.Path)
if err != nil {
return nil, err
}
} else {
release, err = c.buildRelease(releaseDir, opts)
if err != nil {
return nil, err
}
if opts.Final {
err = c.finalizeRelease(releaseDir, release, opts)
if err != nil {
return nil, err
}
}
}
if opts.Tarball.ExpandedPath != "" {
path, err := c.releaseWriter.Write(release, nil)
if err != nil {
return nil, err
}
err = boshfu.NewFileMover(c.fs).Move(path, opts.Tarball.ExpandedPath)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Moving release archive to final destination")
}
}
ReleaseTables{Release: release, ArchivePath: opts.Tarball.ExpandedPath}.Print(c.ui)
return release, nil
}
func (c CreateReleaseCmd) buildRelease(releaseDir boshreldir.ReleaseDir, opts CreateReleaseOpts) (boshrel.Release, error) {
var err error
name := opts.Name
if len(name) == 0 {
name, err = releaseDir.DefaultName()
if err != nil {
return nil, err
}
}
version := semver.Version(opts.Version)
if version.Empty() {
version, err = releaseDir.NextDevVersion(name, opts.TimestampVersion)
if err != nil {
return nil, err
}
}
return releaseDir.BuildRelease(name, version, opts.Force)
}
func (c CreateReleaseCmd) finalizeRelease(releaseDir boshreldir.ReleaseDir, release boshrel.Release, opts CreateReleaseOpts) error {
version := semver.Version(opts.Version)
if version.Empty() {
version, err := releaseDir.NextFinalVersion(release.Name())
if err != nil {
return err
}
release.SetVersion(version.AsString())
}
return releaseDir.FinalizeRelease(release, opts.Force)
}