pkg_install.go 2.54 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
type libCacher func(app.App, registry.InstalledChecker, pkg.Descriptor, string, bool) (*app.LibraryConfig, error)
25 26

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
	force       bool
45
	checker     registry.InstalledChecker
46 47
	libCacherFn libCacher
	libUpdateFn libUpdater
bryanl's avatar
bryanl committed
48 49 50
}

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

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

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

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

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

	return nl, nil
}

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

85
	libCfg, err := pi.libCacherFn(pi.app, pi.checker, d, customName, pi.force)
86 87 88 89 90 91 92 93 94 95
	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
96 97 98
}

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

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

	return d, customName, nil
}