init.go 3.03 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 21 22 23 24 25 26 27 28 29
// 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 (
	"github.com/ksonnet/ksonnet/metadata/app"
	"github.com/ksonnet/ksonnet/pkg/appinit"
	"github.com/ksonnet/ksonnet/pkg/registry"
	"github.com/spf13/afero"
)

const (
	defaultIncubatorRegName = "incubator"
	defaultIncubatorURI     = "github.com/ksonnet/parts/tree/master/" + defaultIncubatorRegName
)

30
// RunInit initializes an app.
bryanl's avatar
bryanl committed
31 32
func RunInit(m map[string]interface{}) error {
	i, err := NewInit(m)
bryanl's avatar
bryanl committed
33 34 35 36 37 38 39
	if err != nil {
		return err
	}

	return i.Run()
}

40 41
type appLoadFn func(fs afero.Fs, root string, skipFindRoot bool) (app.App, error)

bryanl's avatar
bryanl committed
42
type appInitFn func(fs afero.Fs, name, rootPath, k8sSpecFlag, serverURI, namespace string, registries []registry.Registry) error
43 44

type initIncubatorFn func(app.App) (registry.Registry, error)
bryanl's avatar
bryanl committed
45 46 47

// Init creates a component namespace
type Init struct {
48 49 50 51 52 53 54
	fs                    afero.Fs
	name                  string
	rootPath              string
	k8sSpecFlag           string
	serverURI             string
	namespace             string
	skipDefaultRegistries bool
bryanl's avatar
bryanl committed
55

56
	appInitFn       appInitFn
57
	appLoadFn       appLoadFn
58
	initIncubatorFn initIncubatorFn
bryanl's avatar
bryanl committed
59 60 61
}

// NewInit creates an instance of Init.
bryanl's avatar
bryanl committed
62 63 64
func NewInit(m map[string]interface{}) (*Init, error) {
	ol := newOptionLoader(m)

bryanl's avatar
bryanl committed
65
	i := &Init{
66 67 68 69 70
		fs:                    ol.loadFs(OptionFs),
		name:                  ol.loadString(OptionName),
		rootPath:              ol.loadString(OptionRootPath),
		k8sSpecFlag:           ol.loadString(OptionSpecFlag),
		serverURI:             ol.loadOptionalString(OptionServer),
71
		namespace:             ol.loadString(OptionNamespace),
72
		skipDefaultRegistries: ol.loadBool(OptionSkipDefaultRegistries),
bryanl's avatar
bryanl committed
73

74
		appInitFn:       appinit.Init,
75
		appLoadFn:       app.Load,
76
		initIncubatorFn: initIncubator,
bryanl's avatar
bryanl committed
77 78
	}

bryanl's avatar
bryanl committed
79 80 81 82
	if ol.err != nil {
		return nil, ol.err
	}

bryanl's avatar
bryanl committed
83 84 85 86 87
	return i, nil
}

// Run runs that ns create action.
func (i *Init) Run() error {
88
	var registries []registry.Registry
bryanl's avatar
bryanl committed
89

90
	if !i.skipDefaultRegistries {
91 92 93 94 95 96
		a, err := i.appLoadFn(i.fs, i.rootPath, true)
		if err != nil {
			return err
		}

		gh, err := i.initIncubatorFn(a)
97 98 99 100 101 102
		if err != nil {
			return err
		}

		registries = append(registries, gh)
	}
bryanl's avatar
bryanl committed
103 104 105 106 107 108 109 110 111 112

	return i.appInitFn(
		i.fs,
		i.name,
		i.rootPath,
		i.k8sSpecFlag,
		i.serverURI,
		i.namespace,
		registries,
	)
113
}
bryanl's avatar
bryanl committed
114

115 116 117 118 119 120 121 122
func initIncubator(a app.App) (registry.Registry, error) {
	return registry.NewGitHub(
		a,
		&app.RegistryRefSpec{
			Name:     "incubator",
			Protocol: registry.ProtocolGitHub,
			URI:      defaultIncubatorURI,
		})
bryanl's avatar
bryanl committed
123
}