github.com/vmware/govmomi@v0.37.2/govc/namespace/ls.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  
    25  	"github.com/vmware/govmomi/vapi/namespace"
    26  
    27  	"github.com/vmware/govmomi/govc/cli"
    28  	"github.com/vmware/govmomi/govc/flags"
    29  )
    30  
    31  type lsResult []namespace.NamespacesInstanceSummary
    32  
    33  func (r lsResult) Write(w io.Writer) error {
    34  	for _, e := range r {
    35  		fmt.Fprintln(w, e.Namespace)
    36  	}
    37  	return nil
    38  }
    39  
    40  type ls struct {
    41  	*flags.ClientFlag
    42  	*flags.OutputFlag
    43  }
    44  
    45  func init() {
    46  	cli.Register("namespace.ls", &ls{})
    47  }
    48  
    49  func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
    50  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    51  	cmd.ClientFlag.Register(ctx, f)
    52  
    53  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    54  	cmd.OutputFlag.Register(ctx, f)
    55  }
    56  
    57  func (cmd *ls) Process(ctx context.Context) error {
    58  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    59  		return err
    60  	}
    61  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    62  		return err
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  func (cmd *ls) Description() string {
    69  	return `Displays the list of vSphere Namespaces.
    70  
    71  Examples:
    72    govc namespace.ls`
    73  }
    74  
    75  func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
    76  	rc, err := cmd.RestClient()
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	nm := namespace.NewManager(rc)
    82  
    83  	d, err := nm.ListNamespaces(ctx)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	return cmd.WriteResult(lsResult(d))
    89  }