File: sample.go

package info (click to toggle)
golang-github-containers-psgo 1.7.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 372 kB
  • sloc: makefile: 46
file content (73 lines) | stat: -rw-r--r-- 1,589 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"strings"
	"text/tabwriter"

	"github.com/containers/psgo"
)

func main() {
	var (
		descriptors []string
		pidsList    []string
		data        [][]string
		err         error

		pids         = flag.String("pids", "", "comma separated list of process IDs to retrieve")
		format       = flag.String("format", "", "ps(1) AIX format comma-separated string")
		list         = flag.Bool("list", false, "list all supported descriptors")
		join         = flag.Bool("join", false, "join namespace of provided pids (containers)")
		fillMappings = flag.Bool("fill-mappings", false, "fill the UID and GID mappings with the current user namespace")
	)

	log.SetPrefix("psgo: ")
	log.SetFlags(0)

	flag.Parse()

	if *fillMappings && !*join {
		log.Fatal("-fill-mappings requires -join")
	}

	if *list {
		fmt.Println(strings.Join(psgo.ListDescriptors(), ", "))
		return
	}

	if *format != "" {
		descriptors = strings.Split(*format, ",")
	}

	if *pids != "" {
		pidsList = strings.Split(*pids, ",")
	}

	if len(pidsList) > 0 {
		opts := psgo.JoinNamespaceOpts{FillMappings: *fillMappings}

		if *join {
			data, err = psgo.JoinNamespaceAndProcessInfoByPidsWithOptions(pidsList, descriptors, &opts)
		} else {
			data, err = psgo.ProcessInfoByPids(pidsList, descriptors)
		}
		if err != nil {
			log.Panic(err)
		}
	} else {
		data, err = psgo.ProcessInfo(descriptors)
		if err != nil {
			log.Panic(err)
		}
	}

	tw := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
	for _, d := range data {
		fmt.Fprintln(tw, strings.Join(d, "\t"))
	}
	tw.Flush()
}