11package commands
22
33import (
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 {
0 commit comments