<commit_before>package usecases

import (
	"io/ioutil"
	"k8s.io/client-go/pkg/api"
	_ "k8s.io/client-go/pkg/api/install"
	_ "k8s.io/client-go/pkg/apis/extensions/install"
	_ "k8s.io/client-go/pkg/apis/rbac/install"
	"k8s.io/client-go/pkg/api/v1"
	"k8s.io/client-go/pkg/apis/rbac/v1beta1"
	"log"
	"os"
	"regexp"
	"k8s.io/apimachinery/pkg/runtime"
	"fmt"
	"strings"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
)

type CustomRolesAndBindings struct {
	Roles    				map[string]bool
	RoleBindings 			map[string]bool
	ClusterRoles 			map[string]bool
	ClusterRoleBindings 	map[string]bool
	ServiceAccounts 		map[string]bool
}

func ReadAndApplyCustomRolesAndBindings() CustomRolesAndBindings {
	res := CustomRolesAndBindings{}

	customDir := getCustomRoleDir()
	customRolesPresent, err := fileExists(customDir)
	if err != nil {
		log.Printf("An error occurred while trying to read custom roles from directory %s. Err: %s", customDir, err)
		return res
	}
	if !customRolesPresent {
		log.Println("No custom-roles directory present, skipping step...")
		return res
	}

	files, err := ioutil.ReadDir(customDir)
	if err != nil {
		log.Printf("An error occurred while trying to read custom roles from directory %s. Err: %s", customDir, err)
		return res
	}

	regExp := regexp.MustCompile(`.*(\.yml|\.yaml)`)
	k8sclient := getK8sClient()
	for _, f := range files {
		isYaml := regExp.MatchString(f.Name())

		if !f.IsDir() && isYaml {
			fileR, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", customDir, f.Name()))
			if err != nil {
				log.Printf("An error occurred while reading file %s from directory %s. Err: %s", f.Name(), customDir, err)
				return res
			}


			objects := parseK8sYaml(fileR)
			for _, o := range objects {
				switch o := o.(type) {
				case *v1beta1.Role:
					res.Roles[o.Name] = true
					k8sclient.RbacV1beta1().Roles(o.Namespace).Create(o)
					log.Printf("Applied Custom Role %s in Namespace %s", o.Name, o.Namespace)
				case *v1beta1.RoleBinding:
					res.RoleBindings[o.Name] = true
					k8sclient.RbacV1beta1().RoleBindings(o.Namespace).Create(o)
					log.Printf("Applied Custom RoleBinding %s in Namespace %s", o.Name, o.Namespace)
				case *v1beta1.ClusterRole:
					res.ClusterRoles[o.Name] = true
					k8sclient.RbacV1beta1().ClusterRoles().Create(o)
					log.Printf("Applied Custom ClusterRole %s", o.Name)
				case *v1beta1.ClusterRoleBinding:
					res.ClusterRoleBindings[o.Name] = true
					k8sclient.RbacV1beta1().ClusterRoleBindings().Create(o)
					log.Printf("Applied Custom ClusterRoleBinding %s", o.Name)
				case *v1.ServiceAccount:
					res.ServiceAccounts[o.Name] = true
					k8sclient.CoreV1().ServiceAccounts(o.Namespace).Create(o)
					log.Printf("Applied Custom ServiceAccount %s in Namespace %s", o.Name, o.Namespace)
				}
			}
		}
	}
	return res<commit_msg>fixed uninitialized struct error<commit_after>package usecases

import (
	"io/ioutil"
	"k8s.io/client-go/pkg/api"
	_ "k8s.io/client-go/pkg/api/install"
	_ "k8s.io/client-go/pkg/apis/extensions/install"
	_ "k8s.io/client-go/pkg/apis/rbac/install"
	"k8s.io/client-go/pkg/api/v1"
	"k8s.io/client-go/pkg/apis/rbac/v1beta1"
	"log"
	"os"
	"regexp"
	"k8s.io/apimachinery/pkg/runtime"
	"fmt"
	"strings"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
)

type CustomRolesAndBindings struct {
	Roles    				map[string]bool
	RoleBindings 			map[string]bool
	ClusterRoles 			map[string]bool
	ClusterRoleBindings 	map[string]bool
	ServiceAccounts 		map[string]bool
}

func ReadAndApplyCustomRolesAndBindings() CustomRolesAndBindings {
	res := CustomRolesAndBindings{
		 Roles: 				make(map[string]bool),
		 RoleBindings:			make(map[string]bool),
		 ClusterRoles:			make(map[string]bool),
		 ClusterRoleBindings: 	make(map[string]bool),
		 ServiceAccounts:		make(map[string]bool),
	}

	customDir := getCustomRoleDir()
	customRolesPresent, err := fileExists(customDir)
	if err != nil {
		log.Printf("An error occurred while trying to read custom roles from directory %s. Err: %s", customDir, err)
		return res
	}
	if !customRolesPresent {
		log.Println("No custom-roles directory present, skipping step...")
		return res
	}

	files, err := ioutil.ReadDir(customDir)
	if err != nil {
		log.Printf("An error occurred while trying to read custom roles from directory %s. Err: %s", customDir, err)
		return res
	}

	regExp := regexp.MustCompile(`.*(\.yml|\.yaml)`)
	k8sclient := getK8sClient()
	for _, f := range files {
		isYaml := regExp.MatchString(f.Name())

		if !f.IsDir() && isYaml {
			fileR, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", customDir, f.Name()))
			if err != nil {
				log.Printf("An error occurred while reading file %s from directory %s. Err: %s", f.Name(), customDir, err)
				return res
			}


			objects := parseK8sYaml(fileR)
			for _, o := range objects {
				switch o := o.(type) {

				case *v1beta1.Role:
					res.Roles[o.Name] = true
					k8sclient.RbacV1beta1().Roles(o.Namespace).Create(o)
					log.Printf("Applied Custom Role %s in Namespace %s", o.Name, o.Namespace)
				case *v1beta1.RoleBinding:
					res.RoleBindings[o.Name] = true
					k8sclient.RbacV1beta1().RoleBindings(o.Namespace).Create(o)
					log.Printf("Applied Custom RoleBinding %s in Namespace %s", o.Name, o.Namespace)
				case *v1beta1.ClusterRole:
					res.ClusterRoles[o.Name] = true
					k8sclient.RbacV1beta1().ClusterRoles().Create(o)
					log.Printf("Applied Custom ClusterRole %s", o.Name)
				case *v1beta1.ClusterRoleBinding:
					res.ClusterRoleBindings[o.Name] = true
					k8sclient.RbacV1beta1().ClusterRoleBindings().Create(o)
					log.Printf("Applied Custom ClusterRoleBinding %s", o.Name)
				case *v1.ServiceAccount:
					res.ServiceAccounts[o.Name] = true
					k8sclient.CoreV1().ServiceAccounts(o.Namespace).Create(o)
					log.Printf("Applied Custom ServiceAccount %s in Namespace %s", o.Name, o.Namespace)
				}
			}
		}
	}
	return res
{'fraction_non_alphanumeric': 0.1402969790066564, 'fraction_numerical': 0.012971496842464585, 'mean_word_length': 3.84297520661157, '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': '5265112', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2212, 'n_words': 426}