pkg_install.go 2.17 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
// 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
	"github.com/ksonnet/ksonnet/pkg/app"
bryanl's avatar
bryanl committed
20 21 22 23 24
	"github.com/ksonnet/ksonnet/pkg/pkg"
	"github.com/ksonnet/ksonnet/pkg/registry"
)

// DepCacher is a function that caches a dependency.j
25
type DepCacher func(app.App, registry.InstalledChecker, pkg.Descriptor, string) error
bryanl's avatar
bryanl committed
26 27

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

	return pi.Run()
}

37
// PkgInstall installs packages.
bryanl's avatar
bryanl committed
38
type PkgInstall struct {
bryanl's avatar
bryanl committed
39 40 41
	app         app.App
	libName     string
	customName  string
42
	checker     registry.InstalledChecker
bryanl's avatar
bryanl committed
43
	depCacherFn DepCacher
bryanl's avatar
bryanl committed
44 45 46
}

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

50 51 52 53 54
	a := ol.LoadApp()
	if ol.err != nil {
		return nil, ol.err
	}

bryanl's avatar
bryanl committed
55
	nl := &PkgInstall{
56
		app:        a,
bryanl's avatar
bryanl committed
57 58
		libName:    ol.LoadString(OptionLibName),
		customName: ol.LoadString(OptionName),
59
		checker:    registry.NewPackageManager(a),
bryanl's avatar
bryanl committed
60 61

		depCacherFn: registry.CacheDependency,
bryanl's avatar
bryanl committed
62 63
	}

bryanl's avatar
bryanl committed
64 65
	if ol.err != nil {
		return nil, ol.err
bryanl's avatar
bryanl committed
66 67 68 69 70
	}

	return nl, nil
}

71
// Run installs packages.
bryanl's avatar
bryanl committed
72 73 74 75 76 77
func (pi *PkgInstall) Run() error {
	d, customName, err := pi.parseDepSpec()
	if err != nil {
		return err
	}

78
	return pi.depCacherFn(pi.app, pi.checker, d, customName)
bryanl's avatar
bryanl committed
79 80 81
}

func (pi *PkgInstall) parseDepSpec() (pkg.Descriptor, string, error) {
82
	d, err := pkg.Parse(pi.libName)
bryanl's avatar
bryanl committed
83 84 85 86 87 88
	if err != nil {
		return pkg.Descriptor{}, "", err
	}

	customName := pi.customName
	if customName == "" {
89
		customName = d.Name
bryanl's avatar
bryanl committed
90 91 92 93
	}

	return d, customName, nil
}