func removeStepRedundantFields(step *stepmanModels.StepModel) error {
	if step.Title != nil && *step.Title == "" {
		step.Title = nil
	}
	if step.Description != nil && *step.Description == "" {
		step.Description = nil
	}
	if step.Summary != nil && *step.Summary == "" {
		step.Summary = nil
	}
	if step.Website != nil && *step.Website == "" {
		step.Website = nil
	}
	if step.SourceCodeURL != nil && *step.SourceCodeURL == "" {
		step.SourceCodeURL = nil
	}
	if step.SupportURL != nil && *step.SupportURL == "" {
		step.SupportURL = nil
	}
	if step.PublishedAt != nil && (*step.PublishedAt).Equal(time.Time{}) {
		step.PublishedAt = nil
	}
	if step.IsRequiresAdminUser != nil && *step.IsRequiresAdminUser == stepmanModels.DefaultIsRequiresAdminUser {
		step.IsRequiresAdminUser = nil
	}
	if step.IsAlwaysRun != nil && *step.IsAlwaysRun == stepmanModels.DefaultIsAlwaysRun {
		step.IsAlwaysRun = nil
	}
	if step.IsSkippable != nil && *step.IsSkippable == stepmanModels.DefaultIsSkippable {
		step.IsSkippable = nil
	}
	if step.RunIf != nil && *step.RunIf == "" {
		step.RunIf = nil
	}
	for _, env := range step.Inputs {
		if err := removeEnvironmentRedundantFields(&env); err != nil {
			return err
		}
	}
	for _, env := range step.Outputs {
		if err := removeEnvironmentRedundantFields(&env); err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 2
0
// MergeStepWith ...
func MergeStepWith(step, otherStep stepmanModels.StepModel) (stepmanModels.StepModel, error) {
	if otherStep.Title != nil {
		step.Title = pointers.NewStringPtr(*otherStep.Title)
	}
	if otherStep.Description != nil {
		step.Description = pointers.NewStringPtr(*otherStep.Description)
	}
	if otherStep.Summary != nil {
		step.Summary = pointers.NewStringPtr(*otherStep.Summary)
	}
	if otherStep.Website != nil {
		step.Website = pointers.NewStringPtr(*otherStep.Website)
	}
	if otherStep.SourceCodeURL != nil {
		step.SourceCodeURL = pointers.NewStringPtr(*otherStep.SourceCodeURL)
	}
	if otherStep.SupportURL != nil {
		step.SupportURL = pointers.NewStringPtr(*otherStep.SupportURL)
	}
	if otherStep.PublishedAt != nil {
		step.PublishedAt = pointers.NewTimePtr(*otherStep.PublishedAt)
	}
	if otherStep.Source.Git != "" {
		step.Source.Git = otherStep.Source.Git
	}
	if otherStep.Source.Commit != "" {
		step.Source.Commit = otherStep.Source.Commit
	}
	if len(otherStep.Dependencies) > 0 {
		step.Dependencies = otherStep.Dependencies
	}
	if len(otherStep.Deps.Brew) > 0 || len(otherStep.Deps.AptGet) > 0 || len(otherStep.Deps.CheckOnly) > 0 {
		step.Deps = otherStep.Deps
	}
	if len(otherStep.HostOsTags) > 0 {
		step.HostOsTags = otherStep.HostOsTags
	}
	if len(otherStep.ProjectTypeTags) > 0 {
		step.ProjectTypeTags = otherStep.ProjectTypeTags
	}
	if len(otherStep.TypeTags) > 0 {
		step.TypeTags = otherStep.TypeTags
	}
	if otherStep.IsRequiresAdminUser != nil {
		step.IsRequiresAdminUser = pointers.NewBoolPtr(*otherStep.IsRequiresAdminUser)
	}
	if otherStep.IsAlwaysRun != nil {
		step.IsAlwaysRun = pointers.NewBoolPtr(*otherStep.IsAlwaysRun)
	}
	if otherStep.IsSkippable != nil {
		step.IsSkippable = pointers.NewBoolPtr(*otherStep.IsSkippable)
	}
	if otherStep.RunIf != nil {
		step.RunIf = pointers.NewStringPtr(*otherStep.RunIf)
	}

	for _, input := range step.Inputs {
		key, _, err := input.GetKeyValuePair()
		if err != nil {
			return stepmanModels.StepModel{}, err
		}
		otherInput, found := getInputByKey(otherStep, key)
		if found {
			err := MergeEnvironmentWith(&input, otherInput)
			if err != nil {
				return stepmanModels.StepModel{}, err
			}
		}
	}

	for _, output := range step.Outputs {
		key, _, err := output.GetKeyValuePair()
		if err != nil {
			return stepmanModels.StepModel{}, err
		}
		otherOutput, found := getOutputByKey(otherStep, key)
		if found {
			err := MergeEnvironmentWith(&output, otherOutput)
			if err != nil {
				return stepmanModels.StepModel{}, err
			}
		}
	}

	return step, nil
}