github.com/vmware/govmomi@v0.43.0/govc/host/service/ls.go (about)

     1  /*
     2  Copyright (c) 2016 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  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"text/tabwriter"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  	"github.com/vmware/govmomi/vim25/types"
    29  )
    30  
    31  type ls struct {
    32  	*flags.ClientFlag
    33  	*flags.OutputFlag
    34  	*flags.HostSystemFlag
    35  }
    36  
    37  func init() {
    38  	cli.Register("host.service.ls", &ls{})
    39  }
    40  
    41  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    42  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    43  	cmd.ClientFlag.Register(ctx, f)
    44  
    45  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    46  	cmd.OutputFlag.Register(ctx, f)
    47  
    48  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    49  	cmd.HostSystemFlag.Register(ctx, f)
    50  }
    51  
    52  func (cmd *ls) Process(ctx context.Context) error {
    53  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    54  		return err
    55  	}
    56  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    57  		return err
    58  	}
    59  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    60  		return err
    61  	}
    62  	return nil
    63  }
    64  
    65  func (cmd *ls) Description() string {
    66  	return `List HOST services.`
    67  }
    68  
    69  func Status(s types.HostService) string {
    70  	if s.Running {
    71  		return "Running"
    72  	}
    73  	return "Stopped"
    74  }
    75  
    76  func Policy(s types.HostService) string {
    77  	switch types.HostServicePolicy(s.Policy) {
    78  	case types.HostServicePolicyOff:
    79  		return "Disabled"
    80  	case types.HostServicePolicyOn:
    81  		return "Enabled"
    82  	case types.HostServicePolicyAutomatic:
    83  		return "Automatic"
    84  	default:
    85  		return s.Policy
    86  	}
    87  }
    88  
    89  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    90  	host, err := cmd.HostSystem()
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	s, err := host.ConfigManager().ServiceSystem(ctx)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	services, err := s.Service(ctx)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	return cmd.WriteResult(optionResult(services))
   106  }
   107  
   108  type optionResult []types.HostService
   109  
   110  func (services optionResult) Write(w io.Writer) error {
   111  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
   112  
   113  	fmt.Fprintf(tw, "%s\t%s\t%v\t%s\n", "Key", "Policy", "Status", "Label")
   114  
   115  	for _, s := range services {
   116  		fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", s.Key, s.Policy, Status(s), s.Label)
   117  	}
   118  
   119  	return tw.Flush()
   120  }