<commit_before>package bitrise

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"

	"gopkg.in/yaml.v2"

	log "github.com/Sirupsen/logrus"
	models "github.com/bitrise-io/bitrise-cli/models/models_1_0_0"
	stepmanModels "github.com/bitrise-io/stepman/models"

	"github.com/bitrise-io/go-pathutil/pathutil"
)

// ReadBitriseConfig ...
func ReadBitriseConfig(pth string) (models.BitriseDataModel, error) {
	log.Debugln("-> ReadBitriseConfig")
	if isExists, err := pathutil.IsPathExists(pth); err != nil {
		return models.BitriseDataModel{}, err
	} else if !isExists {
		return models.BitriseDataModel{}, errors.New(fmt.Sprint("No file found at path", pth))
	}

	bytes, err := ioutil.ReadFile(pth)
	if err != nil {
		return models.BitriseDataModel{}, err
	}
	var bitriseConfigFile models.BitriseConfigSerializeModel
	if err := yaml.Unmarshal(bytes, &bitriseConfigFile); err != nil {
		return models.BitriseDataModel{}, err
	}

	return bitriseConfigFile.ToBitriseDataModel()
}

// ReadSpecStep ...
func ReadSpecStep(pth string) (models.StepModel, error) {
	if isExists, err := pathutil.IsPathExists(pth); err != nil {
		return models.StepModel{}, err
	} else if !isExists {
		return models.StepModel{}, errors.New(fmt.Sprint("No file found at path", pth))
	}

	bytes, err := ioutil.ReadFile(pth)
	if err != nil {
		return models.StepModel{}, err
	}

	var specStep stepmanModels.StepModel
	if err := yaml.Unmarshal(bytes, &specStep); err != nil {
		return models.StepModel{}, err
	}

	return convertStepmanToBitriseStepModel(specStep)
}

func convertStepmanToBitriseStepModel(specStep stepmanModels.StepModel) (models.StepModel, error) {
	inputs := []models.EnvironmentItemModel{}
	for _, specEnv := range specStep.Inputs {
		env, err := convertStepmanToBitriseEnvironmentItemModel(specEnv)
		if err != nil {
			return models.StepModel{}, err
		}
		inputs = append(inputs, env)
	}

	outputs := []models.EnvironmentItemModel{}
	for _, specEnv := range specStep.Outputs {
		env, err := convertStepmanToBitriseEnvironmentItemModel(specEnv)
		if err != nil {
			return models.StepModel{}, err
		}
		outputs = append(outputs, env)
	}

	step := models.StepModel{
		Name:        specStep.Name,
		Description: specStep.Description,
		Website:     specStep.Website,
		ForkURL:     specStep.ForkURL,
		Source: models.StepSourceModel{
			Git: specStep.Source.Git,
		},
		HostOsTags:          specStep.HostOsTags,
		ProjectTypeTags:     specStep.ProjectTypeTags,
		TypeTags:            specStep.TypeTags,
		IsRequiresAdminUser: *specStep.IsRequiresAdminUser,<commit_msg>isAlways run fielad<commit_after>package bitrise

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"

	"gopkg.in/yaml.v2"

	log "github.com/Sirupsen/logrus"
	models "github.com/bitrise-io/bitrise-cli/models/models_1_0_0"
	stepmanModels "github.com/bitrise-io/stepman/models"

	"github.com/bitrise-io/go-pathutil/pathutil"
)

// ReadBitriseConfig ...
func ReadBitriseConfig(pth string) (models.BitriseDataModel, error) {
	log.Debugln("-> ReadBitriseConfig")
	if isExists, err := pathutil.IsPathExists(pth); err != nil {
		return models.BitriseDataModel{}, err
	} else if !isExists {
		return models.BitriseDataModel{}, errors.New(fmt.Sprint("No file found at path", pth))
	}

	bytes, err := ioutil.ReadFile(pth)
	if err != nil {
		return models.BitriseDataModel{}, err
	}
	var bitriseConfigFile models.BitriseConfigSerializeModel
	if err := yaml.Unmarshal(bytes, &bitriseConfigFile); err != nil {
		return models.BitriseDataModel{}, err
	}

	return bitriseConfigFile.ToBitriseDataModel()
}

// ReadSpecStep ...
func ReadSpecStep(pth string) (models.StepModel, error) {
	if isExists, err := pathutil.IsPathExists(pth); err != nil {
		return models.StepModel{}, err
	} else if !isExists {
		return models.StepModel{}, errors.New(fmt.Sprint("No file found at path", pth))
	}

	bytes, err := ioutil.ReadFile(pth)
	if err != nil {
		return models.StepModel{}, err
	}

	var specStep stepmanModels.StepModel
	if err := yaml.Unmarshal(bytes, &specStep); err != nil {
		return models.StepModel{}, err
	}

	return convertStepmanToBitriseStepModel(specStep)
}

func convertStepmanToBitriseStepModel(specStep stepmanModels.StepModel) (models.StepModel, error) {
	inputs := []models.EnvironmentItemModel{}
	for _, specEnv := range specStep.Inputs {
		env, err := convertStepmanToBitriseEnvironmentItemModel(specEnv)
		if err != nil {
			return models.StepModel{}, err
		}
		inputs = append(inputs, env)
	}

	outputs := []models.EnvironmentItemModel{}
	for _, specEnv := range specStep.Outputs {
		env, err := convertStepmanToBitriseEnvironmentItemModel(specEnv)
		if err != nil {
			return models.StepModel{}, err
		}
		outputs = append(outputs, env)
	}

	step := models.StepModel{
		Name:        specStep.Name,
		Description: specStep.Description,
		Website:     specStep.Website,
		ForkURL:     specStep.ForkURL,
		Source: models.StepSourceModel{
			Git: specStep.Source.Git,
		},
		HostOsTags:          specStep.HostOsTags,
		ProjectTypeTags:     specStep.ProjectTypeTags,
		TypeTags:            specStep.TypeTags,
		IsRequiresAdminUser: specStep.IsRequiresAdminUser,
		IsAlwaysRun:         specStep.IsAlwaysRun,
{'fraction_non_alphanumeric': 0.12334630350194553, 'fraction_numerical': 0.0015564202334630351, 'mean_word_length': 4.388888888888889, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7437312', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1742, 'n_words': 359}