loader

package
v1.1.1-0...-9418116 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2020 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var PredefinedVars = map[string]*predefinedVarDefinition{
	"DEVSPACE_RANDOM": &predefinedVarDefinition{
		Fill: func(options *ConfigOptions) (*string, error) {
			ret, err := randutil.GenerateRandomString(6)
			if err != nil {
				return nil, err
			}

			return &ret, nil
		},
	},
	"DEVSPACE_TIMESTAMP": &predefinedVarDefinition{
		Fill: func(options *ConfigOptions) (*string, error) {
			return ptr.String(strconv.FormatInt(time.Now().Unix(), 10)), nil
		},
	},
	"DEVSPACE_GIT_COMMIT": &predefinedVarDefinition{
		ErrorMessage: "No git repository found, but predefined var DEVSPACE_GIT_COMMIT is used",
		Fill: func(options *ConfigOptions) (*string, error) {
			gitRepo := git.NewGitRepository(".", "")

			hash, err := gitRepo.GetHash()
			if err != nil {
				return nil, nil
			}

			return ptr.String(hash[:8]), nil
		},
	},
	"DEVSPACE_SPACE": &predefinedVarDefinition{
		ErrorMessage: fmt.Sprintf("Current context is not a space, but predefined var DEVSPACE_SPACE is used.\n\nPlease run: \n- `%s` to create a new space\n- `%s` to use an existing space\n- `%s` to list existing spaces", ansi.Color("devspace create space [NAME]", "white+b"), ansi.Color("devspace use space [NAME]", "white+b"), ansi.Color("devspace list spaces", "white+b")),
		Fill: func(options *ConfigOptions) (*string, error) {
			kubeContext, err := kubeconfig.GetCurrentContext()
			if err != nil {
				return nil, nil
			}
			if options.KubeContext != "" {
				kubeContext = options.KubeContext
			}

			isSpace, err := kubeconfig.IsCloudSpace(kubeContext)
			if err != nil || !isSpace {
				return nil, nil
			}

			spaceID, providerName, err := kubeconfig.GetSpaceID(kubeContext)
			if err != nil {
				return nil, err
			}

			loader := cloudconfig.NewLoader()
			cloudConfigData, err := loader.Load()
			if err != nil {
				return nil, nil
			}

			provider := cloudconfig.GetProvider(cloudConfigData, providerName)
			if provider == nil {
				return nil, nil
			}
			if provider.Spaces == nil {
				return nil, nil
			}
			if provider.Spaces[spaceID] == nil {
				return nil, nil
			}

			return &provider.Spaces[spaceID].Space.Name, nil
		},
	},
	"DEVSPACE_SPACE_NAMESPACE": &predefinedVarDefinition{
		ErrorMessage: fmt.Sprintf("Current context is not a space, but predefined var DEVSPACE_SPACE_NAMESPACE is used.\n\nPlease run: \n- `%s` to create a new space\n- `%s` to use an existing space\n- `%s` to list existing spaces", ansi.Color("devspace create space [NAME]", "white+b"), ansi.Color("devspace use space [NAME]", "white+b"), ansi.Color("devspace list spaces", "white+b")),
		Fill: func(options *ConfigOptions) (*string, error) {
			kubeContext, err := kubeconfig.GetCurrentContext()
			if err != nil {
				return nil, nil
			}
			if options.KubeContext != "" {
				kubeContext = options.KubeContext
			}

			isSpace, err := kubeconfig.IsCloudSpace(kubeContext)
			if err != nil || !isSpace {
				return nil, nil
			}

			spaceID, providerName, err := kubeconfig.GetSpaceID(kubeContext)
			if err != nil {
				return nil, err
			}

			loader := cloudconfig.NewLoader()
			cloudConfigData, err := loader.Load()
			if err != nil {
				return nil, nil
			}

			provider := cloudconfig.GetProvider(cloudConfigData, providerName)
			if provider == nil {
				return nil, nil
			}
			if provider.Spaces == nil {
				return nil, nil
			}
			if provider.Spaces[spaceID] == nil {
				return nil, nil
			}

			return &provider.Spaces[spaceID].ServiceAccount.Namespace, nil
		},
	},
	"DEVSPACE_USERNAME": &predefinedVarDefinition{
		ErrorMessage: fmt.Sprintf("You are not logged into DevSpace Cloud, but predefined var DEVSPACE_USERNAME is used.\n\nPlease run: \n- `%s` to login into devspace cloud. Alternatively you can also remove the variable ${DEVSPACE_USERNAME} from your config", ansi.Color("devspace login", "white+b")),
		Fill: func(options *ConfigOptions) (*string, error) {
			kubeContext, err := kubeconfig.GetCurrentContext()
			if err != nil {
				return nil, err
			}
			if options.KubeContext != "" {
				kubeContext = options.KubeContext
			}

			loader := cloudconfig.NewLoader()
			cloudConfigData, err := loader.Load()
			if err != nil {
				return nil, err
			}

			_, providerName, err := kubeconfig.GetSpaceID(kubeContext)
			if err != nil {

				if cloudConfigData.Default != "" {
					providerName = cloudConfigData.Default
				} else {
					providerName = cloudconfig.DevSpaceCloudProviderName
				}
			}

			provider := cloudconfig.GetProvider(cloudConfigData, providerName)
			if provider == nil {
				return nil, nil
			}
			if provider.Token == "" {
				return nil, nil
			}

			accountName, err := cloudtoken.GetAccountName(provider.Token)
			if err != nil {
				return nil, nil
			}

			return &accountName, nil
		},
	},
}

PredefinedVars holds all predefined variables that can be used in the config

Functions

func ApplyPatches

func ApplyPatches(data map[interface{}]interface{}, profile map[interface{}]interface{}) (map[interface{}]interface{}, error)

ApplyPatches applies the patches to the config if defined

func ApplyReplace

func ApplyReplace(config map[interface{}]interface{}, profile map[interface{}]interface{}) error

ApplyReplace applies the replaces

func GetProfiles

func GetProfiles(basePath string) ([]string, error)

GetProfiles retrieves all available profiles

Types

type ConfigLoader

type ConfigLoader interface {
	New() *latest.Config
	Exists() bool

	Load() (*latest.Config, error)
	LoadFromPath(generatedConfig *generated.Config, path string) (*latest.Config, error)
	LoadRaw(path string) (map[interface{}]interface{}, error)
	LoadWithoutProfile() (*latest.Config, error)

	ParseCommands(generatedConfig *generated.Config, data map[interface{}]interface{}) ([]*latest.CommandConfig, error)

	Generated() (*generated.Config, error)
	SaveGenerated(generatedConfig *generated.Config) error

	Save(config *latest.Config) error
	RestoreVars(config *latest.Config) (*latest.Config, error)
	SetDevSpaceRoot() (bool, error)
}

ConfigLoader is the base interface for the main config loader

func NewConfigLoader

func NewConfigLoader(options *ConfigOptions, log log.Logger) ConfigLoader

NewConfigLoader creates a new config loader with the given options

type ConfigOptions

type ConfigOptions struct {
	Profile     string
	KubeContext string

	LoadedVars map[string]string
	Vars       []string
}

ConfigOptions defines options to load the config

func (*ConfigOptions) Clone

func (co *ConfigOptions) Clone() (*ConfigOptions, error)

Clone clones the config options

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL