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

bryanl's avatar
bryanl committed
24
	"github.com/ksonnet/ksonnet/pkg/app"
bryanl's avatar
bryanl committed
25
	"github.com/ksonnet/ksonnet/pkg/pkg"
bryanl's avatar
bryanl committed
26
	"github.com/ksonnet/ksonnet/pkg/prototype"
bryanl's avatar
bryanl committed
27
	"github.com/ksonnet/ksonnet/pkg/registry"
bryanl's avatar
bryanl committed
28 29 30 31
	"github.com/pkg/errors"
)

// RunPrototypeDescribe runs `prototype describe`
bryanl's avatar
bryanl committed
32 33
func RunPrototypeDescribe(m map[string]interface{}) error {
	pd, err := NewPrototypeDescribe(m)
bryanl's avatar
bryanl committed
34 35 36 37 38 39 40
	if err != nil {
		return err
	}

	return pd.Run()
}

41
// PrototypeDescribe describes a prototype.
bryanl's avatar
bryanl committed
42
type PrototypeDescribe struct {
bryanl's avatar
bryanl committed
43 44 45 46
	app            app.App
	out            io.Writer
	query          string
	packageManager registry.PackageManager
bryanl's avatar
bryanl committed
47 48 49
}

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

bryanl's avatar
bryanl committed
53
	app := ol.LoadApp()
54 55
	httpClientOpt := registry.HTTPClientOpt(ol.LoadHTTPClient())

bryanl's avatar
bryanl committed
56
	pd := &PrototypeDescribe{
bryanl's avatar
bryanl committed
57
		app:   app,
bryanl's avatar
bryanl committed
58
		query: ol.LoadString(OptionQuery),
bryanl's avatar
bryanl committed
59

bryanl's avatar
bryanl committed
60
		out:            os.Stdout,
61
		packageManager: registry.NewPackageManager(app, httpClientOpt),
bryanl's avatar
bryanl committed
62 63
	}

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

bryanl's avatar
bryanl committed
68 69 70 71 72
	return pd, nil
}

// Run runs the env list action.
func (pd *PrototypeDescribe) Run() error {
bryanl's avatar
bryanl committed
73
	prototypes, err := pd.packageManager.Prototypes()
bryanl's avatar
bryanl committed
74 75 76 77
	if err != nil {
		return err
	}

78 79 80 81
	index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
	if err != nil {
		return errors.Wrap(err, "create prototype index")
	}
bryanl's avatar
bryanl committed
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

	prototypes, err = index.List()
	if err != nil {
		return err
	}

	p, err := findUniquePrototype(pd.query, prototypes)
	if err != nil {
		return err
	}

	fmt.Fprintln(pd.out, `PROTOTYPE NAME:`)
	fmt.Fprintln(pd.out, p.Name)
	fmt.Fprintln(pd.out)
	fmt.Fprintln(pd.out, `DESCRIPTION:`)
	fmt.Fprintln(pd.out, p.Template.Description)
	fmt.Fprintln(pd.out)
	fmt.Fprintln(pd.out, `REQUIRED PARAMETERS:`)
	fmt.Fprintln(pd.out, p.RequiredParams().PrettyString("  "))
	fmt.Fprintln(pd.out)
	fmt.Fprintln(pd.out, `OPTIONAL PARAMETERS:`)
	fmt.Fprintln(pd.out, p.OptionalParams().PrettyString("  "))
	fmt.Fprintln(pd.out)
	fmt.Fprintln(pd.out, `TEMPLATE TYPES AVAILABLE:`)
	fmt.Fprintf(pd.out, "  %s\n", p.Template.AvailableTemplates())

	return nil
}

111
type prototypeFn func(app.App, pkg.Descriptor) (prototype.Prototypes, error)
bryanl's avatar
bryanl committed
112

113 114 115 116 117
func findUniquePrototype(query string, prototypes prototype.Prototypes) (*prototype.Prototype, error) {
	index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
	if err != nil {
		return nil, err
	}
bryanl's avatar
bryanl committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

	sameSuffix, err := index.SearchNames(query, prototype.Suffix)
	if err != nil {
		return nil, err
	}

	if len(sameSuffix) == 1 {
		// Success.
		return sameSuffix[0], nil
	} else if len(sameSuffix) > 1 {
		// Ambiguous match.
		names := specNames(sameSuffix)
		return nil, errors.Errorf("ambiguous match for '%s': %s", query, strings.Join(names, ", "))
	} else {
		// No matches.
		substring, err := index.SearchNames(query, prototype.Substring)
		if err != nil || len(substring) == 0 {
			return nil, errors.Errorf("no prototype names matched '%s'", query)
		}

		partialMatches := specNames(substring)
		partials := strings.Join(partialMatches, "\n")
		return nil, errors.Errorf("no prototype names matched '%s'; a list of partial matches: %s", query, partials)
	}
}

144
func specNames(prototypes []*prototype.Prototype) []string {
bryanl's avatar
bryanl committed
145 146 147 148 149 150 151
	partialMatches := []string{}
	for _, proto := range prototypes {
		partialMatches = append(partialMatches, proto.Name)
	}

	return partialMatches
}