Skip to content

Commit c5d7897

Browse files
authored
chore(ps): support previous pod logs (#65)
* chore(workflow-cli): build create hint if has large change * chore(ps): support previous pod logs
1 parent 84fab3d commit c5d7897

12 files changed

Lines changed: 85 additions & 45 deletions

File tree

cmd/builds.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"os"
56

67
yaml "gopkg.in/yaml.v3"
78

9+
drycc "github.com/drycc/controller-sdk-go"
810
"github.com/drycc/controller-sdk-go/builds"
911
)
1012

@@ -54,7 +56,7 @@ func (d *DryccCmd) BuildsInfo(appID string, version int) error {
5456
}
5557

5658
// BuildsCreate creates a build for an app.
57-
func (d *DryccCmd) BuildsCreate(appID, image, stack, procfile string, dryccfile string) error {
59+
func (d *DryccCmd) BuildsCreate(appID, image, stack, procfile, dryccfile, confirm string) error {
5860
s, appID, err := load(d.ConfigFile, appID)
5961

6062
if err != nil {
@@ -84,7 +86,11 @@ func (d *DryccCmd) BuildsCreate(appID, image, stack, procfile string, dryccfile
8486
return err
8587
}
8688
}
87-
89+
// check procfileMap dryccfileMap stack is exist
90+
err = buildConfirmAction(s.Client, appID, procfileMap, dryccfileMap, confirm)
91+
if err != nil {
92+
return err
93+
}
8894
d.Print("Creating build... ")
8995
quit := progress(d.WOut)
9096
_, err = builds.New(s.Client, appID, image, stack, procfileMap, dryccfileMap)
@@ -108,3 +114,24 @@ func parseDryccfile(dryccfile []byte) (map[string]interface{}, error) {
108114
dryccfileMap := make(map[string]interface{})
109115
return dryccfileMap, yaml.Unmarshal(dryccfile, &dryccfileMap)
110116
}
117+
118+
func buildConfirmAction(c *drycc.Client, appID string, procfileMap map[string]string,
119+
dryccfileMap map[string]interface{}, confirm string) error {
120+
121+
build, _ := builds.Get(c, appID, 0)
122+
123+
if ((len(build.Procfile) != 0 && len(procfileMap) == 0) || (len(build.Dryccfile) != 0 && len(dryccfileMap) == 0)) && (confirm == "" || confirm != "yes") {
124+
// hint
125+
fmt.Printf(` ! WARNING: Potentially Build Create Action
126+
! The Procfile or drycc.yaml is empty, not last time
127+
! To proceed, type "yes" !
128+
129+
> `)
130+
131+
fmt.Scanln(&confirm)
132+
if confirm != "yes" {
133+
return fmt.Errorf("cancel the build create action")
134+
}
135+
}
136+
return nil
137+
}

cmd/builds_test.go

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,32 @@ func TestBuildsCreate(t *testing.T) {
8484

8585
server.Mux.HandleFunc("/v2/apps/enterprise/build/", func(w http.ResponseWriter, r *http.Request) {
8686
testutil.SetHeaders(w)
87-
testutil.AssertBody(t, api.CreateBuildRequest{
88-
Image: "ncc/1701:A",
89-
Stack: "container",
90-
}, r)
91-
w.WriteHeader(http.StatusCreated)
92-
fmt.Fprintf(w, "{}")
87+
if r.Method == "POST" {
88+
testutil.AssertBody(t, api.CreateBuildRequest{
89+
Image: "ncc/1701:A",
90+
Stack: "container",
91+
}, r)
92+
w.WriteHeader(http.StatusCreated)
93+
fmt.Fprintf(w, "{}")
94+
}
9395
})
9496

95-
err = cmdr.BuildsCreate("enterprise", "ncc/1701:A", "container", "", "")
97+
err = cmdr.BuildsCreate("enterprise", "ncc/1701:A", "container", "", "", "yes")
9698
assert.NoError(t, err)
9799
assert.Equal(t, testutil.StripProgress(b.String()), "Creating build... done\n", "output")
98100

99101
server.Mux.HandleFunc("/v2/apps/bradbury/build/", func(w http.ResponseWriter, r *http.Request) {
100102
testutil.SetHeaders(w)
101-
testutil.AssertBody(t, api.CreateBuildRequest{
102-
Image: "nx/72307:latest",
103-
Stack: "container",
104-
Procfile: map[string]string{
105-
"web": "./drive",
106-
"warp": "./warp 8",
107-
},
108-
}, r)
103+
if r.Method == "POST" {
104+
testutil.AssertBody(t, api.CreateBuildRequest{
105+
Image: "nx/72307:latest",
106+
Stack: "container",
107+
Procfile: map[string]string{
108+
"web": "./drive",
109+
"warp": "./warp 8",
110+
},
111+
}, r)
112+
}
109113

110114
w.WriteHeader(http.StatusCreated)
111115
fmt.Fprintf(w, "{}")
@@ -127,28 +131,30 @@ warp: ./warp 8`
127131
}
128132
}()
129133

130-
err = cmdr.BuildsCreate("bradbury", "nx/72307:latest", "container", tmpDir+"/Procfile", "")
134+
err = cmdr.BuildsCreate("bradbury", "nx/72307:latest", "container", tmpDir+"/Procfile", "", "yes")
131135
assert.NoError(t, err)
132136
assert.Equal(t, testutil.StripProgress(b.String()), "Creating build... done\n", "output")
133137

134138
server.Mux.HandleFunc("/v2/apps/franklin/build/", func(w http.ResponseWriter, r *http.Request) {
135139
testutil.SetHeaders(w)
136-
testutil.AssertBody(t, api.CreateBuildRequest{
137-
Image: "nx/326:latest",
138-
Stack: "container",
139-
Procfile: map[string]string{
140-
"web": "./drive",
141-
"warp": "./warp 8",
142-
},
143-
Dryccfile: map[string]interface{}{
144-
"deploy": map[string]interface{}{
145-
"web": map[string]interface{}{
146-
"command": []string{"bash", "-c"},
147-
"args": []string{"bundle exec puma -C config/puma.rb"},
140+
if r.Method == "POST" {
141+
testutil.AssertBody(t, api.CreateBuildRequest{
142+
Image: "nx/326:latest",
143+
Stack: "container",
144+
Procfile: map[string]string{
145+
"web": "./drive",
146+
"warp": "./warp 8",
147+
},
148+
Dryccfile: map[string]interface{}{
149+
"deploy": map[string]interface{}{
150+
"web": map[string]interface{}{
151+
"command": []string{"bash", "-c"},
152+
"args": []string{"bundle exec puma -C config/puma.rb"},
153+
},
148154
},
149155
},
150-
},
151-
}, r)
156+
}, r)
157+
}
152158

153159
w.WriteHeader(http.StatusCreated)
154160
fmt.Fprintf(w, "{}")
@@ -171,7 +177,7 @@ deploy:
171177
`), os.ModePerm)
172178
assert.NoError(t, err)
173179

174-
err = cmdr.BuildsCreate("franklin", "nx/326:latest", "container", "Procfile", "drycc.yaml")
180+
err = cmdr.BuildsCreate("franklin", "nx/326:latest", "container", "Procfile", "drycc.yaml", "yes")
175181
assert.NoError(t, err)
176182
assert.Equal(t, testutil.StripProgress(b.String()), "Creating build... done\n", "output")
177183

cmd/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Commander interface {
3535
TokensAdd(*drycc.Client, string, string, string, string, bool) (*api.AuthTokenResponse, error)
3636
TokensRemove(string, string) error
3737
BuildsInfo(string, int) error
38-
BuildsCreate(string, string, string, string, string) error
38+
BuildsCreate(string, string, string, string, string, string) error
3939
CertsList(string, int) error
4040
CertAdd(string, string, string, string) error
4141
CertRemove(string, string) error
@@ -89,7 +89,7 @@ type Commander interface {
8989
PermUpdate(string, string, string) error
9090
PermDelete(string, string) error
9191
PsList(string, int) error
92-
PsLogs(string, string, int, bool, string) error
92+
PsLogs(string, string, int, bool, string, bool) error
9393
PsExec(string, string, bool, bool, []string) error
9494
PsDescribe(string, string) error
9595
PsDelete(string, []string) error

cmd/ps.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (d *DryccCmd) PsList(appID string, results int) error {
5050
}
5151

5252
// PodLogs returns the logs from an pod.
53-
func (d *DryccCmd) PsLogs(appID, podID string, lines int, follow bool, container string) error {
53+
func (d *DryccCmd) PsLogs(appID, podID string, lines int, follow bool, container string, previous bool) error {
5454
s, appID, err := load(d.ConfigFile, appID)
5555

5656
if err != nil {
@@ -60,6 +60,7 @@ func (d *DryccCmd) PsLogs(appID, podID string, lines int, follow bool, container
6060
Lines: lines,
6161
Follow: follow,
6262
Container: container,
63+
Previous: previous,
6364
}
6465
conn, err := ps.Logs(s.Client, appID, podID, request)
6566
if err != nil {

cmd/ps_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func TestPsLogs(t *testing.T) {
121121
conn.WriteClose(100)
122122
}),
123123
)
124-
err = cmdr.PsLogs("foo", "foo-web-111", 300, true, "runner")
124+
err = cmdr.PsLogs("foo", "foo-web-111", 300, true, "runner", false)
125125
assert.NoError(t, err)
126126
}
127127

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.23
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-20241122013249-f1d6fea5bf81
8+
github.com/drycc/controller-sdk-go v0.0.0-20241128052705-94f8d2dcc087
99
github.com/drycc/pkg v0.0.0-20241028091700-8caca001c932
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
@@ -9,8 +9,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
99
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1010
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
1111
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
12-
github.com/drycc/controller-sdk-go v0.0.0-20241122013249-f1d6fea5bf81 h1:y809MASIviVNxzUxP1CDU2EaB5AruRNAiFKjOMCx+0s=
13-
github.com/drycc/controller-sdk-go v0.0.0-20241122013249-f1d6fea5bf81/go.mod h1:F9ViuoVcZ/OG1WnfZBYH6j4v6mb5B+RW9FLaCopADoo=
12+
github.com/drycc/controller-sdk-go v0.0.0-20241128052705-94f8d2dcc087 h1:cbniB2KxxeV72y/7KILVorTTxlzCF41xtBysDT1FjJg=
13+
github.com/drycc/controller-sdk-go v0.0.0-20241128052705-94f8d2dcc087/go.mod h1:F9ViuoVcZ/OG1WnfZBYH6j4v6mb5B+RW9FLaCopADoo=
1414
github.com/drycc/pkg v0.0.0-20241028091700-8caca001c932 h1:tUfkfjmFmlm9n0dYp1NoHcFK6+APD/wHZ2We6CKXDSI=
1515
github.com/drycc/pkg v0.0.0-20241028091700-8caca001c932/go.mod h1:upGcZF/RCEqidGjSnWHh/txRC1I4GPsRGvF8/+bDfwU=
1616
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=

parser/builds.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ Options:
8888
A YAML file used to supply a Procfile to the application.
8989
-d --dryccfile=<dryccfile>
9090
A YAML file used to supply a drycc.yaml to the application.
91+
--confirm=yes
92+
To proceed, type "yes".
9193
`
9294

9395
args, err := docopt.ParseArgs(usage, argv, "")
@@ -99,11 +101,12 @@ Options:
99101
app := safeGetString(args, "--app")
100102
image := safeGetString(args, "<image>")
101103
stack := safeGetString(args, "--stack")
104+
confirm := safeGetString(args, "--confirm")
102105
if stack == "" {
103106
stack = "container"
104107
}
105108
procfile := safeGetValue(args, "--procfile", "Procfile")
106109
dryccfile := safeGetValue(args, "--dryccfile", "drycc.yaml")
107110

108-
return cmdr.BuildsCreate(app, image, stack, procfile, dryccfile)
111+
return cmdr.BuildsCreate(app, image, stack, procfile, dryccfile, confirm)
109112
}

parser/builds_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (d FakeDryccCmd) BuildsInfo(string, int) error {
1313
return errors.New("builds:info")
1414
}
1515

16-
func (d FakeDryccCmd) BuildsCreate(string, string, string, string, string) error {
16+
func (d FakeDryccCmd) BuildsCreate(string, string, string, string, string, string) error {
1717
return errors.New("builds:create")
1818
}
1919

parser/ps.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ Options:
8686
specify if the logs should be streamed.
8787
-c --container=<container>
8888
print the logs of this container.
89+
--previous
90+
print the logs for the previous instance of the container in a pod if it exists.
8991
`
9092

9193
args, err := docopt.ParseArgs(usage, argv, "")
@@ -104,8 +106,9 @@ Options:
104106
follow := safeGetBool(args, "--follow")
105107
podID := safeGetString(args, "<pod>")
106108
container := safeGetString(args, "--container")
109+
previous := args["--previous"].(bool)
107110

108-
return cmdr.PsLogs(app, podID, lines, follow, container)
111+
return cmdr.PsLogs(app, podID, lines, follow, container, previous)
109112
}
110113

111114
func psExec(argv []string, cmdr cmd.Commander) error {

0 commit comments

Comments
 (0)