github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/pods/top.go (about) 1 package pods 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "strings" 8 "text/tabwriter" 9 10 "github.com/containers/libpod/cmd/podmanV2/registry" 11 "github.com/containers/libpod/pkg/domain/entities" 12 "github.com/containers/psgo" 13 "github.com/pkg/errors" 14 "github.com/spf13/cobra" 15 ) 16 17 var ( 18 topDescription = fmt.Sprintf(`Specify format descriptors to alter the output. 19 20 You may run "podman pod top -l pid pcpu seccomp" to print the process ID, the CPU percentage and the seccomp mode of each process of the latest pod. 21 Format Descriptors: 22 %s`, strings.Join(psgo.ListDescriptors(), ",")) 23 24 topOptions = entities.PodTopOptions{} 25 26 topCommand = &cobra.Command{ 27 Use: "top [flags] POD [FORMAT-DESCRIPTORS|ARGS]", 28 Short: "Display the running processes in a pod", 29 Long: topDescription, 30 PersistentPreRunE: preRunE, 31 RunE: top, 32 Args: cobra.ArbitraryArgs, 33 Example: `podman pod top podID 34 podman pod top --latest 35 podman pod top podID pid seccomp args %C 36 podman pod top podID -eo user,pid,comm`, 37 } 38 ) 39 40 func init() { 41 registry.Commands = append(registry.Commands, registry.CliCommand{ 42 Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, 43 Command: topCommand, 44 Parent: podCmd, 45 }) 46 47 topCommand.SetHelpTemplate(registry.HelpTemplate()) 48 topCommand.SetUsageTemplate(registry.UsageTemplate()) 49 50 flags := topCommand.Flags() 51 flags.SetInterspersed(false) 52 flags.BoolVar(&topOptions.ListDescriptors, "list-descriptors", false, "") 53 flags.BoolVarP(&topOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of") 54 55 _ = flags.MarkHidden("list-descriptors") // meant only for bash completion 56 if registry.IsRemote() { 57 _ = flags.MarkHidden("latest") 58 } 59 } 60 61 func top(cmd *cobra.Command, args []string) error { 62 if topOptions.ListDescriptors { 63 fmt.Println(strings.Join(psgo.ListDescriptors(), "\n")) 64 return nil 65 } 66 67 if len(args) < 1 && !topOptions.Latest { 68 return errors.Errorf("you must provide the name or id of a running pod") 69 } 70 71 if topOptions.Latest { 72 topOptions.Descriptors = args 73 } else { 74 topOptions.NameOrID = args[0] 75 topOptions.Descriptors = args[1:] 76 } 77 78 topResponse, err := registry.ContainerEngine().PodTop(context.Background(), topOptions) 79 if err != nil { 80 return err 81 } 82 83 w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0) 84 for _, proc := range topResponse.Value { 85 if _, err := fmt.Fprintln(w, proc); err != nil { 86 return err 87 } 88 } 89 return w.Flush() 90 }