env_set.go 2.89 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
// 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"
bryanl's avatar
bryanl committed
20
	"github.com/ksonnet/ksonnet/pkg/env"
bryanl's avatar
bryanl committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
)

// EnvSetNamespace is an option for setting a new namespace name.
func EnvSetNamespace(nsName string) EnvSetOpt {
	return func(es *EnvSet) {
		es.newNsName = nsName
	}
}

// EnvSetName is an option for setting a new name.
func EnvSetName(name string) EnvSetOpt {
	return func(es *EnvSet) {
		es.newName = name
	}
}

// EnvSetOpt is an option for configuring EnvSet.
type EnvSetOpt func(*EnvSet)

// RunEnvSet runs `env set`
bryanl's avatar
bryanl committed
41 42 43
// func RunEnvSet(ksApp app.App, envName string, opts ...EnvSetOpt) error {
func RunEnvSet(m map[string]interface{}) error {
	et, err := NewEnvSet(m)
bryanl's avatar
bryanl committed
44 45 46 47 48 49 50 51 52 53 54 55 56
	if err != nil {
		return err
	}

	return et.Run()
}

// EnvSet sets targets for an environment.
type EnvSet struct {
	app       app.App
	envName   string
	newName   string
	newNsName string
bryanl's avatar
bryanl committed
57

bryanl's avatar
bryanl committed
58 59
	envRenameFn func(a app.App, from, to string, override bool) error
	updateEnvFn func(a app.App, envName string, spec *app.EnvironmentSpec, override bool) error
bryanl's avatar
bryanl committed
60 61 62
}

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

bryanl's avatar
bryanl committed
66
	es := &EnvSet{
bryanl's avatar
bryanl committed
67 68 69
		app:       ol.loadApp(),
		envName:   ol.loadString(OptionEnvName),
		newName:   ol.loadOptionalString(OptionNewEnvName),
70
		newNsName: ol.loadOptionalString(OptionNamespace),
bryanl's avatar
bryanl committed
71 72 73

		envRenameFn: env.Rename,
		updateEnvFn: updateEnv,
bryanl's avatar
bryanl committed
74 75
	}

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

	return es, nil
}

// Run assigns targets to an environment.
func (es *EnvSet) Run() error {
bryanl's avatar
bryanl committed
85 86 87 88 89 90
	env, err := es.app.Environment(es.envName)
	if err != nil {
		return err
	}

	if err := es.updateName(env.IsOverride()); err != nil {
bryanl's avatar
bryanl committed
91 92 93
		return err
	}

bryanl's avatar
bryanl committed
94
	return es.updateNamespace(env)
bryanl's avatar
bryanl committed
95 96
}

bryanl's avatar
bryanl committed
97
func (es *EnvSet) updateName(isOverride bool) error {
bryanl's avatar
bryanl committed
98
	if es.newName != "" {
bryanl's avatar
bryanl committed
99
		if err := es.envRenameFn(es.app, es.envName, es.newName, isOverride); err != nil {
bryanl's avatar
bryanl committed
100 101 102 103 104 105 106 107 108
			return err
		}

		es.envName = es.newName
	}

	return nil
}

bryanl's avatar
bryanl committed
109
func (es *EnvSet) updateNamespace(env *app.EnvironmentSpec) error {
bryanl's avatar
bryanl committed
110
	if es.newNsName != "" {
bryanl's avatar
bryanl committed
111 112
		env.Destination.Namespace = es.newNsName
		return updateEnv(es.app, es.envName, env, env.IsOverride())
bryanl's avatar
bryanl committed
113 114 115 116
	}

	return nil
}
bryanl's avatar
bryanl committed
117 118 119 120

func updateEnv(a app.App, envName string, spec *app.EnvironmentSpec, override bool) error {
	return a.AddEnvironment(envName, "", spec, override)
}