env_list.go 2.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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 (
bryanl's avatar
bryanl committed
19
	"encoding/json"
20 21 22 23
	"io"
	"os"
	"sort"

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

// RunEnvList runs `env list`
bryanl's avatar
bryanl committed
30 31
func RunEnvList(m map[string]interface{}) error {
	nl, err := NewEnvList(m)
32 33 34 35 36 37 38 39 40
	if err != nil {
		return err
	}

	return nl.Run()
}

// EnvList lists available namespaces
type EnvList struct {
bryanl's avatar
bryanl committed
41 42 43
	app        app.App
	outputType string
	out        io.Writer
44 45 46
}

// NewEnvList creates an instance of EnvList
bryanl's avatar
bryanl committed
47 48 49
func NewEnvList(m map[string]interface{}) (*EnvList, error) {
	ol := newOptionLoader(m)

bryanl's avatar
bryanl committed
50
	el := &EnvList{
bryanl's avatar
bryanl committed
51 52
		app:        ol.LoadApp(),
		outputType: ol.LoadOptionalString(OptionOutput),
bryanl's avatar
bryanl committed
53

54 55 56
		out: os.Stdout,
	}

bryanl's avatar
bryanl committed
57 58 59 60
	if ol.err != nil {
		return nil, ol.err
	}

bryanl's avatar
bryanl committed
61 62 63 64 65
	if el.outputType == "" {
		el.outputType = OutputWide
	}

	return el, nil
66 67 68
}

// Run runs the env list action.
bryanl's avatar
bryanl committed
69 70 71 72 73 74
func (el *EnvList) Run() error {
	switch el.outputType {
	default:
		return errors.Errorf("unknown output format %q", el.outputType)
	case OutputWide:
		return el.outputWide()
bryanl's avatar
bryanl committed
75
	case OutputJSON:
bryanl's avatar
bryanl committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
		return el.outputJSON()
	}
}

func (el *EnvList) outputJSON() error {
	environments, err := el.app.Environments()
	if err != nil {
		return err
	}

	return json.NewEncoder(el.out).Encode(environments)
}

func (el *EnvList) outputWide() error {
	environments, err := el.app.Environments()
91 92 93 94
	if err != nil {
		return err
	}

bryanl's avatar
bryanl committed
95
	table := table.New(el.out)
bryanl's avatar
bryanl committed
96
	table.SetHeader([]string{"name", "override", "kubernetes-version", "namespace", "server"})
97 98 99 100

	var rows [][]string

	for name, env := range environments {
bryanl's avatar
bryanl committed
101 102 103 104 105
		override := ""
		if env.IsOverride() {
			override = "*"
		}

106 107
		rows = append(rows, []string{
			name,
bryanl's avatar
bryanl committed
108
			override,
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
			env.KubernetesVersion,
			env.Destination.Namespace,
			env.Destination.Server,
		})
	}

	sort.Slice(rows, func(i, j int) bool {
		return rows[i][0] < rows[j][0]
	})

	table.AppendBulk(rows)

	table.Render()
	return nil
}