module_create.go 1.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// 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/component"
	"github.com/ksonnet/ksonnet/metadata/app"
	"github.com/pkg/errors"
)

24 25 26
// RunModuleCreate creates a module.
func RunModuleCreate(m map[string]interface{}) error {
	mc, err := NewModuleCreate(m)
27 28 29 30
	if err != nil {
		return err
	}

31
	return mc.Run()
32 33
}

34 35
// ModuleCreate creates a component module
type ModuleCreate struct {
36
	app    app.App
37
	module string
38 39 40
	cm     component.Manager
}

41 42
// NewModuleCreate creates an instance of ModuleCreate.
func NewModuleCreate(m map[string]interface{}) (*ModuleCreate, error) {
bryanl's avatar
bryanl committed
43 44
	ol := newOptionLoader(m)

45
	mc := &ModuleCreate{
bryanl's avatar
bryanl committed
46
		app:    ol.loadApp(),
47
		module: ol.loadString(OptionModule),
bryanl's avatar
bryanl committed
48 49

		cm: component.DefaultManager,
50 51
	}

52
	return mc, nil
53 54 55
}

// Run runs that ns create action.
56 57
func (mc *ModuleCreate) Run() error {
	_, err := mc.cm.Module(mc.app, mc.module)
58
	if err == nil {
59
		return errors.Errorf("module %q already exists", mc.module)
60 61
	}

62
	return mc.cm.CreateModule(mc.app, mc.module)
63
}