<commit_before>package provprofile

import (
	"fmt"
	"path/filepath"
	"strings"
	"time"

	plist "github.com/DHowett/go-plist"
	log "github.com/Sirupsen/logrus"
	"github.com/bitrise-io/go-utils/cmdex"
	"github.com/bitrise-io/go-utils/maputil"
	"github.com/bitrise-io/go-utils/pathutil"
	"github.com/ryanuber/go-glob"
)

const (
	provProfileSystemDirPath = "~/Library/MobileDevice/Provisioning Profiles"
)

// ProvisioningProfileInfo ...
type ProvisioningProfileInfo struct {
	Title string
	UUID  string
}

// EntitlementsModel ...
type EntitlementsModel struct {
	AppID  string `plist:"application-identifier"`
	TeamID string `plist:"com.apple.developer.team-identifier"`
}

// ProvisioningProfileModel ...
type ProvisioningProfileModel struct {
	Entitlements   EntitlementsModel `plist:"Entitlements"`
	UUID           string            `plist:"UUID"`
	TeamName       string            `plist:"TeamName"`
	Name           string            `plist:"Name"`
	AppIDName      string            `plist:"AppIDName"`
	ExpirationDate time.Time         `plist:"ExpirationDate"`
}

// ProvisioningProfileFileInfoModel ...
type ProvisioningProfileFileInfoModel struct {
	Path                    string
	ProvisioningProfileInfo ProvisioningProfileModel
}

// ProvisioningProfileFileInfoModels ...
type ProvisioningProfileFileInfoModels []ProvisioningProfileFileInfoModel

// CollectTeamIDs ...
func (ppFileInfos ProvisioningProfileFileInfoModels) CollectTeamIDs() []string {
	teamIDsMap := map[string]interface{}{}
	for _, aProvProfileFileInfo := range ppFileInfos {
		teamIDsMap[aProvProfileFileInfo.ProvisioningProfileInfo.Entitlements.TeamID] = 1
	}
	return maputil.KeysOfStringInterfaceMap(teamIDsMap)
}

// CreateProvisioningProfileModelFromFile ...
func CreateProvisioningProfileModelFromFile(filePth string) (ProvisioningProfileModel, error) {
	profileContent, err := cmdex.NewCommand("security", "cms", "-D", "-i", filePth).RunAndReturnTrimmedCombinedOutput()
	if err != nil {
		return ProvisioningProfileModel{},
			fmt.Errorf("Failed to retrieve information about Provisioning Profile, error: %s",
				err)
	}

	var provProfileData ProvisioningProfileModel
	if err := plist.NewDecoder(strings.NewReader(profileContent)).Decode(&provProfileData); err != nil {
		return provProfileData,
			fmt.Errorf("Failed to parse Provisioning Profile content, error: %s", err)
	}

	if provProfileData.UUID == "" {
		return provProfileData,
			fmt.Errorf("No UUID found in the Provisioning Profile (%#v)", provProfileData)
	}

	return provProfileData, nil
}

// FindProvProfilesByAppID ...
// `appID`` supports "glob", e.g.: *.bundle.id will match any Prov Profile with ".bundle.id"
//   app ID suffix
func FindProvProfilesByAppID(appID string) ([]ProvisioningProfileFileInfoModel, error) {<commit_msg>debug print all found prov profile content<commit_after>package provprofile

import (
	"fmt"
	"path/filepath"
	"strings"
	"time"

	plist "github.com/DHowett/go-plist"
	log "github.com/Sirupsen/logrus"
	"github.com/bitrise-io/go-utils/cmdex"
	"github.com/bitrise-io/go-utils/maputil"
	"github.com/bitrise-io/go-utils/pathutil"
	"github.com/ryanuber/go-glob"
)

const (
	provProfileSystemDirPath = "~/Library/MobileDevice/Provisioning Profiles"
)

// ProvisioningProfileInfo ...
type ProvisioningProfileInfo struct {
	Title string
	UUID  string
}

// EntitlementsModel ...
type EntitlementsModel struct {
	AppID  string `plist:"application-identifier"`
	TeamID string `plist:"com.apple.developer.team-identifier"`
}

// ProvisioningProfileModel ...
type ProvisioningProfileModel struct {
	Entitlements   EntitlementsModel `plist:"Entitlements"`
	UUID           string            `plist:"UUID"`
	TeamName       string            `plist:"TeamName"`
	Name           string            `plist:"Name"`
	AppIDName      string            `plist:"AppIDName"`
	ExpirationDate time.Time         `plist:"ExpirationDate"`
}

// ProvisioningProfileFileInfoModel ...
type ProvisioningProfileFileInfoModel struct {
	Path                    string
	ProvisioningProfileInfo ProvisioningProfileModel
}

// ProvisioningProfileFileInfoModels ...
type ProvisioningProfileFileInfoModels []ProvisioningProfileFileInfoModel

// CollectTeamIDs ...
func (ppFileInfos ProvisioningProfileFileInfoModels) CollectTeamIDs() []string {
	teamIDsMap := map[string]interface{}{}
	for _, aProvProfileFileInfo := range ppFileInfos {
		teamIDsMap[aProvProfileFileInfo.ProvisioningProfileInfo.Entitlements.TeamID] = 1
	}
	return maputil.KeysOfStringInterfaceMap(teamIDsMap)
}

// CreateProvisioningProfileModelFromFile ...
func CreateProvisioningProfileModelFromFile(filePth string) (ProvisioningProfileModel, error) {
	profileContent, err := cmdex.NewCommand("security", "cms", "-D", "-i", filePth).RunAndReturnTrimmedCombinedOutput()
	if err != nil {
		return ProvisioningProfileModel{},
			fmt.Errorf("Failed to retrieve information about Provisioning Profile, error: %s",
				err)
	}

	log.Debugln("profileContent: ", profileContent)

	var provProfileData ProvisioningProfileModel
	if err := plist.NewDecoder(strings.NewReader(profileContent)).Decode(&provProfileData); err != nil {
		return provProfileData,
			fmt.Errorf("Failed to parse Provisioning Profile content, error: %s", err)
	}

	if provProfileData.UUID == "" {
		return provProfileData,
			fmt.Errorf("No UUID found in the Provisioning Profile (%#v)", provProfileData)
	}

	return provProfileData, nil
}

// FindProvProfilesByAppID ...
// `appID`` supports "glob", e.g.: *.bundle.id will match any Prov Profile with ".bundle.id"
//   app ID suffix
func FindProvProfilesByAppID(appID string) ([]ProvisioningProfileFileInfoModel, error) {
{'fraction_non_alphanumeric': 0.1111111111111111, 'fraction_numerical': 0.000354421407052986, 'mean_word_length': 4.495618305744888, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2236481', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1716, 'n_words': 350}