Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cmd_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"github.com/urfave/cli"
)

type cmdStatusHandler struct{ dialer }
type cmdStatusHandler struct {
dialer
JSON bool
}

func (h *cmdStatusHandler) Run(c *cli.Context) error {
if c.Args().Present() {
Expand All @@ -19,7 +22,11 @@ func (h *cmdStatusHandler) Run(c *cli.Context) error {
conn, err := h.Dial()
utils.FatalOnErr(err)

fmt.Fprintln(conn, "status")
if h.JSON {
fmt.Fprintln(conn, "status-json")
} else {
fmt.Fprintln(conn, "status")
}

utils.ScanLines(conn, func(b []byte) bool {
fmt.Fprintf(os.Stdout, "%s\n", b)
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ func setupStatusCmd() cli.Command {
Aliases: []string{"ps"},
Usage: "Prints process statuses",
Action: c.Run,
Flags: socketFlags(&c.SocketPath, &c.Network),
Flags: append(
[]cli.Flag{
cli.BoolFlag{Name: "json, j", Usage: "Output in JSON format", Destination: &c.JSON},
},
socketFlags(&c.SocketPath, &c.Network)...,
),
}
}

Expand Down
1 change: 1 addition & 0 deletions start/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func newCommand(h *Handler) (*command, error) {
(h.AnyCanDie || utils.StringsContain(canDie, e.OrigName)),
(utils.StringsContain(autoRestart, e.OrigName) || utils.StringsContain(autoRestart, "all")),
e.StopSignal,
e.Port,
))
}
}
Expand Down
39 changes: 33 additions & 6 deletions start/command_center.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package start

import (
"encoding/json"
"fmt"
"net"
"regexp"
Expand Down Expand Up @@ -94,6 +95,8 @@ func (c *commandCenter) handleConnection(conn net.Conn) {
c.processEcho(conn)
case "status":
c.processStatus(conn)
case "status-json":
c.processStatusJSON(conn)
}

return true
Expand Down Expand Up @@ -169,6 +172,13 @@ func (c *commandCenter) processEcho(conn net.Conn) {
c.cmd.output.Echo(conn)
}

func processStatusString(p *process) string {
if p.dead || p.keepingAlive {
return "dead"
}
return "running"
}

func (c *commandCenter) processStatus(conn net.Conn) {
maxNameLen := 9
for _, p := range c.cmd.processes {
Expand All @@ -181,20 +191,37 @@ func (c *commandCenter) processStatus(conn net.Conn) {
for i := maxNameLen - len(headerProcess); i > -1; i-- {
conn.Write([]byte{' '})
}

fmt.Fprint(conn, "PORT ")
fmt.Fprint(conn, headerPid)
fmt.Fprint(conn, " ")
fmt.Fprintln(conn, headerStatus)

for _, p := range c.cmd.processes {
utils.FprintRpad(conn, p.Name, maxNameLen+1)
utils.FprintRpad(conn, strconv.Itoa(p.Port), 10)
utils.FprintRpad(conn, strconv.Itoa(p.pid), 10)
fmt.Fprintln(conn, processStatusString(p))
}
conn.Close()
}

if p.dead || p.keepingAlive {
fmt.Fprintln(conn, "dead")
} else {
fmt.Fprintln(conn, "running")
}
type processStatusEntry struct {
Name string `json:"name"`
Port int `json:"port"`
PID int `json:"pid"`
Status string `json:"status"`
}

func (c *commandCenter) processStatusJSON(conn net.Conn) {
entries := make([]processStatusEntry, 0, len(c.cmd.processes))
for _, p := range c.cmd.processes {
entries = append(entries, processStatusEntry{
Name: p.Name,
Port: p.Port,
PID: p.pid,
Status: processStatusString(p),
})
}
json.NewEncoder(conn).Encode(entries)
conn.Close()
}
4 changes: 3 additions & 1 deletion start/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ type process struct {
Name string
Color int
Command string
Port int
}

func newProcess(tmux *tmuxClient, name string, color int, command string, output *multiOutput, canDie bool, autoRestart bool, stopSignal syscall.Signal) *process {
func newProcess(tmux *tmuxClient, name string, color int, command string, output *multiOutput, canDie bool, autoRestart bool, stopSignal syscall.Signal, port int) *process {
out, in := io.Pipe()

proc := &process{
Expand All @@ -56,6 +57,7 @@ func newProcess(tmux *tmuxClient, name string, color int, command string, output
Name: name,
Color: color,
Command: command,
Port: port,
}

tmux.AddProcess(proc)
Expand Down