github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/cli/cmd/list_contexts.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/telepresenceio/telepresence/v2/pkg/client/cli/daemon"
     9  	"github.com/telepresenceio/telepresence/v2/pkg/client/cli/output"
    10  )
    11  
    12  type listContextsCommand struct {
    13  	rq *daemon.CobraRequest
    14  }
    15  
    16  func listContexts() *cobra.Command {
    17  	lcc := &listContextsCommand{}
    18  
    19  	cmd := &cobra.Command{
    20  		Use:   "list-contexts",
    21  		Args:  cobra.NoArgs,
    22  		Short: "Show all contexts",
    23  		RunE:  lcc.run,
    24  	}
    25  	lcc.rq = daemon.InitRequest(cmd)
    26  	return cmd
    27  }
    28  
    29  func (lcc *listContextsCommand) run(cmd *cobra.Command, _ []string) error {
    30  	config, err := lcc.rq.GetConfig(cmd)
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	ctx := cmd.Context()
    36  	kcmap := config.Contexts
    37  
    38  	if output.WantsFormatted(cmd) {
    39  		output.Object(ctx, kcmap, false)
    40  	} else {
    41  		for name, kc := range kcmap {
    42  			fmt.Fprintf(output.Out(ctx), "- name: %s\n  default namespace: %s\n", name, kc.Namespace)
    43  		}
    44  	}
    45  	return nil
    46  }