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

     1  /*
     2  Copyright (c) 2015-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 logs
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"os"
    24  	"text/tabwriter"
    25  
    26  	"github.com/vmware/govmomi/govc/cli"
    27  	"github.com/vmware/govmomi/govc/flags"
    28  	"github.com/vmware/govmomi/object"
    29  )
    30  
    31  type ls struct {
    32  	*flags.HostSystemFlag
    33  }
    34  
    35  func init() {
    36  	cli.Register("logs.ls", &ls{})
    37  }
    38  
    39  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    40  	cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx)
    41  	cmd.HostSystemFlag.Register(ctx, f)
    42  }
    43  
    44  func (cmd *ls) Process(ctx context.Context) error {
    45  	if err := cmd.HostSystemFlag.Process(ctx); err != nil {
    46  		return err
    47  	}
    48  	return nil
    49  }
    50  
    51  func (cmd *ls) Description() string {
    52  	return `List diagnostic log keys.
    53  
    54  Examples:
    55    govc logs.ls
    56    govc logs.ls -host host-a`
    57  }
    58  
    59  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    60  	c, err := cmd.Client()
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	var host *object.HostSystem
    66  
    67  	if c.IsVC() {
    68  		host, err = cmd.HostSystemIfSpecified()
    69  		if err != nil {
    70  			return err
    71  		}
    72  	}
    73  
    74  	m := object.NewDiagnosticManager(c)
    75  
    76  	desc, err := m.QueryDescriptions(ctx, host)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
    82  
    83  	for _, d := range desc {
    84  		fmt.Fprintf(tw, "%s\t%s\n", d.Key, d.FileName)
    85  	}
    86  
    87  	return tw.Flush()
    88  }