github.com/vmware/govmomi@v0.37.2/govc/namespace/service/info.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 info struct {
    34  	*flags.ClientFlag
    35  	*flags.OutputFlag
    36  }
    37  
    38  func init() {
    39  	cli.Register("namespace.service.info", &info{})
    40  }
    41  
    42  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    43  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    44  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    45  	cmd.ClientFlag.Register(ctx, f)
    46  	cmd.OutputFlag.Register(ctx, f)
    47  
    48  }
    49  
    50  func (cmd *info) Process(ctx context.Context) error {
    51  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    52  		return err
    53  	}
    54  	return cmd.OutputFlag.Process(ctx)
    55  }
    56  
    57  func (cmd *info) Description() string {
    58  	return `Gets information of a specific supervisor service.
    59  
    60  Examples:
    61    govc namespace.service.info my-supervisor-service
    62    govc namespace.service.info -json my-supervisor-service | jq .`
    63  }
    64  
    65  type infoWriter struct {
    66  	cmd     *info
    67  	Service namespace.SupervisorServiceInfo `json:"service"`
    68  }
    69  
    70  func (r *infoWriter) Write(w io.Writer) error {
    71  	tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
    72  
    73  	fmt.Fprintf(tw, "%s", r.Service.Name)
    74  	fmt.Fprintf(tw, "\t%s", r.Service.State)
    75  	fmt.Fprintf(tw, "\t%s", r.Service.Description)
    76  
    77  	fmt.Fprintf(tw, "\n")
    78  
    79  	return tw.Flush()
    80  }
    81  
    82  func (r *infoWriter) MarshalJSON() ([]byte, error) {
    83  	return json.Marshal(r.Service)
    84  }
    85  
    86  func (r *infoWriter) Dump() interface{} {
    87  	return r.Service
    88  }
    89  
    90  func (cmd *info) Usage() string {
    91  	return "NAME"
    92  }
    93  
    94  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    95  	service := f.Args()
    96  	if len(service) != 1 {
    97  		return flag.ErrHelp
    98  	}
    99  
   100  	c, err := cmd.RestClient()
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	m := namespace.NewManager(c)
   106  	supervisorservice, err := m.GetSupervisorService(ctx, service[0])
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	return cmd.WriteResult(&infoWriter{cmd, supervisorservice})
   112  }