/
build.go
121 lines (104 loc) · 2.82 KB
/
build.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package manager
import (
"strconv"
"github.com/r3labs/diff"
h "github.com/ernestio/ernest-cli/helper"
"github.com/ernestio/ernest-cli/view"
eclient "github.com/ernestio/ernest-go-sdk/client"
emodels "github.com/ernestio/ernest-go-sdk/models"
)
// Build : ernest-go-sdk Build wrapper
type Build struct {
cli *eclient.Client
Verbose bool
}
// Create : Creates a new build
func (c *Build) Create(definition []byte) *emodels.Build {
build, err := c.cli.Builds.Create(definition)
if err != nil {
merr, ok := err.(*emodels.Error)
if ok {
view.PrintValidation(merr.Validation)
}
h.PrintError(err.Error())
}
if c.Verbose {
view.PrintValidation(build.Validation)
}
return build
}
// Dry : Simulates the creation of a new build
func (c *Build) Dry(definition []byte) *[]string {
build, err := c.cli.Builds.Dry(definition)
if err != nil {
h.PrintError(err.Error())
}
return build
}
// Get : Gets a build by name
func (c *Build) Get(project, env, id string) *emodels.Build {
build, err := c.cli.Builds.Get(project, env, id)
if err != nil {
h.PrintError(err.Error())
}
return build
}
// List : Lists all builds on the system
func (c *Build) List(project, env string) []*emodels.Build {
builds, err := c.cli.Builds.List(project, env)
if err != nil {
h.PrintError(err.Error())
}
return builds
}
// Diff : Diff two builds by id
func (c *Build) Diff(project, env, from, to string) *diff.Changelog {
changelog, err := c.cli.Builds.Diff(project, env, from, to)
if err != nil {
h.PrintError(err.Error())
}
return changelog
}
// Changelog : get a changelog for a build (if it has been generated)
func (c *Build) Changelog(project, env, id string) *diff.Changelog {
changelog, err := c.cli.Builds.Changelog(project, env, id)
if err != nil {
h.PrintError(err.Error())
}
return changelog
}
// Stream : Streams build progress
func (c *Build) Stream(id string) chan []byte {
ch, err := c.cli.Builds.Stream(id)
if err != nil {
h.PrintError(err.Error())
}
return ch
}
// BuildByPosition : Streams build progress
func (c *Build) BuildByPosition(project, env, pos string) *emodels.Build {
builds := c.List(project, env)
if len(builds) == 0 {
h.PrintError("No builds were found for the specified parameters")
}
num := 0
if pos != "" {
num, _ = strconv.Atoi(pos)
if num < 1 || num > len(builds) {
h.PrintError("Invalid build ID")
}
num = len(builds) - num
}
return builds[num]
}
// Definition : Gets a build definitin by name
func (c *Build) Definition(project, env, id string) string {
build, err := c.cli.Builds.Definition(project, env, id)
if err != nil {
h.PrintError(err.Error())
}
return build
}