MCPcopy Index your code
hub / github.com/devspace-sh/devspace / command

Method command

pkg/devspace/server/command.go:17–70  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

15}
16
17func (h *handler) command(w http.ResponseWriter, r *http.Request) {
18 name, ok := r.URL.Query()["name"]
19 if !ok || len(name) != 1 {
20 http.Error(w, "name is missing", http.StatusBadRequest)
21 return
22 }
23
24 ws, err := upgrader.Upgrade(w, r, nil)
25 if err != nil {
26 h.ctx.Log().Errorf("Error upgrading connection: %v", err)
27 http.Error(w, err.Error(), http.StatusInternalServerError)
28 return
29 }
30 defer ws.Close()
31
32 // Open logs connection
33 stream := &wsStream{WebSocket: ws}
34 cmd := exec.Command("devspace", "--namespace", h.ctx.KubeClient().Namespace(), "--kube-context", h.ctx.KubeClient().CurrentContext(), "run", name[0])
35 done := make(chan bool)
36 defer close(done)
37
38 stdinWriter, err := cmd.StdinPipe()
39 if err != nil {
40 return
41 }
42
43 defer stdinWriter.Close()
44
45 cmd.Stdout = stream
46 cmd.Stderr = stream
47
48 go func(done chan bool) {
49 _, _ = io.Copy(stdinWriter, stream)
50
51 select {
52 case <-done:
53 case <-time.After(time.Second):
54 proc := cmd.Process
55 if proc != nil {
56 _ = proc.Kill()
57 }
58 }
59 }(done)
60
61 err = cmd.Run()
62 if err != nil {
63 h.ctx.Log().Errorf("Error in %s: %v", r.URL.String(), err)
64 websocketError(ws, err)
65 return
66 }
67
68 _ = ws.SetWriteDeadline(time.Now().Add(time.Second * 5))
69 _ = ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
70}

Callers

nothing calls this directly

Calls 15

closeFunction · 0.85
websocketErrorFunction · 0.85
KillMethod · 0.80
SetWriteDeadlineMethod · 0.80
UpgradeMethod · 0.65
LogMethod · 0.65
CloseMethod · 0.65
NamespaceMethod · 0.65
KubeClientMethod · 0.65
CurrentContextMethod · 0.65
CopyMethod · 0.65
RunMethod · 0.65

Tested by

no test coverage detected