| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "text/tabwriter" |
| 10 | |
| 11 | "github.com/containers/psgo" |
| 12 | ) |
| 13 | |
| 14 | func main() { |
| 15 | var ( |
| 16 | descriptors []string |
| 17 | pidsList []string |
| 18 | data [][]string |
| 19 | err error |
| 20 | |
| 21 | pids = flag.String("pids", "", "comma separated list of process IDs to retrieve") |
| 22 | format = flag.String("format", "", "ps(1) AIX format comma-separated string") |
| 23 | list = flag.Bool("list", false, "list all supported descriptors") |
| 24 | join = flag.Bool("join", false, "join namespace of provided pids (containers)") |
| 25 | fillMappings = flag.Bool("fill-mappings", false, "fill the UID and GID mappings with the current user namespace") |
| 26 | ) |
| 27 | |
| 28 | log.SetPrefix("psgo: ") |
| 29 | log.SetFlags(0) |
| 30 | |
| 31 | flag.Parse() |
| 32 | |
| 33 | if *fillMappings && !*join { |
| 34 | log.Fatal("-fill-mappings requires -join") |
| 35 | } |
| 36 | |
| 37 | if *list { |
| 38 | fmt.Println(strings.Join(psgo.ListDescriptors(), ", ")) |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | if *format != "" { |
| 43 | descriptors = strings.Split(*format, ",") |
| 44 | } |
| 45 | |
| 46 | if *pids != "" { |
| 47 | pidsList = strings.Split(*pids, ",") |
| 48 | } |
| 49 | |
| 50 | if len(pidsList) > 0 { |
| 51 | opts := psgo.JoinNamespaceOpts{FillMappings: *fillMappings} |
| 52 | |
| 53 | if *join { |
| 54 | data, err = psgo.JoinNamespaceAndProcessInfoByPidsWithOptions(pidsList, descriptors, &opts) |
| 55 | } else { |
| 56 | data, err = psgo.ProcessInfoByPids(pidsList, descriptors) |
| 57 | } |
| 58 | if err != nil { |
| 59 | log.Panic(err) |
| 60 | } |
| 61 | } else { |
| 62 | data, err = psgo.ProcessInfo(descriptors) |
| 63 | if err != nil { |
| 64 | log.Panic(err) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | tw := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0) |
| 69 | for _, d := range data { |
| 70 | _, _ = fmt.Fprintln(tw, strings.Join(d, "\t")) |
| 71 | } |
| 72 | _ = tw.Flush() |
| 73 | } |