Skip to content

Commit 08618f6

Browse files
committed
feat(build): add build info
1 parent de591be commit 08618f6

11 files changed

Lines changed: 115 additions & 131 deletions

File tree

cmd/builds.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,54 @@
11
package cmd
22

33
import (
4-
"fmt"
54
"os"
65

76
yaml "gopkg.in/yaml.v3"
87

98
"github.com/drycc/controller-sdk-go/builds"
109
)
1110

12-
// BuildsList lists an app's builds.
13-
func (d *DryccCmd) BuildsList(appID string, results int) error {
11+
// BuildsInfo lists an app's builds.
12+
func (d *DryccCmd) BuildsInfo(appID string, version int) error {
1413
s, appID, err := load(d.ConfigFile, appID)
15-
1614
if err != nil {
1715
return err
1816
}
1917

20-
if results == defaultLimit {
21-
results = s.Limit
22-
}
23-
24-
builds, count, err := builds.List(s.Client, appID, results)
18+
build, err := builds.Get(s.Client, appID, version)
2519
if d.checkAPICompatibility(s.Client, err) != nil {
2620
return err
2721
}
28-
if count > 0 {
29-
table := d.getDefaultFormatTable([]string{"OWNER", "SHA", "CREATED"})
30-
for _, build := range builds {
31-
table.Append([]string{
32-
safeGetString(build.Owner),
33-
safeGetString(build.Sha),
34-
d.formatTime(build.Created),
35-
})
36-
}
22+
table := d.getDefaultFormatTable([]string{})
23+
table.Append([]string{"App:", build.App})
24+
table.Append([]string{"Sha:", build.Sha})
25+
table.Append([]string{"UUID:", build.UUID})
26+
table.Append([]string{"Owner:", build.Owner})
27+
table.Append([]string{"Image:", build.Image})
28+
table.Append([]string{"Stack:", build.Stack})
29+
table.Append([]string{"Created:", build.Created})
30+
table.Append([]string{"Updated:", build.Updated})
31+
table.Render()
32+
33+
if len(build.Dockerfile) != 0 {
34+
table = d.getDefaultFormatTable([]string{})
35+
table.Append([]string{"Dockerfile:"})
36+
table.Append([]string{d.indentString(build.Dockerfile, 2)})
37+
table.Render()
38+
}
39+
40+
if len(build.Procfile) != 0 {
41+
table = d.getDefaultFormatTable([]string{})
42+
table.Append([]string{"Procfile:"})
43+
table.Append([]string{d.indentString(d.toYamlString(build.Procfile, 2), 2)})
44+
table.Render()
45+
}
46+
47+
if len(build.Dryccfile) != 0 {
48+
table = d.getDefaultFormatTable([]string{})
49+
table.Append([]string{"Dryccfile:"})
50+
table.Append([]string{d.indentString(d.toYamlString(build.Dryccfile, 2), 2)})
3751
table.Render()
38-
} else {
39-
d.Println(fmt.Sprintf("No builds found in %s app.", appID))
4052
}
4153
return nil
4254
}

cmd/builds_test.go

Lines changed: 25 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ foo
2727
assert.NotEqual(t, err, nil, "yaml")
2828
}
2929

30-
func TestBuildsList(t *testing.T) {
30+
func TestBuildsInfo(t *testing.T) {
3131
t.Parallel()
3232
cf, server, err := testutil.NewTestServerAndClient()
3333
if err != nil {
@@ -37,83 +37,32 @@ func TestBuildsList(t *testing.T) {
3737
var b bytes.Buffer
3838
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
3939

40-
server.Mux.HandleFunc("/v2/apps/foo/builds/", func(w http.ResponseWriter, _ *http.Request) {
40+
server.Mux.HandleFunc("/v2/apps/foo/build/", func(w http.ResponseWriter, _ *http.Request) {
4141
testutil.SetHeaders(w)
4242
fmt.Fprintf(w, `{
43-
"count": 2,
44-
"next": null,
45-
"previous": null,
46-
"results": [
47-
{
48-
"app": "",
49-
"created": "2014-01-01T00:00:00UTC",
50-
"dockerfile": "",
51-
"image": "",
52-
"owner": "",
53-
"procfile": {},
54-
"sha": "",
55-
"updated": "",
56-
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
57-
},
58-
{
59-
"app": "",
60-
"created": "2014-01-05T00:00:00UTC",
61-
"dockerfile": "",
62-
"image": "",
63-
"owner": "",
64-
"procfile": {},
65-
"sha": "",
66-
"updated": "",
67-
"uuid": "c4aed81c-d1ca-4ff1-ab89-d2151264e1a3"
68-
}
69-
]
70-
}`)
71-
})
72-
73-
err = cmdr.BuildsList("foo", -1)
74-
assert.NoError(t, err)
75-
assert.Equal(t, b.String(), `OWNER SHA CREATED
76-
<none> <none> 2014-01-01T00:00:00UTC
77-
<none> <none> 2014-01-05T00:00:00UTC
78-
`, "output")
79-
}
80-
81-
func TestBuildsListLimit(t *testing.T) {
82-
t.Parallel()
83-
cf, server, err := testutil.NewTestServerAndClient()
84-
if err != nil {
85-
t.Fatal(err)
86-
}
87-
defer server.Close()
88-
var b bytes.Buffer
89-
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
90-
91-
server.Mux.HandleFunc("/v2/apps/foo/builds/", func(w http.ResponseWriter, _ *http.Request) {
92-
testutil.SetHeaders(w)
93-
fmt.Fprintf(w, `{
94-
"count": 2,
95-
"next": null,
96-
"previous": null,
97-
"results": [
98-
{
99-
"app": "foo",
100-
"created": "2014-01-01T00:00:00UTC",
101-
"dockerfile": "",
102-
"image": "",
103-
"owner": "",
104-
"procfile": {},
105-
"sha": "",
106-
"updated": "",
107-
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
108-
}
109-
]
110-
}`)
43+
"app": "",
44+
"created": "2014-01-01T00:00:00UTC",
45+
"dockerfile": "",
46+
"image": "",
47+
"owner": "",
48+
"procfile": {},
49+
"sha": "",
50+
"updated": "",
51+
"uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
52+
}
53+
`)
11154
})
11255

113-
err = cmdr.BuildsList("foo", 1)
56+
err = cmdr.BuildsInfo("foo", -1)
11457
assert.NoError(t, err)
115-
assert.Equal(t, b.String(), `OWNER SHA CREATED
116-
<none> <none> 2014-01-01T00:00:00UTC
58+
assert.Equal(t, b.String(), `App:
59+
Sha:
60+
UUID: de1bf5b5-4a72-4f94-a10c-d2a3741cdf75
61+
Owner:
62+
Image:
63+
Stack:
64+
Created: 2014-01-01T00:00:00UTC
65+
Updated:
11766
`, "output")
11867
}
11968

@@ -133,7 +82,7 @@ func TestBuildsCreate(t *testing.T) {
13382
err = os.Chdir(name)
13483
assert.NoError(t, err)
13584

136-
server.Mux.HandleFunc("/v2/apps/enterprise/builds/", func(w http.ResponseWriter, r *http.Request) {
85+
server.Mux.HandleFunc("/v2/apps/enterprise/build/", func(w http.ResponseWriter, r *http.Request) {
13786
testutil.SetHeaders(w)
13887
testutil.AssertBody(t, api.CreateBuildRequest{
13988
Image: "ncc/1701:A",
@@ -147,7 +96,7 @@ func TestBuildsCreate(t *testing.T) {
14796
assert.NoError(t, err)
14897
assert.Equal(t, testutil.StripProgress(b.String()), "Creating build... done\n", "output")
14998

150-
server.Mux.HandleFunc("/v2/apps/bradbury/builds/", func(w http.ResponseWriter, r *http.Request) {
99+
server.Mux.HandleFunc("/v2/apps/bradbury/build/", func(w http.ResponseWriter, r *http.Request) {
151100
testutil.SetHeaders(w)
152101
testutil.AssertBody(t, api.CreateBuildRequest{
153102
Image: "nx/72307:latest",
@@ -182,7 +131,7 @@ warp: ./warp 8`
182131
assert.NoError(t, err)
183132
assert.Equal(t, testutil.StripProgress(b.String()), "Creating build... done\n", "output")
184133

185-
server.Mux.HandleFunc("/v2/apps/franklin/builds/", func(w http.ResponseWriter, r *http.Request) {
134+
server.Mux.HandleFunc("/v2/apps/franklin/build/", func(w http.ResponseWriter, r *http.Request) {
186135
testutil.SetHeaders(w)
187136
testutil.AssertBody(t, api.CreateBuildRequest{
188137
Image: "nx/326:latest",

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Commander interface {
3434
TokensList(int) error
3535
TokensAdd(*drycc.Client, string, string, string, string, bool) (*api.AuthTokenResponse, error)
3636
TokensRemove(string, string) error
37-
BuildsList(string, int) error
37+
BuildsInfo(string, int) error
3838
BuildsCreate(string, string, string, string, string) error
3939
CertsList(string, int) error
4040
CertAdd(string, string, string, string) error

cmd/ps.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (d *DryccCmd) PsDescribe(appID, podID string) error {
150150
innerValue = "<none>"
151151
}
152152
}
153-
table.Append([]string{fmt.Sprintf(" %s:", innerKey), strings.Trim(innerValue, `"`)})
153+
table.Append([]string{d.indentString(innerKey, 2), strings.Trim(innerValue, `"`)})
154154
}
155155
}
156156
// LastState
@@ -166,7 +166,7 @@ func (d *DryccCmd) PsDescribe(appID, podID string) error {
166166
innerValue = "<none>"
167167
}
168168
}
169-
table.Append([]string{fmt.Sprintf(" %s:", innerKey), strings.Trim(innerValue, `"`)})
169+
table.Append([]string{d.indentString(innerKey, 2), strings.Trim(innerValue, `"`)})
170170
}
171171
}
172172
table.Append([]string{"Ready:", fmt.Sprintf("%v", containerState.Ready)})
@@ -183,10 +183,10 @@ func (d *DryccCmd) PsDescribe(appID, podID string) error {
183183
// table event
184184
te := d.getDefaultFormatTable([]string{})
185185
te.Append([]string{"Events:"})
186-
te.Append([]string{" REASON", "MESSAGE", "CREATED"})
186+
te.Append([]string{d.indentString("REASON", 2), "MESSAGE", "CREATED"})
187187
for _, ev := range events {
188188
te.Append([]string{
189-
fmt.Sprintf(" %s", ev.Reason),
189+
d.indentString(ev.Reason, 2),
190190
ev.Message,
191191
d.formatTime(ev.Created),
192192
})

cmd/pts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ func printProcessTypeDetail(d *DryccCmd, ptypeStates api.PtypeStates, events api
193193
// table event
194194
te := d.getDefaultFormatTable([]string{})
195195
te.Append([]string{"Events:"})
196-
te.Append([]string{" REASON", "MESSAGE", "CREATED"})
196+
te.Append([]string{d.indentString("REASON", 2), "MESSAGE", "CREATED"})
197197
for _, ev := range events {
198198
te.Append([]string{
199-
fmt.Sprintf(" %s", ev.Reason),
199+
d.indentString(ev.Reason, 2),
200200
ev.Message,
201201
d.formatTime(ev.Created),
202202
})

cmd/releases.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,11 @@ func (d *DryccCmd) ReleasesInfo(appID string, version int) error {
8181
if c.Exception != "" {
8282
exception = c.Exception
8383
}
84-
85-
te.Append([]string{" - created:", d.formatTime(c.Created)})
86-
te.Append([]string{" state:", c.State})
87-
te.Append([]string{" action:", c.Action})
88-
te.Append([]string{" ptypes:", d.wrapString(ptypes)})
89-
te.Append([]string{" exception:", d.wrapString(exception)})
84+
te.Append([]string{d.indentString("- created:", 2), d.formatTime(c.Created)})
85+
te.Append([]string{d.indentString("state:", 4), c.State})
86+
te.Append([]string{d.indentString("action:", 4), c.Action})
87+
te.Append([]string{d.indentString("ptypes:", 4), d.wrapString(ptypes)})
88+
te.Append([]string{d.indentString("exception:", 4), d.wrapString(exception)})
9089
}
9190
te.Render()
9291
}

cmd/utils.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
67
"os"
78
"sort"
9+
"strconv"
810
"strings"
911
"time"
1012

1113
drycc "github.com/drycc/controller-sdk-go"
1214
"github.com/drycc/workflow-cli/pkg/git"
1315
"github.com/drycc/workflow-cli/settings"
1416
"github.com/olekukonko/tablewriter"
17+
yaml "gopkg.in/yaml.v3"
1518
)
1619

1720
var defaultLimit = -1
@@ -133,6 +136,26 @@ func (d *DryccCmd) wrapString(s string) string {
133136
return strings.Join(sa, "\n")
134137
}
135138

139+
// indentString indent s into a paragraph of lines of length lim, with minimal raggedness.
140+
func (d *DryccCmd) indentString(s string, indent int) string {
141+
var lines []string
142+
for _, line := range strings.Split(s, "\n") {
143+
line = strings.TrimRight(line, "\r")
144+
padding := indent + len(line)
145+
lines = append(lines, fmt.Sprintf("% "+strconv.Itoa(padding)+"s", line))
146+
}
147+
return strings.Join(lines, "\n")
148+
}
149+
150+
// toYamlString convert object to yaml string
151+
func (d *DryccCmd) toYamlString(v interface{}, indent int) string {
152+
buf := bytes.Buffer{}
153+
encode := yaml.NewEncoder(&buf)
154+
encode.SetIndent(indent)
155+
encode.Encode(v)
156+
return buf.String()
157+
}
158+
136159
func sortKeys(data map[string]interface{}) *[]string {
137160
keys := make([]string, 0, len(data))
138161
for k := range data {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22
55
require (
66
github.com/containerd/console v1.0.4
77
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
8-
github.com/drycc/controller-sdk-go v0.0.0-20240919014516-72a03dc4e50e
8+
github.com/drycc/controller-sdk-go v0.0.0-20240919122116-8c964e1db024
99
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f
1010
github.com/minio/selfupdate v0.6.0
1111
github.com/olekukonko/tablewriter v0.0.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
77
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
88
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
99
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
10-
github.com/drycc/controller-sdk-go v0.0.0-20240919014516-72a03dc4e50e h1:xIEaewVvwVNTXjoDADApc1WN6cK0/SloIfxMr1x8Hng=
11-
github.com/drycc/controller-sdk-go v0.0.0-20240919014516-72a03dc4e50e/go.mod h1:n6eQe1irJqjwLo/7t9+Dhdv6faSESQN+ATnZRBP3/Uc=
10+
github.com/drycc/controller-sdk-go v0.0.0-20240919122116-8c964e1db024 h1:Pla1jSDekProbHoE0kBpltz+PsqZjLRTVSlnBAQ1ZnA=
11+
github.com/drycc/controller-sdk-go v0.0.0-20240919122116-8c964e1db024/go.mod h1:n6eQe1irJqjwLo/7t9+Dhdv6faSESQN+ATnZRBP3/Uc=
1212
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f h1:kgjvUQJeAszDoU1Vo4vTTE92KI8Av3JPb6Qn890niXg=
1313
github.com/drycc/pkg v0.0.0-20240225112316-78fc9239f51f/go.mod h1:n+QxGif6ha9CEoxVnlipxb9IdmerybcUSzTEDFkvjiA=
1414
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=

0 commit comments

Comments
 (0)