github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/app_list.go (about)

     1  // Copyright 2016 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  
    21  	rkt "github.com/rkt/rkt/lib"
    22  	"github.com/spf13/cobra"
    23  )
    24  
    25  var (
    26  	cmdAppList = &cobra.Command{
    27  		Use:   "list UUID",
    28  		Short: "List apps for the given pod",
    29  		Long:  "This only lists the name and state of the apps, app status will show more detailed info.",
    30  		Run:   runWrapper(runAppList),
    31  	}
    32  )
    33  
    34  func init() {
    35  	cmdApp.AddCommand(cmdAppList)
    36  	cmdAppList.Flags().BoolVar(&flagNoLegend, "no-legend", false, "suppress a legend with the list")
    37  }
    38  
    39  func runAppList(cmd *cobra.Command, args []string) int {
    40  	if len(args) != 1 {
    41  		cmd.Usage()
    42  		return 1
    43  	}
    44  
    45  	apps, err := rkt.AppsForPod(args[0], getDataDir(), "")
    46  	if err != nil {
    47  		stderr.PrintE("error listing apps", err)
    48  		return 1
    49  	}
    50  
    51  	tabBuffer := new(bytes.Buffer)
    52  	tabOut := getTabOutWithWriter(tabBuffer)
    53  
    54  	if !flagNoLegend {
    55  		fmt.Fprintf(tabOut, "NAME\tSTATE\n")
    56  	}
    57  
    58  	for _, app := range apps {
    59  		fmt.Fprintf(tabOut, "%s\t%s\n", app.Name, app.State)
    60  	}
    61  
    62  	tabOut.Flush()
    63  	stdout.Print(tabBuffer)
    64  	return 0
    65  }