pkg_install.go 2.83 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
	a := ol.LoadApp()
	if ol.err != nil {
		return nil, ol.err
	}
58 59
	httpClient := ol.LoadHTTPClient()
	httpClientOpt := registry.HTTPClientOpt(httpClient)
60

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

69 70 71
		libCacherFn: func(a app.App, checker registry.InstalledChecker, d pkg.Descriptor, customName string, force bool) (*app.LibraryConfig, error) {
			return registry.CacheDependency(a, checker, d, customName, force, httpClient)
		},
72
		libUpdateFn: a.UpdateLib,
bryanl's avatar
bryanl committed
73 74
	}

bryanl's avatar
bryanl committed
75 76
	if ol.err != nil {
		return nil, ol.err
bryanl's avatar
bryanl committed
77 78 79 80 81
	}

	return nl, nil
}

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

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

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

	customName := pi.customName
	if customName == "" {
110
		customName = d.Name
bryanl's avatar
bryanl committed
111 112 113 114
	}

	return d, customName, nil
}