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 }
func clearEnvs() error { if isExists, err := pathutil.IsPathExists(envman.CurrentEnvStoreFilePath); err != nil { return err } else if !isExists { errMsg := "EnvStore not found in path:" + envman.CurrentEnvStoreFilePath return errors.New(errMsg) } if err := envman.WriteEnvMapToFile(envman.CurrentEnvStoreFilePath, []models.EnvironmentItemModel{}); err != nil { return err } return nil }
func addEnv(key string, value string, expand, replace, skipIfEmpty bool) error { // Load envs, or create if not exist environments, err := envman.ReadEnvsOrCreateEmptyList() if err != nil { return err } // Validate input validatedValue, err := validateEnv(key, value, environments) if err != nil { return err } value = validatedValue // Add or update envlist newEnv := models.EnvironmentItemModel{ key: value, models.OptionsKey: models.EnvironmentItemOptionsModel{ IsExpand: pointers.NewBoolPtr(expand), SkipIfEmpty: pointers.NewBoolPtr(skipIfEmpty), }, } if err := newEnv.NormalizeValidateFillDefaults(); 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 }