actions.go 3.33 KB
Newer Older
bryanl's avatar
bryanl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

bryanl's avatar
bryanl committed
16
package clicmd
bryanl's avatar
bryanl committed
17

bryanl's avatar
bryanl committed
18
import (
bryanl's avatar
bryanl committed
19
	"github.com/ksonnet/ksonnet/pkg/actions"
bryanl's avatar
bryanl committed
20 21
	"github.com/pkg/errors"
)
bryanl's avatar
bryanl committed
22 23 24 25

type initName int

const (
bryanl's avatar
bryanl committed
26
	actionApply initName = iota
bryanl's avatar
bryanl committed
27
	actionComponentList
bryanl's avatar
bryanl committed
28
	actionComponentRm
bryanl's avatar
bryanl committed
29 30 31
	actionDelete
	actionDiff
	actionEnvAdd
bryanl's avatar
bryanl committed
32
	actionEnvCurrent
bryanl's avatar
bryanl committed
33 34 35 36 37
	actionEnvDescribe
	actionEnvList
	actionEnvRm
	actionEnvSet
	actionEnvTargets
bryanl's avatar
bryanl committed
38
	actionEnvUpdate
bryanl's avatar
bryanl committed
39
	actionImport
bryanl's avatar
bryanl committed
40
	actionInit
41 42
	actionModuleCreate
	actionModuleList
bryanl's avatar
bryanl committed
43
	actionParamDelete
bryanl's avatar
bryanl committed
44 45 46
	actionParamDiff
	actionParamList
	actionParamSet
bryanl's avatar
bryanl committed
47
	actionParamUnset
bryanl's avatar
bryanl committed
48 49 50 51 52 53 54 55 56 57 58 59 60
	actionPkgDescribe
	actionPkgInstall
	actionPkgList
	actionPrototypeDescribe
	actionPrototypeList
	actionPrototypePreview
	actionPrototypeSearch
	actionPrototypeUse
	actionRegistryAdd
	actionRegistryDescribe
	actionRegistryList
	actionShow
	actionUpgrade
bryanl's avatar
bryanl committed
61
	actionValidate
bryanl's avatar
bryanl committed
62 63
)

bryanl's avatar
bryanl committed
64 65 66 67
type actionFn func(map[string]interface{}) error

var (
	actionFns = map[initName]actionFn{
bryanl's avatar
bryanl committed
68 69
		actionApply:         actions.RunApply,
		actionComponentList: actions.RunComponentList,
bryanl's avatar
bryanl committed
70
		actionComponentRm:   actions.RunComponentRm,
bryanl's avatar
bryanl committed
71
		actionDelete:        actions.RunDelete,
bryanl's avatar
bryanl committed
72
		// actionDiff
73 74 75 76 77 78 79
		actionEnvAdd:            actions.RunEnvAdd,
		actionEnvCurrent:        actions.RunEnvCurrent,
		actionEnvDescribe:       actions.RunEnvDescribe,
		actionEnvList:           actions.RunEnvList,
		actionEnvRm:             actions.RunEnvRm,
		actionEnvSet:            actions.RunEnvSet,
		actionEnvTargets:        actions.RunEnvTargets,
bryanl's avatar
bryanl committed
80
		actionEnvUpdate:         actions.RunEnvUpdate,
81 82 83 84 85
		actionImport:            actions.RunImport,
		actionInit:              actions.RunInit,
		actionModuleCreate:      actions.RunModuleCreate,
		actionModuleList:        actions.RunModuleList,
		actionParamDiff:         actions.RunParamDiff,
bryanl's avatar
bryanl committed
86
		actionParamDelete:       actions.RunParamDelete,
bryanl's avatar
bryanl committed
87
		actionParamUnset:        actions.RunParamDelete,
bryanl's avatar
bryanl committed
88 89 90 91 92 93 94 95 96 97 98 99 100
		actionParamList:         actions.RunParamList,
		actionParamSet:          actions.RunParamSet,
		actionPkgDescribe:       actions.RunPkgDescribe,
		actionPkgInstall:        actions.RunPkgInstall,
		actionPkgList:           actions.RunPkgList,
		actionPrototypeDescribe: actions.RunPrototypeDescribe,
		actionPrototypeList:     actions.RunPrototypeList,
		actionPrototypePreview:  actions.RunPrototypePreview,
		actionPrototypeSearch:   actions.RunPrototypeSearch,
		actionPrototypeUse:      actions.RunPrototypeUse,
		actionRegistryAdd:       actions.RunRegistryAdd,
		actionRegistryDescribe:  actions.RunRegistryDescribe,
		actionRegistryList:      actions.RunRegistryList,
bryanl's avatar
bryanl committed
101 102 103
		actionShow:              actions.RunShow,
		actionUpgrade:           actions.RunUpgrade,
		actionValidate:          actions.RunValidate,
bryanl's avatar
bryanl committed
104 105 106 107 108 109
	}
)

func runAction(name initName, args map[string]interface{}) error {
	fn, ok := actionFns[name]
	if !ok {
bryanl's avatar
bryanl committed
110
		return errors.Errorf("invalid action %q", name)
bryanl's avatar
bryanl committed
111 112 113 114
	}

	return fn(args)
}