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

     1  /*
     2  Copyright (c) 2024-2024 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 namespace
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"strings"
    25  	"text/tabwriter"
    26  
    27  	"github.com/vmware/govmomi/vapi/namespace"
    28  
    29  	"github.com/vmware/govmomi/govc/cli"
    30  	"github.com/vmware/govmomi/govc/flags"
    31  )
    32  
    33  type infoResult namespace.NamespacesInstanceInfo
    34  
    35  func (r infoResult) Write(w io.Writer) error {
    36  	tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
    37  	fmt.Fprintf(tw, "Cluster:\t%s\n", r.ClusterId)
    38  	fmt.Fprintf(tw, "Status:\t%s\n", r.ConfigStatus)
    39  	fmt.Fprintf(tw, "VM Classes:\t%s\n", strings.Join(r.VmServiceSpec.VmClasses, ","))
    40  	fmt.Fprintf(tw, "VM Libraries:\t%s\n", strings.Join(r.VmServiceSpec.ContentLibraries, ","))
    41  	return tw.Flush()
    42  }
    43  
    44  type info struct {
    45  	*flags.ClientFlag
    46  	*flags.OutputFlag
    47  }
    48  
    49  func init() {
    50  	cli.Register("namespace.info", &info{})
    51  }
    52  
    53  func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
    54  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    55  	cmd.ClientFlag.Register(ctx, f)
    56  
    57  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    58  	cmd.OutputFlag.Register(ctx, f)
    59  }
    60  
    61  func (cmd *info) Process(ctx context.Context) error {
    62  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    63  		return err
    64  	}
    65  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    66  		return err
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  func (cmd *info) Usage() string {
    73  	return "NAME"
    74  }
    75  
    76  func (cmd *info) Description() string {
    77  	return `Displays the details of a vSphere Namespace.
    78  
    79  Examples:
    80    govc namespace.info test-namespace`
    81  }
    82  
    83  func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
    84  	if f.NArg() != 1 {
    85  		return flag.ErrHelp
    86  	}
    87  
    88  	rc, err := cmd.RestClient()
    89  	if err != nil {
    90  		return err
    91  	}
    92  
    93  	nm := namespace.NewManager(rc)
    94  
    95  	d, err := nm.GetNamespace(ctx, f.Arg(0))
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	return cmd.WriteResult(infoResult(d))
   101  }