Skip to content

Commit f3e700c

Browse files
committed
feat(cli): update gateway and route commands for new API
- Update gateway and route commands to work with new API requiring app field - Simplify command structure and remove redundant parameters - Update parser to handle new command format - Update tests to match new API behavior - Update i18n translations for command descriptions - Update go.mod dependencies
1 parent c926d87 commit f3e700c

12 files changed

Lines changed: 232 additions & 471 deletions

File tree

g.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: demo01
2+
ports:
3+
- port: 80
4+
protocol: HTTP
5+
- port: 8000
6+
protocol: TCP
7+
- port: 8001
8+
protocol: HTTP

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.26
55
require (
66
github.com/chai2010/gettext-go v1.0.3
77
github.com/containerd/console v1.0.4
8-
github.com/drycc/controller-sdk-go v0.0.0-20260527062908-7d751e6440b0
8+
github.com/drycc/controller-sdk-go v0.0.0-20260528005024-dd88dda33163
99
github.com/drycc/pkg v0.0.0-20250917064731-345368da3dbf
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/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6N
99
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
1010
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
1111
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12-
github.com/drycc/controller-sdk-go v0.0.0-20260527062908-7d751e6440b0 h1:LBGTmWRopu4y0i8DIgRaTnr9oycPlRVo7905yOJSPWw=
13-
github.com/drycc/controller-sdk-go v0.0.0-20260527062908-7d751e6440b0/go.mod h1:jV1AUDHtY8aPMF95evHQGXZOX6tUXaf7wgqzUEnD5SM=
12+
github.com/drycc/controller-sdk-go v0.0.0-20260528005024-dd88dda33163 h1:5Cy3YMCvVxoZRZspl0VtqiYAibZi1lfv79Af5a6vPcA=
13+
github.com/drycc/controller-sdk-go v0.0.0-20260528005024-dd88dda33163/go.mod h1:jV1AUDHtY8aPMF95evHQGXZOX6tUXaf7wgqzUEnD5SM=
1414
github.com/drycc/pkg v0.0.0-20250917064731-345368da3dbf h1:CYy3NoPhfFhkGAbEppTOQfY/HC2s0FJDcBgbtRKeweg=
1515
github.com/drycc/pkg v0.0.0-20250917064731-345368da3dbf/go.mod h1:BrrNrNskHKm+nJYhXfGuI114w8nupi0AMo8QZHID7CM=
1616
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=

internal/commands/commands.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,13 @@ type Commander interface {
5858
ServicesList(string) error
5959
ServicesAdd(string, string, string, string) error
6060
ServicesRemove(string, string, string, int) error
61-
GatewaysAdd(string, string, int, string) error
6261
GatewaysList(string, int) error
63-
GatewaysRemove(string, string, int, string) error
64-
RoutesCreate(string, string, string, ...api.BackendRefRequest) error
62+
GatewaysInfo(string, string) error
63+
GatewaysApply(string, string) error
64+
GatewaysRemove(string, string) error
6565
RoutesList(string, int) error
66-
RoutesGet(string, string) error
67-
RoutesSet(string, string, string) error
68-
RoutesAttach(string, string, int, string) error
69-
RoutesDetach(string, string, int, string) error
66+
RoutesInfo(string, string) error
67+
RoutesApply(string, string) error
7068
RoutesRemove(string, string) error
7169
GitRemote(string, string, bool) error
7270
GitRemove(string) error

internal/commands/gateways.go

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package commands
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
7+
"os"
58
"strings"
69

710
"github.com/drycc/controller-sdk-go/api"
811
"github.com/drycc/controller-sdk-go/gateways"
912
"github.com/drycc/workflow-cli/internal/loader"
13+
"sigs.k8s.io/yaml"
1014
)
1115

1216
// GatewaysList lists gateways for the app
@@ -26,28 +30,73 @@ func (d *DryccCmd) GatewaysList(appID string, results int) error {
2630
if count == 0 {
2731
d.Println(fmt.Sprintf("No gateways found in %s app.", appID))
2832
} else {
29-
table := d.getDefaultFormatTable([]string{"NAME", "LISENTER", "PORT", "PROTOCOL", "ADDRESSES"})
33+
table := d.getDefaultFormatTable([]string{"NAME", "PORT", "PROTOCOL", "ADDRESSES"})
3034
for _, gateway := range gateways {
3135
addresesStr := parseAddress(gateway.Addresses)
32-
for _, listener := range gateway.Listeners {
33-
table.Append([]string{gateway.Name, listener.Name, fmt.Sprint(listener.Port), listener.Protocol, addresesStr})
36+
for _, port := range gateway.Ports {
37+
table.Append([]string{gateway.Name, fmt.Sprint(port.Port), port.Protocol, addresesStr})
3438
}
3539
}
3640
table.Render()
3741
}
3842
return nil
3943
}
4044

41-
// GatewaysAdd adds a gateway to an app.
42-
func (d *DryccCmd) GatewaysAdd(appID, name string, port int, protocol string) error {
45+
// GatewaysInfo shows detailed information about a gateway.
46+
func (d *DryccCmd) GatewaysInfo(appID, name string) error {
4347
appID, s, err := loader.LoadAppSettings(d.ConfigFile, appID)
4448
if err != nil {
4549
return err
4650
}
47-
d.Printf("Adding gateway %s to %s... ", name, appID)
51+
52+
info, err := gateways.Info(s.Client, appID, name)
53+
if d.checkAPICompatibility(s.Client, err) != nil {
54+
return err
55+
}
56+
57+
jsonBytes, err := json.Marshal(info)
58+
if err != nil {
59+
return err
60+
}
61+
yamlBytes, err := yaml.JSONToYAML(jsonBytes)
62+
if err != nil {
63+
return err
64+
}
65+
d.Println(string(yamlBytes))
66+
return nil
67+
}
68+
69+
// GatewaysApply applies gateway configuration from a YAML file.
70+
func (d *DryccCmd) GatewaysApply(appID, filePath string) error {
71+
appID, s, err := loader.LoadAppSettings(d.ConfigFile, appID)
72+
if err != nil {
73+
return err
74+
}
75+
76+
yamlData, err := os.ReadFile(filePath)
77+
if err != nil {
78+
return err
79+
}
80+
81+
jsonData, err := yaml.YAMLToJSON(yamlData)
82+
if err != nil {
83+
return err
84+
}
85+
86+
var req api.GatewayApplyRequest
87+
decoder := json.NewDecoder(bytes.NewReader(jsonData))
88+
decoder.DisallowUnknownFields()
89+
if err := decoder.Decode(&req); err != nil {
90+
return fmt.Errorf("invalid gateway configuration: %w", err)
91+
}
92+
if req.Name == "" {
93+
return fmt.Errorf("invalid gateway configuration: missing name")
94+
}
95+
96+
d.Printf("Applying gateway %s to %s... ", req.Name, appID)
4897

4998
quit := progress(d.WOut)
50-
err = gateways.New(s.Client, appID, name, port, protocol)
99+
_, err = gateways.Apply(s.Client, appID, req)
51100
quit <- true
52101
<-quit
53102
if d.checkAPICompatibility(s.Client, err) != nil {
@@ -58,16 +107,16 @@ func (d *DryccCmd) GatewaysAdd(appID, name string, port int, protocol string) er
58107
return nil
59108
}
60109

61-
// GatewaysRemove removes a gateway registered with an app.
62-
func (d *DryccCmd) GatewaysRemove(appID, name string, port int, protocol string) error {
110+
// GatewaysRemove removes a gateway from an app.
111+
func (d *DryccCmd) GatewaysRemove(appID, name string) error {
63112
appID, s, err := loader.LoadAppSettings(d.ConfigFile, appID)
64113
if err != nil {
65114
return err
66115
}
67-
d.Printf("Removing gateway %s to %s... ", name, appID)
116+
d.Printf("Removing gateway %s from %s... ", name, appID)
68117

69118
quit := progress(d.WOut)
70-
err = gateways.Delete(s.Client, appID, name, port, protocol)
119+
err = gateways.Delete(s.Client, appID, name)
71120
quit <- true
72121
<-quit
73122
if d.checkAPICompatibility(s.Client, err) != nil {

internal/commands/gateways_test.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"net/http"
7+
"os"
78
"testing"
89

910
"github.com/drycc/controller-sdk-go/api"
@@ -32,20 +33,15 @@ func TestGatewaysList(t *testing.T) {
3233
"app": "foo",
3334
"name": "foo",
3435
"created": "2023-04-19T00:00:00UTC",
35-
"owner": "test",
3636
"updated": "2023-04-19T00:00:00UTC",
37-
"listeners": [
37+
"ports": [
3838
{
39-
"name": "foo-80-http",
4039
"port": 80,
41-
"protocol": "HTTP",
42-
"allowedRoutes": {"namespaces": {"from": "All"}}
40+
"protocol": "HTTP"
4341
},
4442
{
45-
"name": "foo-443-https",
4643
"port": 443,
47-
"protocol": "HTTPS",
48-
"allowedRoutes": {"namespaces": {"from": "All"}}
44+
"protocol": "HTTPS"
4945
}
5046
],
5147
"addresses": [
@@ -62,13 +58,13 @@ func TestGatewaysList(t *testing.T) {
6258
err = cmdr.GatewaysList("foo", -1)
6359
assert.NoError(t, err)
6460

65-
assert.Equal(t, b.String(), `NAME LISENTER PORT PROTOCOL ADDRESSES
66-
foo foo-80-http 80 HTTP 192.168.11.1
67-
foo foo-443-https 443 HTTPS 192.168.11.1
61+
assert.Equal(t, b.String(), `NAME PORT PROTOCOL ADDRESSES
62+
foo 80 HTTP 192.168.11.1
63+
foo 443 HTTPS 192.168.11.1
6864
`, "output")
6965
}
7066

71-
func TestGatewaysAdd(t *testing.T) {
67+
func TestGatewaysApply(t *testing.T) {
7268
t.Parallel()
7369
cf, server, err := testutil.NewTestServerAndClient()
7470
if err != nil {
@@ -78,21 +74,27 @@ func TestGatewaysAdd(t *testing.T) {
7874
var b bytes.Buffer
7975
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
8076

81-
server.Mux.HandleFunc("/v2/apps/foo/gateways/", func(w http.ResponseWriter, r *http.Request) {
82-
testutil.AssertBody(t, api.GatewayCreateRequest{Name: "example-go", Port: 443, Protocol: "HTTPS"}, r)
77+
server.Mux.HandleFunc("/v2/apps/foo/gateways/example-go/", func(w http.ResponseWriter, r *http.Request) {
78+
testutil.AssertBody(t, api.GatewayApplyRequest{Name: "example-go", Ports: []api.GatewayPort{{Port: 443, Protocol: "HTTPS"}}}, r)
8379
testutil.SetHeaders(w)
84-
w.WriteHeader(http.StatusCreated)
85-
// Body isn't used by CLI, so it isn't set.
80+
w.WriteHeader(http.StatusOK)
8681
w.Write([]byte("{}"))
8782
})
8883

89-
err = cmdr.GatewaysAdd("foo", "example-go", 443, "HTTPS")
84+
gatewayFile, err := os.CreateTemp("", "gateway.yaml")
85+
assert.NoError(t, err)
86+
defer os.Remove(gatewayFile.Name())
87+
_, err = gatewayFile.Write([]byte("name: example-go\nports:\n- port: 443\n protocol: HTTPS\n"))
88+
assert.NoError(t, err)
89+
gatewayFile.Close()
90+
91+
err = cmdr.GatewaysApply("foo", gatewayFile.Name())
9092
assert.NoError(t, err)
9193

92-
assert.Equal(t, testutil.StripProgress(b.String()), "Adding gateway example-go to foo... done\n", "output")
94+
assert.Equal(t, testutil.StripProgress(b.String()), "Applying gateway example-go to foo... done\n", "output")
9395
}
9496

95-
func TestGatewaysDelete(t *testing.T) {
97+
func TestGatewaysRemove(t *testing.T) {
9698
t.Parallel()
9799
cf, server, err := testutil.NewTestServerAndClient()
98100
if err != nil {
@@ -102,13 +104,13 @@ func TestGatewaysDelete(t *testing.T) {
102104
var b bytes.Buffer
103105
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
104106

105-
server.Mux.HandleFunc("/v2/apps/foo/gateways/", func(w http.ResponseWriter, _ *http.Request) {
107+
server.Mux.HandleFunc("/v2/apps/foo/gateways/example-go/", func(w http.ResponseWriter, _ *http.Request) {
106108
testutil.SetHeaders(w)
107109
w.WriteHeader(http.StatusNoContent)
108110
})
109111

110-
err = cmdr.GatewaysRemove("foo", "example-go", 443, "HTTPS")
112+
err = cmdr.GatewaysRemove("foo", "example-go")
111113
assert.NoError(t, err)
112114

113-
assert.Equal(t, testutil.StripProgress(b.String()), "Removing gateway example-go to foo... done\n", "output")
115+
assert.Equal(t, testutil.StripProgress(b.String()), "Removing gateway example-go from foo... done\n", "output")
114116
}

0 commit comments

Comments
 (0)