MCPcopy
hub / github.com/docker/docker-agent / runAPICommand

Method runAPICommand

cmd/root/api.go:58–141  ·  view source on GitHub ↗
(cmd *cobra.Command, args []string)

Source from the content-addressed store, hash-verified

56}
57
58func (f *apiFlags) runAPICommand(cmd *cobra.Command, args []string) (commandErr error) {
59 ctx := cmd.Context()
60 telemetry.TrackCommand(ctx, "serve", append([]string{"api"}, args...))
61 defer func() { // do not inline this defer so that commandErr is not resolved early
62 telemetry.TrackCommandError(ctx, "serve", append([]string{"api"}, args...), commandErr)
63 }()
64
65 out := cli.NewPrinter(cmd.OutOrStdout())
66 agentsPath := args[0]
67
68 // Make sure no question is ever asked to the user in api mode.
69 os.Stdin = nil
70
71 // Start fake proxy if --fake is specified
72 cleanup, err := setupFakeProxy(ctx, f.fakeResponses, 0, &f.runConfig)
73 if err != nil {
74 return err
75 }
76 defer func() {
77 if err := cleanup(); err != nil {
78 slog.ErrorContext(ctx, "Failed to cleanup fake proxy", "error", err)
79 }
80 }()
81
82 // Start recording proxy if --record is specified
83 _, recordCleanup, err := setupRecordingProxy(ctx, f.recordPath, &f.runConfig)
84 if err != nil {
85 return err
86 }
87 defer func() {
88 if err := recordCleanup(); err != nil {
89 slog.ErrorContext(ctx, "Failed to cleanup recording proxy", "error", err)
90 }
91 }()
92
93 if f.pullIntervalMins > 0 && !config.IsOCIReference(agentsPath) && !config.IsURLReference(agentsPath) {
94 return errors.New("--pull-interval flag can only be used with OCI or URL references, not local files")
95 }
96
97 if pprofAddr := cmp.Or(f.pprofAddr, os.Getenv("CAGENT_PPROF_ADDR")); pprofAddr != "" {
98 if err := profiling.StartPprofServer(ctx, pprofAddr); err != nil {
99 return err
100 }
101 }
102
103 ln, lnCleanup, err := newListener(ctx, f.listenAddr)
104 if err != nil {
105 return err
106 }
107 defer lnCleanup()
108
109 out.Println("Listening on", ln.Addr().String())
110 warnIfNotLoopback(out, ln.Addr())
111
112 slog.DebugContext(ctx, "Starting server", "agents", agentsPath, "addr", ln.Addr().String())
113
114 // Expand tilde in session database path
115 sessionDB, err := pathx.ExpandHomeDir(f.sessionDB)

Callers

nothing calls this directly

Calls 15

PrintlnMethod · 0.95
CloseMethod · 0.95
TrackCommandFunction · 0.92
TrackCommandErrorFunction · 0.92
NewPrinterFunction · 0.92
IsOCIReferenceFunction · 0.92
IsURLReferenceFunction · 0.92
StartPprofServerFunction · 0.92
NewSQLiteSessionStoreFunction · 0.92
ResolveSourcesFunction · 0.92
NewFunction · 0.92
setupFakeProxyFunction · 0.85

Tested by

no test coverage detected