@@ -2,9 +2,12 @@ package controller
22
33import (
44 "bytes"
5+ "fmt"
56 "io"
67 "strings"
8+ "unicode"
79
10+ "github.com/ktr0731/evans/entity"
811 "github.com/ktr0731/evans/usecase/port"
912 "github.com/pkg/errors"
1013)
@@ -109,7 +112,7 @@ func (c *showCommand) Synopsis() string {
109112}
110113
111114func (c * showCommand ) Help () string {
112- return "usage: show <package | service | message | rpc>"
115+ return "usage: show <package | service | message | rpc | header >"
113116}
114117
115118func (c * showCommand ) Validate (args []string ) error {
@@ -132,6 +135,8 @@ func (c *showCommand) Run(args []string) (string, error) {
132135 params .Type = port .ShowTypeMessage
133136 case "a" , "r" , "rpc" , "api" :
134137 params .Type = port .ShowTypeRPC
138+ case "h" , "header" , "headers" :
139+ params .Type = port .ShowTypeHeader
135140 default :
136141 return "" , errors .Wrap (ErrUnknownTarget , target )
137142 }
@@ -170,6 +175,53 @@ func (c *callCommand) Run(args []string) (string, error) {
170175 return read (res )
171176}
172177
178+ type headerCommand struct {
179+ inputPort port.InputPort
180+ }
181+
182+ func (c * headerCommand ) Synopsis () string {
183+ return "set/unset headers to each request. if header value is empty, the header is removed."
184+ }
185+
186+ func (c * headerCommand ) Help () string {
187+ return "usage: header <key>=<value>[, <key>=<value>...]"
188+ }
189+
190+ func (c * headerCommand ) Validate (args []string ) error {
191+ if len (args ) < 1 {
192+ return errors .Wrap (ErrArgumentRequired , "<key>=<value> or <key>" )
193+ }
194+ return nil
195+ }
196+
197+ func (c * headerCommand ) Run (args []string ) (string , error ) {
198+ headers := []* entity.Header {}
199+ for _ , h := range args {
200+ sp := strings .SplitN (h , "=" , 2 )
201+ header := & entity.Header {
202+ Key : sp [0 ],
203+ }
204+ for _ , r := range sp [0 ] {
205+ if ! unicode .IsLetter (r ) && ! unicode .IsDigit (r ) && r != '-' && r != '_' && r != '.' {
206+ return "" , fmt .Errorf ("invalid char in key: %c" , r )
207+ }
208+ }
209+ // remove the key
210+ if len (sp ) == 1 || sp [1 ] == "" {
211+ header .NeedToRemove = true
212+ } else {
213+ header .Val = sp [1 ]
214+ }
215+ headers = append (headers , header )
216+ }
217+ params := & port.HeaderParams {headers }
218+ res , err := c .inputPort .Header (params )
219+ if err != nil {
220+ return "" , err
221+ }
222+ return read (res )
223+ }
224+
173225func read (r io.Reader ) (string , error ) {
174226 if r == nil {
175227 return "" , nil
0 commit comments