MCPcopy
hub / github.com/cloudflare/cloudflared / Run

Function Run

cmd/cloudflared/tail/cmd.go:263–394  ·  view source on GitHub ↗

Run implements a foreground runner

(c *cli.Context)

Source from the content-addressed store, hash-verified

261
262// Run implements a foreground runner
263func Run(c *cli.Context) error {
264 log := cliutil.CreateStderrLogger(c)
265
266 signals := make(chan os.Signal, 10)
267 signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT)
268 defer signal.Stop(signals)
269
270 output := "default"
271 switch c.String("output") {
272 case "default", "":
273 output = "default"
274 case "json":
275 output = "json"
276 default:
277 log.Err(errors.New("invalid --output value provided, please make sure it is one of: default, json")).Send()
278 }
279
280 filters, err := parseFilters(c)
281 if err != nil {
282 log.Error().Err(err).Msgf("invalid filters provided")
283 return nil
284 }
285
286 u, err := buildURL(c, log, cfapi.Logs)
287 if err != nil {
288 log.Err(err).Msg("unable to construct management request URL")
289 return nil
290 }
291
292 header := make(http.Header)
293 header.Add("User-Agent", buildInfo.UserAgent())
294 trace := c.String("trace")
295 if trace != "" {
296 header["cf-trace-id"] = []string{trace}
297 }
298 ctx := c.Context
299 // nolint: bodyclose
300 conn, resp, err := websocket.Dial(ctx, u.String(), &websocket.DialOptions{
301 HTTPHeader: header,
302 })
303 if err != nil {
304 if resp != nil && resp.StatusCode != http.StatusSwitchingProtocols {
305 handleValidationError(resp, log)
306 return nil
307 }
308 log.Error().Err(err).Msgf("unable to start management log streaming session")
309 return nil
310 }
311 defer conn.Close(websocket.StatusInternalError, "management connection was closed abruptly")
312
313 // Once connection is established, send start_streaming event to begin receiving logs
314 err = management.WriteEvent(conn, ctx, &management.EventStartStreaming{
315 ClientEvent: management.ClientEvent{Type: management.StartStreaming},
316 Filters: filters,
317 })
318 if err != nil {
319 log.Error().Err(err).Msg("unable to request logs from management tunnel")
320 return nil

Callers

nothing calls this directly

Calls 15

CreateStderrLoggerFunction · 0.92
WriteEventFunction · 0.92
ReadServerEventFunction · 0.92
AsClosedFunction · 0.92
IntoServerEventFunction · 0.92
parseFiltersFunction · 0.85
buildURLFunction · 0.85
handleValidationErrorFunction · 0.85
printJSONFunction · 0.85
printLineFunction · 0.85
NotifyMethod · 0.80
UserAgentMethod · 0.80

Tested by

no test coverage detected