Пример #1
0
func addEnv(key string, value string, expand, replace bool) error {
	// Validate input
	if key == "" {
		return errors.New("Key is not specified, required.")
	}

	// Load envs, or create if not exist
	environments, err := envman.ReadEnvsOrCreateEmptyList()
	if err != nil {
		return err
	}

	// Add or update envlist
	newEnv := models.EnvironmentItemModel{
		key: value,
		models.OptionsKey: models.EnvironmentItemOptionsModel{
			IsExpand: pointers.NewBoolPtr(expand),
		},
	}
	if err := newEnv.NormalizeEnvironmentItemModel(); err != nil {
		return err
	}

	newEnvSlice, err := envman.UpdateOrAddToEnvlist(environments, newEnv, replace)
	if err != nil {
		return err
	}

	if err := envman.WriteEnvMapToFile(envman.CurrentEnvStoreFilePath, newEnvSlice); err != nil {
		return err
	}

	return nil
}