<commit_before>/*
Copyright 2015 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package repo

import (
	"fmt"
	"strings"
	"sync"
)

type inmemRepoService struct {
	sync.RWMutex
	repositories map[string]IRepo
}

// NewInmemRepoService returns a new memory based repository service.
func NewInmemRepoService() IRepoService {
	rs := &inmemRepoService{
		repositories: make(map[string]IRepo),
	}

	r, err := NewPublicGCSRepo(nil)
	if err == nil {
		rs.CreateRepo(r)
	}

	return rs
}

// ListRepos returns the list of all known chart repositories
func (rs *inmemRepoService) ListRepos() (map[string]string, error) {
	rs.RLock()
	defer rs.RUnlock()

	ret := make(map[string]string)
	for _, r := range rs.repositories {
		ret[r.GetName()] = r.GetURL()
	}

	return ret, nil
}

// CreateRepo adds a known repository to the list
func (rs *inmemRepoService) CreateRepo(repository IRepo) error {
	rs.Lock()
	defer rs.Unlock()

	URL := repository.GetURL()
	_, ok := rs.repositories[URL]
	if ok {
		return fmt.Errorf("Repository with URL %s already exists", URL)<commit_msg>feat(repo): prevent from adding duplicate repos<commit_after>/*
Copyright 2015 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package repo

import (
	"fmt"
	"strings"
	"sync"
)

type inmemRepoService struct {
	sync.RWMutex
	repositories map[string]IRepo
}

// NewInmemRepoService returns a new memory based repository service.
func NewInmemRepoService() IRepoService {
	rs := &inmemRepoService{
		repositories: make(map[string]IRepo),
	}

	r, err := NewPublicGCSRepo(nil)
	if err == nil {
		rs.CreateRepo(r)
	}

	return rs
}

// ListRepos returns the list of all known chart repositories
func (rs *inmemRepoService) ListRepos() (map[string]string, error) {
	rs.RLock()
	defer rs.RUnlock()

	ret := make(map[string]string)
	for _, r := range rs.repositories {
		ret[r.GetName()] = r.GetURL()
	}

	return ret, nil
}

// CreateRepo adds a known repository to the list
func (rs *inmemRepoService) CreateRepo(repository IRepo) error {
	rs.Lock()
	defer rs.Unlock()

	URL := repository.GetURL()
	name := repository.GetName()

	for u, r := range rs.repositories {
		if u == URL {
			return fmt.Errorf("Repository with URL %s already exists", URL)
		} else if r.GetName() == name {
			return fmt.Errorf("Repository with Name %s already exists", name)
		}
{'fraction_non_alphanumeric': 0.09648600357355569, 'fraction_numerical': 0.004764740917212627, 'mean_word_length': 4.3317460317460315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4127161', 'n_tokens_mistral': 1074, 'n_tokens_neox': 1042, 'n_words': 388}