pkg_describe.go 2.38 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
// 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"
bryanl's avatar
bryanl committed
21
	"text/template"
bryanl's avatar
bryanl committed
22

bryanl's avatar
bryanl committed
23
	"github.com/ksonnet/ksonnet/pkg/app"
bryanl's avatar
bryanl committed
24 25 26 27
	"github.com/ksonnet/ksonnet/pkg/registry"
)

// RunPkgDescribe runs `pkg install`
bryanl's avatar
bryanl committed
28 29
func RunPkgDescribe(m map[string]interface{}) error {
	pd, err := NewPkgDescribe(m)
bryanl's avatar
bryanl committed
30 31 32 33 34 35 36
	if err != nil {
		return err
	}

	return pd.Run()
}

37
// PkgDescribe describes a package.
bryanl's avatar
bryanl committed
38
type PkgDescribe struct {
bryanl's avatar
bryanl committed
39 40 41 42
	app     app.App
	pkgName string

	templateSrc    string
bryanl's avatar
bryanl committed
43
	out            io.Writer
bryanl's avatar
bryanl committed
44
	packageManager registry.PackageManager
bryanl's avatar
bryanl committed
45 46 47
}

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

bryanl's avatar
bryanl committed
51
	app := ol.LoadApp()
bryanl's avatar
bryanl committed
52
	pd := &PkgDescribe{
bryanl's avatar
bryanl committed
53
		app:     app,
bryanl's avatar
bryanl committed
54
		pkgName: ol.LoadString(OptionPackageName),
bryanl's avatar
bryanl committed
55

bryanl's avatar
bryanl committed
56
		templateSrc:    pkgDescribeTemplate,
bryanl's avatar
bryanl committed
57
		out:            os.Stdout,
bryanl's avatar
bryanl committed
58
		packageManager: registry.NewPackageManager(app),
bryanl's avatar
bryanl committed
59 60
	}

bryanl's avatar
bryanl committed
61 62 63 64
	if ol.err != nil {
		return nil, ol.err
	}

bryanl's avatar
bryanl committed
65 66 67
	return pd, nil
}

68
// Run describes a package.
bryanl's avatar
bryanl committed
69
func (pd *PkgDescribe) Run() error {
bryanl's avatar
bryanl committed
70
	p, err := pd.packageManager.Find(pd.pkgName)
bryanl's avatar
bryanl committed
71 72 73 74
	if err != nil {
		return err
	}

bryanl's avatar
bryanl committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88
	data := map[string]interface{}{
		"Name":        pd.pkgName,
		"Description": p.Description(),
	}

	isInstalled, err := p.IsInstalled()
	if err != nil {
		return err
	}

	data["IsInstalled"] = isInstalled

	if isInstalled {
		prototypes, err := p.Prototypes()
bryanl's avatar
bryanl committed
89 90 91
		if err != nil {
			return err
		}
bryanl's avatar
bryanl committed
92 93

		data["Prototypes"] = prototypes
bryanl's avatar
bryanl committed
94 95
	}

bryanl's avatar
bryanl committed
96 97 98 99
	t, err := template.New("pkg-describe").Parse(pd.templateSrc)
	if err != nil {
		return err
	}
bryanl's avatar
bryanl committed
100

bryanl's avatar
bryanl committed
101 102
	if err = t.Execute(pd.out, data); err != nil {
		return err
bryanl's avatar
bryanl committed
103 104 105 106
	}

	return nil
}
bryanl's avatar
bryanl committed
107 108 109 110 111 112 113 114 115 116 117 118 119

const pkgDescribeTemplate = `LIBRARY NAME:
{{.Name}}

DESCRIPTION:
{{.Description}}

{{- if .IsInstalled}}

PROTOTYPES:{{- range .Prototypes}}
  {{.Name}} - {{.Template.ShortDescription}}
{{- end}}{{- end}}
`