本文整理汇总了Golang中github.com/bitrise-io/envman/models.EnvironmentItemModel类的典型用法代码示例。如果您正苦于以下问题:Golang EnvironmentItemModel类的具体用法?Golang EnvironmentItemModel怎么用?Golang EnvironmentItemModel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EnvironmentItemModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: addEnv
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
}
示例2: removeDefaults
func removeDefaults(env *models.EnvironmentItemModel) error {
opts, err := env.GetOptions()
if err != nil {
return err
}
if opts.Title != nil && *opts.Title == "" {
opts.Title = nil
}
if opts.Description != nil && *opts.Description == "" {
opts.Description = nil
}
if opts.Summary != nil && *opts.Summary == "" {
opts.Summary = nil
}
if opts.IsRequired != nil && *opts.IsRequired == models.DefaultIsRequired {
opts.IsRequired = nil
}
if opts.IsExpand != nil && *opts.IsExpand == models.DefaultIsExpand {
opts.IsExpand = nil
}
if opts.IsDontChangeValue != nil && *opts.IsDontChangeValue == models.DefaultIsDontChangeValue {
opts.IsDontChangeValue = nil
}
(*env)[models.OptionsKey] = opts
return nil
}
示例3: removeEnvironmentRedundantFields
func removeEnvironmentRedundantFields(env *envmanModels.EnvironmentItemModel) error {
options, err := env.GetOptions()
if err != nil {
return err
}
hasOptions := false
if options.Title != nil {
if *options.Title == "" {
options.Title = nil
} else {
hasOptions = true
}
}
if options.Description != nil {
if *options.Description == "" {
options.Description = nil
} else {
hasOptions = true
}
}
if options.Summary != nil {
if *options.Summary == "" {
options.Summary = nil
} else {
hasOptions = true
}
}
if options.IsRequired != nil {
if *options.IsRequired == envmanModels.DefaultIsRequired {
options.IsRequired = nil
} else {
hasOptions = true
}
}
if options.IsExpand != nil {
if *options.IsExpand == envmanModels.DefaultIsExpand {
options.IsExpand = nil
} else {
hasOptions = true
}
}
if options.IsDontChangeValue != nil {
if *options.IsDontChangeValue == envmanModels.DefaultIsDontChangeValue {
options.IsDontChangeValue = nil
} else {
hasOptions = true
}
}
if hasOptions {
(*env)[envmanModels.OptionsKey] = options
} else {
delete(*env, envmanModels.OptionsKey)
}
return nil
}
示例4: convert
func (input InputModel) convert() (envmanModels.EnvironmentItemModel, error) {
environment := envmanModels.EnvironmentItemModel{
input.MappedTo: input.Value,
envmanModels.OptionsKey: input.getOptions(),
}
if err := environment.FillMissingDefaults(); err != nil {
return envmanModels.EnvironmentItemModel{}, err
}
return environment, nil
}
示例5: MergeEnvironmentWith
// MergeEnvironmentWith ...
func MergeEnvironmentWith(env *envmanModels.EnvironmentItemModel, otherEnv envmanModels.EnvironmentItemModel) error {
// merge key-value
key, _, err := env.GetKeyValuePair()
if err != nil {
return err
}
otherKey, otherValue, err := otherEnv.GetKeyValuePair()
if err != nil {
return err
}
if otherKey != key {
return errors.New("Env keys are diferent")
}
(*env)[key] = otherValue
//merge options
options, err := env.GetOptions()
if err != nil {
return err
}
otherOptions, err := otherEnv.GetOptions()
if err != nil {
return err
}
if otherOptions.Title != nil {
options.Title = pointers.NewStringPtr(*otherOptions.Title)
}
if otherOptions.Description != nil {
options.Description = pointers.NewStringPtr(*otherOptions.Description)
}
if otherOptions.Summary != nil {
options.Summary = pointers.NewStringPtr(*otherOptions.Summary)
}
if len(otherOptions.ValueOptions) > 0 {
options.ValueOptions = otherOptions.ValueOptions
}
if otherOptions.IsRequired != nil {
options.IsRequired = pointers.NewBoolPtr(*otherOptions.IsRequired)
}
if otherOptions.IsExpand != nil {
options.IsExpand = pointers.NewBoolPtr(*otherOptions.IsExpand)
}
if otherOptions.IsDontChangeValue != nil {
options.IsDontChangeValue = pointers.NewBoolPtr(*otherOptions.IsDontChangeValue)
}
if otherOptions.IsTemplate != nil {
options.IsTemplate = pointers.NewBoolPtr(*otherOptions.IsTemplate)
}
(*env)[envmanModels.OptionsKey] = options
return nil
}
示例6: ValidateStepInputOutputModel
// ValidateStepInputOutputModel ...
func ValidateStepInputOutputModel(env envmanModels.EnvironmentItemModel, checkRequiredFields bool) error {
if err := env.Validate(); err != nil {
return err
}
if checkRequiredFields {
options, err := env.GetOptions()
if err != nil {
return err
}
if options.Title == nil || *options.Title == "" {
return errors.New("Invalid environment: missing or empty title")
}
}
return nil
}
示例7: countOfEnvKeyInEnvSlice
func countOfEnvKeyInEnvSlice(env models.EnvironmentItemModel, envSlice []models.EnvironmentItemModel) (cnt int, err error) {
for _, e := range envSlice {
key, _, err := env.GetKeyValuePair()
if err != nil {
return 0, err
}
k, _, err := e.GetKeyValuePair()
if err != nil {
return 0, err
}
if key == k {
cnt++
}
}
return
}
示例8: TestRemoveDefaults
func TestRemoveDefaults(t *testing.T) {
defaultIsRequired := models.DefaultIsRequired
defaultIsExpand := models.DefaultIsExpand
defaultIsDontChangeValue := models.DefaultIsDontChangeValue
// Filled env
env := models.EnvironmentItemModel{
testKey: testValue,
models.OptionsKey: models.EnvironmentItemOptionsModel{
Title: pointers.NewStringPtr(testTitle),
Description: pointers.NewStringPtr(testEmptyString),
ValueOptions: []string{},
IsRequired: pointers.NewBoolPtr(defaultIsRequired),
IsExpand: pointers.NewBoolPtr(defaultIsExpand),
IsDontChangeValue: pointers.NewBoolPtr(defaultIsDontChangeValue),
},
}
err := removeDefaults(&env)
if err != nil {
t.Fatal(err)
}
opts, err := env.GetOptions()
if err != nil {
t.Fatal(err)
}
if opts.Title == nil {
t.Fatal("Removed Title")
}
if opts.Description != nil {
t.Fatal("Failed to remove default Description")
}
if opts.IsRequired != nil {
t.Fatal("Failed to remove default IsRequired")
}
if opts.IsExpand != nil {
t.Fatal("Failed to remove default IsExpand")
}
if opts.IsDontChangeValue != nil {
t.Fatal("Failed to remove default IsDontChangeValue")
}
}
示例9: ValidateStepInputOutputModel
// ValidateStepInputOutputModel ...
func ValidateStepInputOutputModel(env envmanModels.EnvironmentItemModel) error {
key, _, err := env.GetKeyValuePair()
if err != nil {
return err
}
if key == "" {
return errors.New("Invalid environment: empty env_key")
}
options, err := env.GetOptions()
if err != nil {
return err
}
if options.Title == nil || *options.Title == "" {
return errors.New("Invalid environment: missing or empty title")
}
return nil
}
示例10: addEnv
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
}
示例11: TestRemoveDefaults
func TestRemoveDefaults(t *testing.T) {
// Filled env
env := models.EnvironmentItemModel{
"test_key": "test_value",
models.OptionsKey: models.EnvironmentItemOptionsModel{
Title: pointers.NewStringPtr("test_title"),
IsTemplate: pointers.NewBoolPtr(!models.DefaultIsTemplate),
Description: pointers.NewStringPtr(""),
Summary: pointers.NewStringPtr(""),
ValueOptions: []string{},
IsRequired: pointers.NewBoolPtr(models.DefaultIsRequired),
IsDontChangeValue: pointers.NewBoolPtr(models.DefaultIsDontChangeValue),
IsExpand: pointers.NewBoolPtr(models.DefaultIsExpand),
SkipIfEmpty: pointers.NewBoolPtr(models.DefaultSkipIfEmpty),
},
}
require.Equal(t, nil, removeDefaults(&env))
opts, err := env.GetOptions()
require.Equal(t, nil, err)
require.NotEqual(t, (*string)(nil), opts.Title)
require.Equal(t, "test_title", *opts.Title)
require.NotEqual(t, (*bool)(nil), opts.IsTemplate)
require.Equal(t, !models.DefaultIsTemplate, *opts.IsTemplate)
require.Equal(t, (*string)(nil), opts.Description)
require.Equal(t, (*string)(nil), opts.Summary)
require.Equal(t, 0, len(opts.ValueOptions))
require.Equal(t, (*bool)(nil), opts.IsRequired)
require.Equal(t, (*bool)(nil), opts.IsDontChangeValue)
require.Equal(t, (*bool)(nil), opts.IsExpand)
require.Equal(t, (*bool)(nil), opts.SkipIfEmpty)
}
示例12: TestMergeEnvironmentWith
func TestMergeEnvironmentWith(t *testing.T) {
diffEnv := envmanModels.EnvironmentItemModel{
"test_key": "test_value",
envmanModels.OptionsKey: envmanModels.EnvironmentItemOptionsModel{
Title: pointers.NewStringPtr("test_title"),
Description: pointers.NewStringPtr("test_description"),
Summary: pointers.NewStringPtr("test_summary"),
ValueOptions: []string{"test_valu_options1", "test_valu_options2"},
IsRequired: pointers.NewBoolPtr(true),
IsExpand: pointers.NewBoolPtr(false),
IsDontChangeValue: pointers.NewBoolPtr(true),
IsTemplate: pointers.NewBoolPtr(true),
},
}
t.Log("Different keys")
{
env := envmanModels.EnvironmentItemModel{
"test_key1": "test_value",
}
require.Error(t, MergeEnvironmentWith(&env, diffEnv))
}
t.Log("Normal merge")
{
env := envmanModels.EnvironmentItemModel{
"test_key": "test_value",
envmanModels.OptionsKey: envmanModels.EnvironmentItemOptionsModel{
SkipIfEmpty: pointers.NewBoolPtr(true),
Category: pointers.NewStringPtr("test"),
},
}
require.NoError(t, MergeEnvironmentWith(&env, diffEnv))
options, err := env.GetOptions()
require.NoError(t, err)
diffOptions, err := diffEnv.GetOptions()
require.NoError(t, err)
require.Equal(t, *diffOptions.Title, *options.Title)
require.Equal(t, *diffOptions.Description, *options.Description)
require.Equal(t, *diffOptions.Summary, *options.Summary)
require.Equal(t, len(diffOptions.ValueOptions), len(options.ValueOptions))
require.Equal(t, *diffOptions.IsRequired, *options.IsRequired)
require.Equal(t, *diffOptions.IsExpand, *options.IsExpand)
require.Equal(t, *diffOptions.IsDontChangeValue, *options.IsDontChangeValue)
require.Equal(t, *diffOptions.IsTemplate, *options.IsTemplate)
require.Equal(t, true, *options.SkipIfEmpty)
require.Equal(t, "test", *options.Category)
}
}
示例13: ValidateStepInputOutputModel
// ValidateStepInputOutputModel ...
func ValidateStepInputOutputModel(env envmanModels.EnvironmentItemModel, checkRequiredFields bool) error {
key, _, err := env.GetKeyValuePair()
if err != nil {
return fmt.Errorf("Invalid environment (%s), err: %s", key, err)
}
if err := env.Validate(); err != nil {
return fmt.Errorf("Invalid environment (%s), err: %s", key, err)
}
if checkRequiredFields {
options, err := env.GetOptions()
if err != nil {
return fmt.Errorf("Invalid environment (%s), err: %s", key, err)
}
if options.Title == nil || *options.Title == "" {
return fmt.Errorf("Invalid environment (%s), err: missing or empty title", key)
}
}
return nil
}
示例14: TestUpdateOrAddToEnvlist
func TestUpdateOrAddToEnvlist(t *testing.T) {
env1 := models.EnvironmentItemModel{
"test_key1": "test_value1",
}
require.Equal(t, nil, env1.FillMissingDefaults())
env2 := models.EnvironmentItemModel{
"test_key2": "test_value2",
}
require.Equal(t, nil, env2.FillMissingDefaults())
// Should add to list, but not override
oldEnvSlice := []models.EnvironmentItemModel{env1, env2}
newEnvSlice, err := UpdateOrAddToEnvlist(oldEnvSlice, env1, false)
require.Equal(t, nil, err)
env1Cnt, err := countOfEnvKeyInEnvSlice(env1, newEnvSlice)
require.Equal(t, nil, err)
require.Equal(t, 2, env1Cnt)
env2Cnt, err := countOfEnvKeyInEnvSlice(env2, newEnvSlice)
require.Equal(t, nil, err)
require.Equal(t, 1, env2Cnt)
// Should update list
oldEnvSlice = []models.EnvironmentItemModel{env1, env2}
newEnvSlice, err = UpdateOrAddToEnvlist(oldEnvSlice, env1, true)
require.Equal(t, nil, err)
env1Cnt, err = countOfEnvKeyInEnvSlice(env1, newEnvSlice)
require.Equal(t, nil, err)
require.Equal(t, 1, env1Cnt)
env2Cnt, err = countOfEnvKeyInEnvSlice(env2, newEnvSlice)
require.Equal(t, nil, err)
require.Equal(t, 1, env2Cnt)
}
示例15: TestCommandEnvs
func TestCommandEnvs(t *testing.T) {
t.Log("commandEnvs test")
{
env1 := models.EnvironmentItemModel{
"test_key1": "test_value1",
}
require.Equal(t, nil, env1.FillMissingDefaults())
env2 := models.EnvironmentItemModel{
"test_key2": "test_value2",
}
require.Equal(t, nil, env2.FillMissingDefaults())
envs := []models.EnvironmentItemModel{env1, env2}
sessionEnvs, err := commandEnvs(envs)
require.Equal(t, nil, err)
env1Found := false
env2Found := false
for _, envString := range sessionEnvs {
comp := strings.Split(envString, "=")
key := comp[0]
value := comp[1]
envKey1, envValue1, err := env1.GetKeyValuePair()
require.Equal(t, nil, err)
envKey2, envValue2, err := env2.GetKeyValuePair()
require.Equal(t, nil, err)
if key == envKey1 && value == envValue1 {
env1Found = true
}
if key == envKey2 && value == envValue2 {
env2Found = true
}
}
require.Equal(t, true, env1Found)
require.Equal(t, true, env2Found)
}
// Test skip_if_empty
t.Log("skip_if_empty=false && value=empty => should add")
{
env1 := models.EnvironmentItemModel{
"test_key3": "",
}
require.Equal(t, nil, env1.FillMissingDefaults())
env2 := models.EnvironmentItemModel{
"test_key4": "test_value4",
}
require.Equal(t, nil, env2.FillMissingDefaults())
envs := []models.EnvironmentItemModel{env1, env2}
sessionEnvs, err := commandEnvs(envs)
require.Equal(t, nil, err)
env1Found := false
env2Found := false
for _, envString := range sessionEnvs {
comp := strings.Split(envString, "=")
key := comp[0]
value := comp[1]
envKey1, envValue1, err := env1.GetKeyValuePair()
require.Equal(t, nil, err)
envKey2, envValue2, err := env2.GetKeyValuePair()
require.Equal(t, nil, err)
if key == envKey1 && value == envValue1 {
env1Found = true
}
if key == envKey2 && value == envValue2 {
env2Found = true
}
}
require.Equal(t, true, env1Found)
require.Equal(t, true, env2Found)
}
t.Log("skip_if_empty=true && value=empty => should NOT add")
{
env1 := models.EnvironmentItemModel{
"test_key5": "",
"opts": models.EnvironmentItemOptionsModel{
SkipIfEmpty: pointers.NewBoolPtr(true),
},
}
require.Equal(t, nil, env1.FillMissingDefaults())
env2 := models.EnvironmentItemModel{
"test_key6": "test_value6",
}
require.Equal(t, nil, env2.FillMissingDefaults())
envs := []models.EnvironmentItemModel{env1, env2}
//.........这里部分代码省略.........