github.com/lalkh/containerd@v1.4.3/cmd/ctr/commands/tasks/list.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package tasks
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"text/tabwriter"
    23  
    24  	tasks "github.com/containerd/containerd/api/services/tasks/v1"
    25  	"github.com/containerd/containerd/cmd/ctr/commands"
    26  	"github.com/urfave/cli"
    27  )
    28  
    29  var listCommand = cli.Command{
    30  	Name:      "list",
    31  	Usage:     "list tasks",
    32  	Aliases:   []string{"ls"},
    33  	ArgsUsage: "[flags]",
    34  	Flags: []cli.Flag{
    35  		cli.BoolFlag{
    36  			Name:  "quiet, q",
    37  			Usage: "print only the task id",
    38  		},
    39  	},
    40  	Action: func(context *cli.Context) error {
    41  		quiet := context.Bool("quiet")
    42  		client, ctx, cancel, err := commands.NewClient(context)
    43  		if err != nil {
    44  			return err
    45  		}
    46  		defer cancel()
    47  		s := client.TaskService()
    48  		response, err := s.List(ctx, &tasks.ListTasksRequest{})
    49  		if err != nil {
    50  			return err
    51  		}
    52  		if quiet {
    53  			for _, task := range response.Tasks {
    54  				fmt.Println(task.ID)
    55  			}
    56  			return nil
    57  		}
    58  		w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0)
    59  		fmt.Fprintln(w, "TASK\tPID\tSTATUS\t")
    60  		for _, task := range response.Tasks {
    61  			if _, err := fmt.Fprintf(w, "%s\t%d\t%s\n",
    62  				task.ID,
    63  				task.Pid,
    64  				task.Status.String(),
    65  			); err != nil {
    66  				return err
    67  			}
    68  		}
    69  		return w.Flush()
    70  	},
    71  }