component_list.go 2.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Copyright 2018 The ksonnet authors
//
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

package actions

import (
	"io"
	"os"
	"sort"

bryanl's avatar
bryanl committed
23
	"github.com/ksonnet/ksonnet/pkg/app"
bryanl's avatar
bryanl committed
24
	"github.com/ksonnet/ksonnet/pkg/component"
25 26 27 28 29
	"github.com/ksonnet/ksonnet/pkg/util/table"
	"github.com/pkg/errors"
)

// RunComponentList runs `component list`
bryanl's avatar
bryanl committed
30 31
func RunComponentList(m map[string]interface{}) error {
	cl, err := NewComponentList(m)
32 33 34 35 36 37 38
	if err != nil {
		return err
	}

	return cl.Run()
}

39
// ComponentList create a list of components in a module.
40 41
type ComponentList struct {
	app    app.App
42
	module string
43 44 45 46 47 48
	output string
	cm     component.Manager
	out    io.Writer
}

// NewComponentList creates an instance of ComponentList.
bryanl's avatar
bryanl committed
49 50 51
func NewComponentList(m map[string]interface{}) (*ComponentList, error) {
	ol := newOptionLoader(m)

52
	cl := &ComponentList{
bryanl's avatar
bryanl committed
53 54 55
		app:    ol.LoadApp(),
		module: ol.LoadString(OptionModule),
		output: ol.LoadString(OptionOutput),
bryanl's avatar
bryanl committed
56 57 58 59 60 61 62

		cm:  component.DefaultManager,
		out: os.Stdout,
	}

	if ol.err != nil {
		return nil, ol.err
63 64 65 66 67 68 69
	}

	return cl, nil
}

// Run runs the ComponentList action.
func (cl *ComponentList) Run() error {
70
	ns, err := cl.cm.Module(cl.app, cl.module)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	if err != nil {
		return err
	}

	components, err := ns.Components()
	if err != nil {
		return err
	}

	switch cl.output {
	default:
		return errors.Errorf("invalid output option %q", cl.output)
	case "":
		cl.listComponents(components)
	case "wide":
		if err := cl.listComponentsWide(components); err != nil {
			return err
		}
	}

	return nil
}

func (cl *ComponentList) listComponents(components []component.Component) {
	var list []string
	for _, c := range components {
		list = append(list, c.Name(false))
	}

	sort.Strings(list)

	table := table.New(cl.out)
	table.SetHeader([]string{"component"})
	for _, item := range list {
		table.Append([]string{item})
	}
	table.Render()
}

func (cl *ComponentList) listComponentsWide(components []component.Component) error {
	var rows [][]string
	for _, c := range components {
113
		summary, err := c.Summarize()
114 115 116 117
		if err != nil {
			return err
		}

118 119 120 121 122 123
		row := []string{
			summary.ComponentName,
			summary.Type,
			summary.APIVersion,
			summary.Kind,
			summary.Name,
124
		}
125 126

		rows = append(rows, row)
127 128 129
	}

	table := table.New(cl.out)
130
	table.SetHeader([]string{"component", "type", "apiversion", "kind", "name"})
131 132 133 134 135
	table.AppendBulk(rows)
	table.Render()

	return nil
}