actions.go 6.1 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
// 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 (
	"fmt"

	"github.com/ksonnet/ksonnet/client"
	"github.com/ksonnet/ksonnet/metadata/app"
bryanl's avatar
bryanl committed
23 24
	"github.com/pkg/errors"
	"github.com/spf13/afero"
bryanl's avatar
bryanl committed
25 26 27
)

const (
bryanl's avatar
bryanl committed
28 29 30 31 32 33 34 35 36
	// OptionApp is app option.
	OptionApp = "app"
	// OptionArguments is arguments option. Used for passing arguments to prototypes.
	OptionArguments = "arguments"
	// OptionClientConfig is clientConfig option.
	OptionClientConfig = "client-config"
	// OptionComponentName is a componentName option.
	OptionComponentName = "component-name"
	// OptionComponentNames is componentNames option.
bryanl's avatar
bryanl committed
37
	OptionComponentNames = "component-names"
bryanl's avatar
bryanl committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	// OptionCreate is create option.
	OptionCreate = "create"
	// OptionDryRun is dryRun option.
	OptionDryRun = "dry-run"
	// OptionEnvName is envName option.
	OptionEnvName = "env-name"
	// OptionFs is fs option.
	OptionFs = "fs"
	// OptionGcTag is gcTag option.
	OptionGcTag = "gc-tag"
	// OptionGlobal is global option.
	OptionGlobal = "global"
	// OptionIndex is index option. Is used to target individual items in multi object
	// components.
	OptionIndex = "index"
	// OptionLibName is libName.
	OptionLibName = "lib-name"
	// OptionName is name option.
	OptionName = "name"
	// OptionNamespaceName is namespaceName option.
	OptionNamespaceName = "namespace-name"
	// OptionNewEnvName is newEnvName option. Used for renaming environments.
	OptionNewEnvName = "new-env-name"
	// OptionOutput is output option.
	OptionOutput = "output"
	// OptionOverride is override option.
	OptionOverride = "override"
	// OptionPackageName is packageName option.
	OptionPackageName = "package-name"
	// OptionPath is path option.
	OptionPath = "path"
	// OptionQuery is query option.
	OptionQuery = "query"
	// OptionRootPath is path option.
	OptionRootPath = "root-path"
	// OptionServer is server option.
	OptionServer = "server"
	// OptionServerURI is serverURI option.
	OptionServerURI = "server-uri"
77 78
	// OptionSkipDefaultRegistries is skipDefaultRegistries option. Used by init.
	OptionSkipDefaultRegistries = "skip-default-registries"
bryanl's avatar
bryanl committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	// OptionSkipGc is skipGc option.
	OptionSkipGc = "skip-gc"
	// OptionSpecFlag is specFlag option. Used for setting k8s spec.
	OptionSpecFlag = "spec-flag"
	// OptionURI is uri option. Used for setting registry URI.
	OptionURI = "URI"
	// OptionValue is value option.
	OptionValue = "value"
	// OptionVersion is version option.
	OptionVersion = "version"
)

var (
	// ErrNotInApp is an error stating the user is not in a ksonnet application directory
	// hierarchy.
	ErrNotInApp = errors.Errorf("could not find ksonnet app")
bryanl's avatar
bryanl committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
)

type missingOptionError struct {
	name string
}

func newMissingOptionError(name string) *missingOptionError {
	return &missingOptionError{
		name: name,
	}
}

func (e *missingOptionError) Error() string {
	return fmt.Sprintf("missing required %s option", e.name)
}

type invalidOptionError struct {
	name string
}

func newInvalidOptionError(name string) *invalidOptionError {
	return &invalidOptionError{
		name: name,
	}
}

func (e *invalidOptionError) Error() string {
	return fmt.Sprintf("invalid type for option %s", e.name)
}

type optionLoader struct {
	err error
	m   map[string]interface{}
}

func newOptionLoader(m map[string]interface{}) *optionLoader {
	return &optionLoader{
		m: m,
	}
}

bryanl's avatar
bryanl committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
func (o *optionLoader) loadFs(name string) afero.Fs {
	i := o.load(name)
	if i == nil {
		return nil
	}

	a, ok := i.(afero.Fs)
	if !ok {
		o.err = newInvalidOptionError(name)
		return nil
	}

	return a
}

bryanl's avatar
bryanl committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
func (o *optionLoader) loadBool(name string) bool {
	i := o.load(name)
	if i == nil {
		return false
	}

	a, ok := i.(bool)
	if !ok {
		o.err = newInvalidOptionError(name)
		return false
	}

	return a
}

bryanl's avatar
bryanl committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
func (o *optionLoader) loadOptionalBool(name string) bool {
	i := o.loadOptional(name)
	if i == nil {
		return false
	}

	a, ok := i.(bool)
	if !ok {
		return false
	}

	return a
}

func (o *optionLoader) loadInt(name string) int {
	i := o.load(name)
	if i == nil {
		return 0
	}

	a, ok := i.(int)
	if !ok {
		o.err = newInvalidOptionError(name)
		return 0
	}

	return a
}

func (o *optionLoader) loadOptionalInt(name string) int {
	i := o.loadOptional(name)
	if i == nil {
		return 0
	}

	a, ok := i.(int)
	if !ok {
		return 0
	}

	return a
}

bryanl's avatar
bryanl committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
func (o *optionLoader) loadString(name string) string {
	i := o.load(name)
	if i == nil {
		return ""
	}

	a, ok := i.(string)
	if !ok {
		o.err = newInvalidOptionError(name)
		return ""
	}

	return a
}

bryanl's avatar
bryanl committed
224 225
func (o *optionLoader) loadOptionalString(name string) string {
	i := o.loadOptional(name)
bryanl's avatar
bryanl committed
226
	if i == nil {
bryanl's avatar
bryanl committed
227
		return ""
bryanl's avatar
bryanl committed
228 229
	}

bryanl's avatar
bryanl committed
230
	a, ok := i.(string)
bryanl's avatar
bryanl committed
231
	if !ok {
bryanl's avatar
bryanl committed
232
		return ""
bryanl's avatar
bryanl committed
233 234 235 236 237
	}

	return a
}

bryanl's avatar
bryanl committed
238 239
func (o *optionLoader) loadStringSlice(name string) []string {
	i := o.load(name)
bryanl's avatar
bryanl committed
240 241 242 243
	if i == nil {
		return nil
	}

bryanl's avatar
bryanl committed
244
	a, ok := i.([]string)
bryanl's avatar
bryanl committed
245
	if !ok {
bryanl's avatar
bryanl committed
246
		o.err = newInvalidOptionError(name)
bryanl's avatar
bryanl committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
		return nil
	}

	return a
}

func (o *optionLoader) loadClientConfig() *client.Config {
	i := o.load(OptionClientConfig)
	if i == nil {
		return nil
	}

	a, ok := i.(*client.Config)
	if !ok {
		o.err = newInvalidOptionError(OptionClientConfig)
		return nil
	}

	return a
}

func (o *optionLoader) loadApp() app.App {
	i := o.load(OptionApp)
	if i == nil {
bryanl's avatar
bryanl committed
271
		o.err = ErrNotInApp
bryanl's avatar
bryanl committed
272 273 274 275 276 277 278 279 280 281 282 283
		return nil
	}

	a, ok := i.(app.App)
	if !ok {
		o.err = newInvalidOptionError(OptionApp)
		return nil
	}

	return a
}

bryanl's avatar
bryanl committed
284 285
func (o *optionLoader) load(key string) interface{} {
	if o.err != nil {
bryanl's avatar
bryanl committed
286 287 288
		return nil
	}

bryanl's avatar
bryanl committed
289
	i, ok := o.m[key]
bryanl's avatar
bryanl committed
290
	if !ok {
bryanl's avatar
bryanl committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304
		o.err = newMissingOptionError(key)
	}

	return i
}

func (o *optionLoader) loadOptional(key string) interface{} {
	if o.err != nil {
		return nil
	}

	i, ok := o.m[key]
	if !ok {
		return nil
bryanl's avatar
bryanl committed
305 306 307 308
	}

	return i
}