github.com/vmware/govmomi@v0.37.2/govc/namespace/service/ls.go (about)

     1  /*
     2  Copyright (c) 2021-2023 VMware, Inc. All Rights Reserved.
     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 service
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"flag"
    23  	"fmt"
    24  	"io"
    25  	"os"
    26  	"text/tabwriter"
    27  
    28  	"github.com/vmware/govmomi/govc/cli"
    29  	"github.com/vmware/govmomi/govc/flags"
    30  	"github.com/vmware/govmomi/vapi/namespace"
    31  )
    32  
    33  type ls struct {
    34  	*flags.ClientFlag
    35  	*flags.OutputFlag
    36  
    37  	long bool
    38  }
    39  
    40  func init() {
    41  	cli.Register("namespace.service.ls", &ls{})
    42  }
    43  
    44  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    45  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    46  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    47  	cmd.ClientFlag.Register(ctx, f)
    48  	cmd.OutputFlag.Register(ctx, f)
    49  
    50  	f.BoolVar(&cmd.long, "l", false, "Long listing format")
    51  }
    52  
    53  func (cmd *ls) Process(ctx context.Context) error {
    54  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    55  		return err
    56  	}
    57  	return cmd.OutputFlag.Process(ctx)
    58  }
    59  
    60  func (cmd *ls) Description() string {
    61  	return `List namepace registered supervisor services.
    62  
    63  Examples:
    64    govc namespace.service.ls
    65    govc namespace.service.ls -l
    66    govc namespace.service.ls -json | jq .`
    67  }
    68  
    69  type lsWriter struct {
    70  	cmd     *ls
    71  	Service []namespace.SupervisorServiceSummary `json:"service"`
    72  }
    73  
    74  func (r *lsWriter) Write(w io.Writer) error {
    75  	tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
    76  
    77  	for _, svc := range r.Service {
    78  		fmt.Fprintf(tw, "%s", svc.ID)
    79  		if r.cmd.long {
    80  			fmt.Fprintf(tw, "\t%s", svc.State)
    81  			fmt.Fprintf(tw, "\t%s", svc.Name)
    82  		}
    83  		fmt.Fprintf(tw, "\n")
    84  	}
    85  	return tw.Flush()
    86  }
    87  
    88  func (r *lsWriter) MarshalJSON() ([]byte, error) {
    89  	return json.Marshal(r.Service)
    90  }
    91  
    92  func (r *lsWriter) Dump() interface{} {
    93  	return r.Service
    94  }
    95  
    96  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    97  	c, err := cmd.RestClient()
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	m := namespace.NewManager(c)
   103  	supervisorservices, err := m.ListSupervisorServices(ctx)
   104  	if err != nil {
   105  		return err
   106  	}
   107  
   108  	return cmd.WriteResult(&lsWriter{cmd, supervisorservices})
   109  }