mirror of
https://github.com/Luzifer/github2gitea.git
synced 2024-12-23 04:21:19 +00:00
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"regexp"
|
||
|
|
||
|
"github.com/pkg/errors"
|
||
|
"gopkg.in/yaml.v2"
|
||
|
)
|
||
|
|
||
|
type mapFile struct {
|
||
|
Mappings []mapping `yaml:"mappings"`
|
||
|
}
|
||
|
|
||
|
type mapping struct {
|
||
|
SourceExpression string `yaml:"source_expression"`
|
||
|
TargetUser int64 `yaml:"target_user"`
|
||
|
TargetUserName string `yaml:"target_user_name"`
|
||
|
}
|
||
|
|
||
|
func loadMapFile(fileName string) (*mapFile, error) {
|
||
|
if _, err := os.Stat(fileName); err != nil {
|
||
|
return nil, errors.Wrap(err, "Mapping file not available")
|
||
|
}
|
||
|
|
||
|
f, err := os.Open(fileName)
|
||
|
if err != nil {
|
||
|
return nil, errors.Wrap(err, "Unable to open mapping file")
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
var out = &mapFile{}
|
||
|
return out, errors.Wrap(yaml.NewDecoder(f).Decode(out), "Unable to decode mapping file")
|
||
|
}
|
||
|
|
||
|
func newMapFile() *mapFile {
|
||
|
return &mapFile{}
|
||
|
}
|
||
|
|
||
|
func (m mapFile) GetMapping(repoName string) *mapping {
|
||
|
for _, me := range m.Mappings {
|
||
|
if regexp.MustCompile(me.SourceExpression).MatchString(repoName) {
|
||
|
return &me
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m mapFile) MappingAvailable(repoName string) bool {
|
||
|
for _, me := range m.Mappings {
|
||
|
if regexp.MustCompile(me.SourceExpression).MatchString(repoName) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return false
|
||
|
}
|