pkg_install.go 2.47 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
	"github.com/ksonnet/ksonnet/pkg/pkg"
	"github.com/ksonnet/ksonnet/pkg/registry"
)

24 25 26
type libCacher func(app.App, registry.InstalledChecker, pkg.Descriptor, string) (*app.LibraryConfig, error)

type libUpdater func(name string, env string, spec *app.LibraryConfig) error
bryanl's avatar
bryanl committed
27 28

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

	return pi.Run()
}

38
// PkgInstall installs packages.
bryanl's avatar
bryanl committed
39
type PkgInstall struct {
bryanl's avatar
bryanl committed
40 41 42
	app         app.App
	libName     string
	customName  string
43
	envName     string
44
	checker     registry.InstalledChecker
45 46
	libCacherFn libCacher
	libUpdateFn libUpdater
bryanl's avatar
bryanl committed
47 48 49
}

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

53 54 55 56 57
	a := ol.LoadApp()
	if ol.err != nil {
		return nil, ol.err
	}

bryanl's avatar
bryanl committed
58
	nl := &PkgInstall{
59
		app:        a,
bryanl's avatar
bryanl committed
60 61
		libName:    ol.LoadString(OptionLibName),
		customName: ol.LoadString(OptionName),
62
		envName:    ol.LoadOptionalString(OptionEnvName),
63
		checker:    registry.NewPackageManager(a),
bryanl's avatar
bryanl committed
64

65 66
		libCacherFn: registry.CacheDependency,
		libUpdateFn: a.UpdateLib,
bryanl's avatar
bryanl committed
67 68
	}

bryanl's avatar
bryanl committed
69 70
	if ol.err != nil {
		return nil, ol.err
bryanl's avatar
bryanl committed
71 72 73 74 75
	}

	return nl, nil
}

76
// Run installs packages.
bryanl's avatar
bryanl committed
77 78 79 80 81 82
func (pi *PkgInstall) Run() error {
	d, customName, err := pi.parseDepSpec()
	if err != nil {
		return err
	}

83 84 85 86 87 88 89 90 91 92 93
	libCfg, err := pi.libCacherFn(pi.app, pi.checker, d, customName)
	if err != nil {
		return err
	}

	err = pi.libUpdateFn(d.Name, pi.envName, libCfg)
	if err != nil {
		return err
	}

	return nil
bryanl's avatar
bryanl committed
94 95 96
}

func (pi *PkgInstall) parseDepSpec() (pkg.Descriptor, string, error) {
97
	d, err := pkg.Parse(pi.libName)
bryanl's avatar
bryanl committed
98 99 100 101 102 103
	if err != nil {
		return pkg.Descriptor{}, "", err
	}

	customName := pi.customName
	if customName == "" {
104
		customName = d.Name
bryanl's avatar
bryanl committed
105 106 107 108
	}

	return d, customName, nil
}