1
0
Fork 0
mirror of https://github.com/Luzifer/github2gitea.git synced 2024-11-14 01:42:47 +00:00
github2gitea/mapfile.go
Knut Ahlers 494dbf16dd
Allow specifying multiple actions at once
in order to speed up the execution and to lower the need of Github
tokens. Instead of running the tool multiple times it can now be run
once while specifying more than one mapping using a mapping file.

Signed-off-by: Knut Ahlers <knut@ahlers.me>
2019-08-04 14:34:54 +02:00

58 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
}