| 1 | + | package cmd |
| 2 | + | |
| 3 | + | import ( |
| 4 | + | "fmt" |
| 5 | + | "github.com/liamg/dismember/pkg/proc" |
| 6 | + | "github.com/spf13/cobra" |
| 7 | + | "io" |
| 8 | + | ) |
| 9 | + | |
| 10 | + | func init() { |
| 11 | + | treeCmd := &cobra.Command{ |
| 12 | + | Use: "tree", |
| 13 | + | Short: "Show a tree diagram of a process and all children (defaults to PID 1).", |
| 14 | + | Long: ``, |
| 15 | + | RunE: treeHandler, |
| 16 | + | Args: cobra.ExactArgs(0), |
| 17 | + | } |
| 18 | + | treeCmd.Flags().IntVarP(&flagPID, "pid", "p", 1, "PID of the process to analyse") |
| 19 | + | rootCmd.AddCommand(treeCmd) |
| 20 | + | |
| 21 | + | } |
| 22 | + | |
| 23 | + | type procWithStatus struct { |
| 24 | + | process proc.Process |
| 25 | + | status proc.Status |
| 26 | + | } |
| 27 | + | |
| 28 | + | func treeHandler(cmd *cobra.Command, _ []string) error { |
| 29 | + | |
| 30 | + | processes, err := proc.List(true) |
| 31 | + | if err != nil { |
| 32 | + | return err |
| 33 | + | } |
| 34 | + | |
| 35 | + | root := proc.Process(flagPID) |
| 36 | + | status, err := root.Status() |
| 37 | + | if err != nil { |
| 38 | + | return err |
| 39 | + | } |
| 40 | + | rootWithStatus := procWithStatus{ |
| 41 | + | process: root, |
| 42 | + | status: *status, |
| 43 | + | } |
| 44 | + | |
| 45 | + | var all []procWithStatus |
| 46 | + | for _, process := range processes { |
| 47 | + | status, err := process.Status() |
| 48 | + | if err != nil { |
| 49 | + | continue |
| 50 | + | } |
| 51 | + | all = append(all, procWithStatus{ |
| 52 | + | process: process, |
| 53 | + | status: *status, |
| 54 | + | }) |
| 55 | + | } |
| 56 | + | |
| 57 | + | drawBranch(cmd.OutOrStdout(), rootWithStatus, nil, all) |
| 58 | + | return nil |
| 59 | + | } |
| 60 | + | |
| 61 | + | func drawBranch(w io.Writer, parent procWithStatus, lasts []bool, all []procWithStatus) { |
| 62 | + | |
| 63 | + | var children []procWithStatus |
| 64 | + | for _, process := range all { |
| 65 | + | if process.status.Parent != parent.process { |
| 66 | + | continue |
| 67 | + | } |
| 68 | + | children = append(children, process) |
| 69 | + | } |
| 70 | + | |
| 71 | + | var done bool |
| 72 | + | _, _ = fmt.Fprint(w, ansiDim) |
| 73 | + | if len(lasts) > 1 { |
| 74 | + | for _, last := range lasts[1:] { |
| 75 | + | if !last { |
| 76 | + | _, _ = fmt.Fprint(w, " │ ") |
| 77 | + | } else if !done { |
| 78 | + | done = true |
| 79 | + | _, _ = fmt.Fprint(w, " │ ") |
| 80 | + | } else { |
| 81 | + | _, _ = fmt.Fprint(w, " ") |
| 82 | + | } |
| 83 | + | } |
| 84 | + | } |
| 85 | + | |
| 86 | + | if len(lasts) > 0 { |
| 87 | + | symbol := '├' |
| 88 | + | if lasts[len(lasts)-1] { |
| 89 | + | symbol = '└' |
| 90 | + | } |
| 91 | + | _, _ = fmt.Fprintf(w, " %c─ ", symbol) |
| 92 | + | } |
| 93 | + | |
| 94 | + | _, _ = fmt.Fprint(w, ansiReset) |
| 95 | + | _, _ = fmt.Fprintf(w, "%s %s(%s%d%s)%s\n", parent.status.Name, ansiDim, ansiReset, parent.process, ansiDim, ansiReset) |
| 96 | + | |
| 97 | + | for i, child := range children { |
| 98 | + | drawBranch(w, child, append(lasts, i == len(children)-1), all) |
| 99 | + | } |
| 100 | + | } |
| 101 | + | |