@@ -5,14 +5,13 @@ package cmd
5
5
6
6
import (
7
7
"embed"
8
- "github.com/mitchellh/mapstructure "
8
+ "gopkg.in/yaml.v3 "
9
9
"net/url"
10
10
"os"
11
11
12
12
"github.com/pb33f/wiretap/shared"
13
13
"github.com/pterm/pterm"
14
14
"github.com/spf13/cobra"
15
- "github.com/spf13/viper"
16
15
)
17
16
18
17
var (
@@ -33,89 +32,24 @@ var (
33
32
34
33
configFlag , _ := cmd .Flags ().GetString ("config" )
35
34
36
- if configFlag == "" {
37
- pterm .Info .Println ("Attempting to locate wiretap configuration..." )
38
- viper .SetConfigFile (".wiretap" )
39
- viper .SetConfigType ("env" )
40
- viper .AddConfigPath ("$HOME/.wiretap" )
41
- viper .AddConfigPath ("." )
42
- } else {
43
- viper .SetConfigFile (configFlag )
44
- }
45
-
46
- cerr := viper .ReadInConfig ()
47
- if cerr != nil && configFlag != "" {
48
- pterm .Error .Printf ("No wiretap configuration located. Using defaults: %s\n " , cerr .Error ())
49
- }
50
- if cerr != nil && configFlag == "" {
51
- pterm .Info .Println ("No wiretap configuration located. Using defaults." )
52
- }
53
- if cerr == nil {
54
- pterm .Info .Printf ("Located configuration file at: %s\n " , viper .ConfigFileUsed ())
55
- }
56
-
57
35
var spec string
58
36
var port string
59
37
var monitorPort string
60
38
var wsPort string
61
39
var staticDir string
62
- var pathConfigurations map [ string ] * shared. WiretapPathConfig
40
+
63
41
var redirectHost string
64
42
var redirectPort string
65
43
var redirectScheme string
66
44
var redirectBasePath string
67
45
var redirectURL string
68
46
var globalAPIDelay int
69
47
70
- // extract from wiretap environment variables.
71
- if viper .IsSet ("PORT" ) {
72
- port = viper .GetString ("PORT" )
73
- }
74
-
75
- if viper .IsSet ("SPEC" ) {
76
- spec = viper .GetString ("SPEC" )
77
- }
78
-
79
- if viper .IsSet ("MONITOR_PORT" ) {
80
- monitorPort = viper .GetString ("MONITOR_PORT" )
81
- }
82
-
83
- if viper .IsSet ("WEBSOCKET_PORT" ) {
84
- wsPort = viper .GetString ("WEBSOCKET_PORT" )
85
- }
86
-
87
- if viper .IsSet ("STATIC_DIR" ) {
88
- staticDir = viper .GetString ("STATIC_DIR" )
89
- }
90
-
91
- if viper .IsSet ("PATHS" ) {
92
- paths := viper .Get ("PATHS" )
93
- var pc map [string ]* shared.WiretapPathConfig
94
- err := mapstructure .Decode (paths , & pc )
95
- if err != nil {
96
- pterm .Error .Printf ("Unable to decode paths from configuration: %s\n " , err .Error ())
97
- } else {
98
- // print out the path configurations.
99
- printLoadedPathConfigurations (pc )
100
- pathConfigurations = pc
101
- }
102
- }
103
-
104
- if viper .IsSet ("REDIRECT_URL" ) {
105
- redirectURL = viper .GetString ("REDIRECT_URL" )
106
- }
107
-
108
- if viper .IsSet ("GLOBAL_API_DELAY" ) {
109
- globalAPIDelay = viper .GetInt ("GLOBAL_API_DELAY" )
110
- }
111
-
112
48
portFlag , _ := cmd .Flags ().GetString ("port" )
113
49
if portFlag != "" {
114
50
port = portFlag
115
51
} else {
116
- if port == "" {
117
- port = "9090" // default
118
- }
52
+ port = "9090" // default
119
53
}
120
54
121
55
specFlag , _ := cmd .Flags ().GetString ("spec" )
127
61
if monitorPortFlag != "" {
128
62
monitorPort = monitorPortFlag
129
63
} else {
130
- if monitorPort == "" {
131
- monitorPort = "9091" // default
132
- }
64
+ monitorPort = "9091" // default
133
65
}
134
66
135
67
staticDirFlag , _ := cmd .Flags ().GetString ("static" )
@@ -141,18 +73,11 @@ var (
141
73
if wsPortFlag != "" {
142
74
wsPort = wsPortFlag
143
75
} else {
144
- if wsPort == "" {
145
- wsPort = "9092" // default
146
- }
76
+ wsPort = "9092" // default
147
77
}
148
78
149
79
redirectURLFlag , _ := cmd .Flags ().GetString ("url" )
150
80
if redirectURLFlag != "" {
151
-
152
- if pathConfigurations != nil {
153
- // warn the user that the path configurations will trump the switch
154
- pterm .Warning .Println ("Using the --url flag will be *overridden* by the path configuration 'target' setting" )
155
- }
156
81
redirectURL = redirectURLFlag
157
82
}
158
83
@@ -161,6 +86,27 @@ var (
161
86
globalAPIDelay = globalAPIDelayFlag
162
87
}
163
88
89
+ var config shared.WiretapConfiguration
90
+ if configFlag != "" {
91
+
92
+ cBytes , err := os .ReadFile (configFlag )
93
+ if err != nil {
94
+ pterm .Error .Printf ("Failed to read wiretap configuration '%s': %s\n " , configFlag , err .Error ())
95
+ return err
96
+ }
97
+ err = yaml .Unmarshal (cBytes , & config )
98
+ if err != nil {
99
+ pterm .Error .Printf ("Failed to parse wiretap configuration '%s': %s\n " , configFlag , err .Error ())
100
+ return err
101
+ }
102
+ pterm .Info .Printf ("Loaded wiretap configuration '%s'...\n \n " , configFlag )
103
+ if config .RedirectURL != "" {
104
+ redirectURL = config .RedirectURL
105
+ }
106
+ } else {
107
+ pterm .Info .Println ("No wiretap configuration located. Using defaults" )
108
+ }
109
+
164
110
if spec == "" {
165
111
pterm .Warning .Println ("No OpenAPI specification provided. " +
166
112
"Please provide a path to an OpenAPI specification using the --spec or -s flags." )
@@ -177,48 +123,64 @@ var (
177
123
return nil
178
124
}
179
125
180
- if redirectURL != "" {
181
- parsedURL , e := url .Parse (redirectURL )
182
- if e != nil {
183
- pterm .Println ()
184
- pterm .Error .Printf ("URL is not valid. " +
185
- "Please provide a valid URL to redirect to. %s cannot be parsed\n \n " , redirectURL )
186
- pterm .Println ()
187
- return nil
188
- }
189
- if parsedURL .Scheme == "" || parsedURL .Host == "" {
190
- pterm .Println ()
191
- pterm .Error .Printf ("URL is not valid. " +
192
- "Please provide a valid URL to redirect to. %s cannot be parsed\n \n " , redirectURL )
193
- pterm .Println ()
194
- return nil
195
- }
196
- redirectHost = parsedURL .Hostname ()
197
- redirectPort = parsedURL .Port ()
198
- redirectScheme = parsedURL .Scheme
199
- redirectBasePath = parsedURL .Path
126
+ parsedURL , e := url .Parse (redirectURL )
127
+ if e != nil {
128
+ pterm .Println ()
129
+ pterm .Error .Printf ("URL is not valid. " +
130
+ "Please provide a valid URL to redirect to. %s cannot be parsed\n \n " , redirectURL )
131
+ pterm .Println ()
132
+ return nil
200
133
}
134
+ if parsedURL .Scheme == "" || parsedURL .Host == "" {
135
+ pterm .Println ()
136
+ pterm .Error .Printf ("URL is not valid. " +
137
+ "Please provide a valid URL to redirect to. %s cannot be parsed\n \n " , redirectURL )
138
+ pterm .Println ()
139
+ return nil
140
+ }
141
+ redirectHost = parsedURL .Hostname ()
142
+ redirectPort = parsedURL .Port ()
143
+ redirectScheme = parsedURL .Scheme
144
+ redirectBasePath = parsedURL .Path
201
145
202
- config := shared. WiretapConfiguration {
203
- Contract : spec ,
204
- RedirectURL : redirectURL ,
205
- RedirectHost : redirectHost ,
206
- RedirectBasePath : redirectBasePath ,
207
- RedirectPort : redirectPort ,
208
- RedirectProtocol : redirectScheme ,
209
- Port : port ,
210
- MonitorPort : monitorPort ,
211
- GlobalAPIDelay : globalAPIDelay ,
212
- WebSocketPort : wsPort ,
213
- StaticDir : staticDir ,
214
- PathConfigurations : pathConfigurations ,
215
- FS : FS ,
146
+ config . Contract = spec
147
+ config . RedirectURL = redirectURL
148
+ config . RedirectHost = redirectHost
149
+ config . RedirectBasePath = redirectBasePath
150
+ config . RedirectPort = redirectPort
151
+ config . RedirectProtocol = redirectScheme
152
+ if config . Port == "" {
153
+ config . Port = port
154
+ }
155
+ if config . MonitorPort == "" {
156
+ config . MonitorPort = monitorPort
157
+ }
158
+ if config . WebSocketPort == "" {
159
+ config . WebSocketPort = wsPort
216
160
}
161
+ if config .GlobalAPIDelay == 0 {
162
+ config .GlobalAPIDelay = globalAPIDelay
163
+ }
164
+ if config .StaticDir == "" {
165
+ config .StaticDir = staticDir
166
+ }
167
+ config .FS = FS
217
168
218
- if len (pathConfigurations ) > 0 {
169
+ if len (config .PathConfigurations ) > 0 {
170
+ printLoadedPathConfigurations (config .PathConfigurations )
219
171
config .CompilePaths ()
220
172
}
221
173
174
+ if config .Headers != nil && len (config .Headers .DropHeaders ) > 0 {
175
+
176
+ pterm .Info .Printf ("Dropping the following %d %s:\n " , len (config .Headers .DropHeaders ),
177
+ shared .Pluralize (len (config .Headers .DropHeaders ), "header" , "headers" ))
178
+ for _ , header := range config .Headers .DropHeaders {
179
+ pterm .Printf ("🗑️ %s\n " , pterm .LightMagenta (header ))
180
+ }
181
+ pterm .Println ()
182
+ }
183
+
222
184
// ready to boot, let's go!
223
185
_ , pErr := runWiretapService (& config )
224
186
0 commit comments