1/*
2Copyright (c) 2015 VMware, Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package fields
18
19import (
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
31type ls struct {
32	*flags.ClientFlag
33}
34
35func init() {
36	cli.Register("fields.ls", &ls{})
37}
38
39func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
40	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
41	cmd.ClientFlag.Register(ctx, f)
42}
43
44func (cmd *ls) Process(ctx context.Context) error {
45	if err := cmd.ClientFlag.Process(ctx); err != nil {
46		return err
47	}
48	return nil
49}
50
51func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
52	c, err := cmd.Client()
53	if err != nil {
54		return err
55	}
56
57	m, err := object.GetCustomFieldsManager(c)
58	if err != nil {
59		return err
60	}
61
62	field, err := m.Field(ctx)
63	if err != nil {
64		return err
65	}
66
67	tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
68
69	for _, def := range field {
70		fmt.Fprintf(tw, "%d\t%s\n", def.Key, def.Name)
71	}
72
73	return tw.Flush()
74}
75